From 14cf3b1237359269d5146fa7698a1936f6ed42c6 Mon Sep 17 00:00:00 2001 From: Akif9748 Date: Fri, 26 Aug 2022 20:12:23 +0300 Subject: [PATCH] Added undelete ways to api --- README.md | 23 +++++++++++++---------- routes/api/routes/messages.js | 21 +++++++++++++++++++-- routes/api/routes/threads.js | 14 ++++++++++++++ routes/api/routes/users.js | 15 +++++++++++++++ util/APIDOCS.md | 3 +++ 5 files changed, 64 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 132d6d5..0494f1b 100644 --- a/README.md +++ b/README.md @@ -31,6 +31,10 @@ And, you can learn about API in `util/APIDOCS.md`. ## Roadmap +### TO-DO: +- If thread deleted, not show its messages in API. + +### Frontend ### User | To do | Is done? | Priority | | ----- | -------- | -------- | @@ -66,16 +70,15 @@ And, you can learn about API in `util/APIDOCS.md`. | Edit | 🔴 | MEDIUM | ### API -| To do | Is done? | Priority | -| ----- | -------- | -------- | -| RATELIMITS | 🟢 | MEDIUM | -| Other clients for forum via API | 🟢 | LOW | -| Get message**s** | 🟢 | MEDIUM | -| Send message | 🟢 | MEDIUM | -| Create thread | 🟢 | MEDIUM | -| Get info about thread | 🟢 | MEDIUM | -| Delete message & thread | 🟢 | HIGH | -| Edit message & thread | 🔴 | HIGH | +| To do | Is done? +| ----- | -------- +| RATELIMITS | 🟢 +| Get message**s** | 🟢 +| Create message & thread & user | 🟢 +| Get message & thread & user | 🟢 +| Delete message & thread & user | 🟢 +| Undelete message & thread & user | 🟢 +| Edit message & thread & user | 🔴 ### Other | To do | Is done? | Priority | diff --git a/routes/api/routes/messages.js b/routes/api/routes/messages.js index 8961485..00ea108 100644 --- a/routes/api/routes/messages.js +++ b/routes/api/routes/messages.js @@ -10,7 +10,7 @@ app.get("/:id", async (req, res) => { const message = await MessageModel.get(req.params.id); - if (!message || (message.deleted && req.user && !req.user.admin)) return res.error(404, `We don't have any thread with id ${id}.`); + if (!message || (message.deleted && req.user && !req.user.admin)) return res.error(404, `We don't have any message with id ${req.params.id}.`); res.complate(message.toObject({ virtuals: true })); @@ -59,7 +59,8 @@ app.post("/:id/react/:type", async (req, res) => { app.post("/:id/delete", async (req, res) => { const message = await MessageModel.get(req.params.id); - if (!message || (message.deleted && req.user && !req.user.admin)) return res.error(404, "We have not got any message declared as this id."); + if (!message || (message.deleted && req.user && !req.user.admin)) + return res.error(404, `We don't have any message with id ${req.params.id}.`); const user = req.user; if (user.id != message.authorID && !user.admin) return res.error(403, "You have not got permission for this."); @@ -70,4 +71,20 @@ app.post("/:id/delete", async (req, res) => { }) +app.post("/:id/undelete", async (req, res) => { + if (!req.user.admin) return res.error(403, "You have not got permission for this."); + + const message = await MessageModel.get(req.params.id); + + if (!message ) return res.error(404, `We don't have any message with id ${req.params.id}.`); + + if (!message.deleted) return res.error(404, "This message is not deleted, first, delete it."); + + message.deleted = false; + await message.save(); + + res.complate(message.toObject({ virtuals: true })); + +}) + module.exports = app; \ No newline at end of file diff --git a/routes/api/routes/threads.js b/routes/api/routes/threads.js index 0a04ff7..86508b0 100644 --- a/routes/api/routes/threads.js +++ b/routes/api/routes/threads.js @@ -65,5 +65,19 @@ app.post("/:id/delete", async (req, res) => { res.complate(thread.toObject({ virtuals: true })); }) +app.post("/:id/undelete", async (req, res) => { + if (!req.user.admin) return res.error(403, "You have not got permission for this."); + const thread = await ThreadModel.get(req.params.id); + + if (!thread ) return res.error(404, `We don't have any thread with id ${req.params.id}.`); + + if (!thread.deleted) return res.error(404, "This thread is not deleted, first, delete it."); + + thread.deleted = false; + await thread.save(); + + res.complate(thread.toObject({ virtuals: true })); + +}) module.exports = app; \ No newline at end of file diff --git a/routes/api/routes/users.js b/routes/api/routes/users.js index af7a2c1..75d4925 100644 --- a/routes/api/routes/users.js +++ b/routes/api/routes/users.js @@ -29,6 +29,21 @@ app.post("/:id/delete/", async (req, res) => { res.complate(member); }); +app.post("/:id/undelete/", async (req, res) => { + if (!req.user.admin) return res.error(403, "You have not got permission for this."); + + const member = await UserModel.get(id); + + if (!member ) return res.error(404, `We don't have any user with id ${req.params.id}.`); + + if (!member.deleted) return res.error(404, "This user is not deleted, first, delete it."); + + member.deleted = false; + await member.save(); + + res.complate(member.toObject({ virtuals: true })); + +}) app.post("/:id/admin/", async (req, res) => { const user = req.user; diff --git a/util/APIDOCS.md b/util/APIDOCS.md index b3e80b0..ec2448a 100644 --- a/util/APIDOCS.md +++ b/util/APIDOCS.md @@ -19,16 +19,19 @@ You need this headers for send request to API: ### Request types: - GET `/api/users/:id` for fetch user. - POST `/api/users/:id/delete` for delete user. +- POST `/api/users/:id/undelete` for undelete user. - POST `/api/users/:id/admin` for give admin permissions for a user. - GET `/api/threads/:id` for fetch thread. - GET `/api/threads/:id/messages/:limit` for fetch messages in thread. - POST `/api/threads` for create thread. - POST `/api/threads/:id/delete` for delete thread. +- POST `/api/threads/:id/undelete` for undelete thread. - GET `/api/messages/:id` for fetch message. - POST `/api/messages` for create message. - POST `/api/messages/:id/delete` for delete message. +- POST `/api/messages/:id/undelete` for undelete message. - POST `/api/messages/:id/react/:type` for react to a message. ### Example request: