Skip to content

Commit 6155eea

Browse files
authored
Update server.js
1 parent a30858c commit 6155eea

File tree

1 file changed

+59
-6
lines changed

1 file changed

+59
-6
lines changed

src/npm/server.js

Lines changed: 59 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ let wikis = [
1313
];
1414

1515
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'));
1818

1919
// Authorized users for editing and deleting wikis
2020
const authorizedUsers = ['kRxZy_kRxZy', 'MyScratchedAccount', 'mcgdj'];
@@ -27,14 +27,17 @@ const isAuthorized = (username, wikiOwner) => {
2727
// API: Create a new wiki
2828
app.post('/api/wikis', (req, res) => {
2929
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+
}
3033
const newWiki = { id: wikis.length + 1, title, content, owner };
3134
wikis.push(newWiki);
32-
res.status(201).json(newWiki); // Return the created wiki as JSON
35+
res.status(201).json(newWiki);
3336
});
3437

3538
// API: Get all wikis
3639
app.get('/api/wikis', (req, res) => {
37-
res.json(wikis); // Return all wikis as JSON
40+
res.json(wikis);
3841
});
3942

4043
// API: Get a specific wiki by ID
@@ -44,7 +47,29 @@ app.get('/api/wikis/:id', (req, res) => {
4447
if (!wiki) {
4548
return res.status(404).json({ error: 'Wiki not found' });
4649
}
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' });
4873
});
4974

5075
// Serve HTML page for a specific wiki title
@@ -72,11 +97,13 @@ app.get('/wiki/:title', (req, res) => {
7297
.wiki-content h2 { color: #e60000; font-size: 2.5rem; margin-bottom: 20px; text-align: center; }
7398
.wiki-content p { font-size: 1.5rem; color: #333; line-height: 1.6; text-align: center; }
7499
.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; }
76101
.edit-button { background: #ffcc00; }
77102
.edit-button:hover { background: #ffaa00; }
78103
.report-button { background: #ff4d4d; }
79104
.report-button:hover { background: #ff1a1a; }
105+
.delete-button { background: #d11a2a; }
106+
.delete-button:hover { background: #a3001b; }
80107
</style>
81108
</head>
82109
<body>
@@ -91,8 +118,34 @@ app.get('/wiki/:title', (req, res) => {
91118
<div class="button-container">
92119
<a href="https://scratch-coding-hut.github.io/Wiki/edit?edit=${encodeURIComponent(wiki.title)}" class="edit-button">Edit Wiki</a>
93120
<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>
94122
</div>
95123
</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>
96149
</body>
97150
</html>`);
98151
});

0 commit comments

Comments
 (0)