@@ -13,8 +13,8 @@ let wikis = [
13
13
] ;
14
14
15
15
app . use ( bodyParser . json ( ) ) ;
16
- app . use ( cors ( ) ) ; // Enable CORS for all requests
17
- app . use ( express . static ( 'public' ) ) ; // Serve static files (e.g., images, CSS)
16
+ app . use ( cors ( ) ) ;
17
+ app . use ( express . static ( 'public' ) ) ;
18
18
19
19
// Authorized users for editing and deleting wikis
20
20
const authorizedUsers = [ 'kRxZy_kRxZy' , 'MyScratchedAccount' , 'mcgdj' ] ;
@@ -27,14 +27,17 @@ const isAuthorized = (username, wikiOwner) => {
27
27
// API: Create a new wiki
28
28
app . post ( '/api/wikis' , ( req , res ) => {
29
29
const { title, content, owner } = req . body ;
30
+ if ( ! title || ! content || ! owner ) {
31
+ return res . status ( 400 ) . json ( { error : 'Title, content, and owner are required' } ) ;
32
+ }
30
33
const newWiki = { id : wikis . length + 1 , title, content, owner } ;
31
34
wikis . push ( newWiki ) ;
32
- res . status ( 201 ) . json ( newWiki ) ; // Return the created wiki as JSON
35
+ res . status ( 201 ) . json ( newWiki ) ;
33
36
} ) ;
34
37
35
38
// API: Get all wikis
36
39
app . get ( '/api/wikis' , ( req , res ) => {
37
- res . json ( wikis ) ; // Return all wikis as JSON
40
+ res . json ( wikis ) ;
38
41
} ) ;
39
42
40
43
// API: Get a specific wiki by ID
@@ -44,7 +47,29 @@ app.get('/api/wikis/:id', (req, res) => {
44
47
if ( ! wiki ) {
45
48
return res . status ( 404 ) . json ( { error : 'Wiki not found' } ) ;
46
49
}
47
- res . json ( wiki ) ; // Return the specific wiki as JSON
50
+ res . json ( wiki ) ;
51
+ } ) ;
52
+
53
+ // API: Delete a wiki by ID
54
+ app . delete ( '/api/wikis/:id' , ( req , res ) => {
55
+ const { id } = req . params ;
56
+ const { username } = req . body ; // Assume the username is sent in the request body
57
+
58
+ const wikiIndex = wikis . findIndex ( wiki => wiki . id === parseInt ( id ) ) ;
59
+ if ( wikiIndex === - 1 ) {
60
+ return res . status ( 404 ) . json ( { error : 'Wiki not found' } ) ;
61
+ }
62
+
63
+ const wiki = wikis [ wikiIndex ] ;
64
+
65
+ // Check if the user is authorized to delete
66
+ if ( ! isAuthorized ( username , wiki . owner ) ) {
67
+ return res . status ( 403 ) . json ( { error : 'Unauthorized to delete this wiki' } ) ;
68
+ }
69
+
70
+ wikis . splice ( wikiIndex , 1 ) ;
71
+
72
+ res . json ( { message : 'Wiki deleted successfully' } ) ;
48
73
} ) ;
49
74
50
75
// Serve HTML page for a specific wiki title
@@ -72,11 +97,13 @@ app.get('/wiki/:title', (req, res) => {
72
97
.wiki-content h2 { color: #e60000; font-size: 2.5rem; margin-bottom: 20px; text-align: center; }
73
98
.wiki-content p { font-size: 1.5rem; color: #333; line-height: 1.6; text-align: center; }
74
99
.button-container { margin-top: 20px; display: flex; gap: 10px; justify-content: center; }
75
- .edit-button, .report-button { display: inline-block; padding: 10px 20px; font-size: 1.2rem; border: none; border-radius: 5px; cursor: pointer; transition: background 0.3s; text-decoration: none; color: white; }
100
+ .edit-button, .report-button, .delete-button { display: inline-block; padding: 10px 20px; font-size: 1.2rem; border: none; border-radius: 5px; cursor: pointer; transition: background 0.3s; text-decoration: none; color: white; }
76
101
.edit-button { background: #ffcc00; }
77
102
.edit-button:hover { background: #ffaa00; }
78
103
.report-button { background: #ff4d4d; }
79
104
.report-button:hover { background: #ff1a1a; }
105
+ .delete-button { background: #d11a2a; }
106
+ .delete-button:hover { background: #a3001b; }
80
107
</style>
81
108
</head>
82
109
<body>
@@ -91,8 +118,34 @@ app.get('/wiki/:title', (req, res) => {
91
118
<div class="button-container">
92
119
<a href="https://scratch-coding-hut.github.io/Wiki/edit?edit=${ encodeURIComponent ( wiki . title ) } " class="edit-button">Edit Wiki</a>
93
120
<a href="https://scratch-coding-hut.github.io/Wiki/report.html?wiki=${ encodeURIComponent ( wiki . title ) } " class="report-button">Report</a>
121
+ <button class="delete-button" onclick="deleteWiki(${ wiki . id } )">Delete Wiki</button>
94
122
</div>
95
123
</div>
124
+
125
+ <script>
126
+ function deleteWiki(wikiId) {
127
+ const username = prompt("Enter your username to confirm deletion:");
128
+ if (!username) return alert("Deletion cancelled.");
129
+
130
+ if (!confirm("Are you sure you want to delete this wiki?")) return;
131
+
132
+ fetch(\`/api/wikis/\${wikiId}\`, {
133
+ method: "DELETE",
134
+ headers: { "Content-Type": "application/json" },
135
+ body: JSON.stringify({ username })
136
+ })
137
+ .then(response => response.json())
138
+ .then(data => {
139
+ if (data.error) {
140
+ alert("Error: " + data.error);
141
+ } else {
142
+ alert("Wiki deleted successfully.");
143
+ window.location.href = "https://scratch-coding-hut.github.io/index.html";
144
+ }
145
+ })
146
+ .catch(error => console.error("Error deleting wiki:", error));
147
+ }
148
+ </script>
96
149
</body>
97
150
</html>` ) ;
98
151
} ) ;
0 commit comments