From dfbcf172ad7d56b596ca65bd9e5dc33206422616 Mon Sep 17 00:00:00 2001 From: furkansimsekli Date: Thu, 27 Oct 2022 20:00:15 +0300 Subject: [PATCH] backend.crud: add updating and deleting operations for members, fix #22 --- controllers/main.js | 38 +++++++++++++++++++++++++------------- routes/main.js | 1 + 2 files changed, 26 insertions(+), 13 deletions(-) diff --git a/controllers/main.js b/controllers/main.js index 34be0f7..f06de1a 100644 --- a/controllers/main.js +++ b/controllers/main.js @@ -66,22 +66,24 @@ const enroll = async (req, res) => { } } - if (!oldMember) { - const newMember = { - firstName: req.body.firstName, - lastName: req.body.lastName, - studentID: req.body.studentID, - degree: req.body.degree, - email: req.body.email, - department: req.body.department, - mobileNumber: req.body.mobileNumber, - groupChat: req.body.groupChat - } + const newMember = { + firstName: req.body.firstName, + lastName: req.body.lastName, + studentID: req.body.studentID, + degree: req.body.degree, + email: req.body.email, + department: req.body.department, + mobileNumber: req.body.mobileNumber, + groupChat: req.body.groupChat + } + if (!oldMember) { await Member.insertMany(newMember); + res.redirect('/welcome'); + } else { + await Member.findOneAndUpdate({ studentID: req.body.studentID }, newMember, { runValidators: true, new: true }) + res.render(boilerplate, { page: "../success" }); } - - res.redirect('/welcome'); } const contact = async (req, res) => { @@ -216,6 +218,15 @@ const deleteFaq = async (req, res) => { } } +const deleteMember = async (req, res) => { + if (req.body.auth === process.env.AUTH_KEY) { + await Member.findOneAndDelete({ studentID: req.body.studentID }); + res.sendStatus(200); + } else { + res.sendStatus(401); + } +} + const updateEvent = async (req, res) => { if (req.body.auth === process.env.AUTH_KEY) { const event = { @@ -325,6 +336,7 @@ module.exports = { deleteCourse, deleteProject, deleteFaq, + deleteMember, updateEvent, updateCourse, updateProject, diff --git a/routes/main.js b/routes/main.js index b359ad7..281a01a 100644 --- a/routes/main.js +++ b/routes/main.js @@ -25,6 +25,7 @@ router.delete('/event', controller.deleteEvent); router.delete('/course', controller.deleteCourse); router.delete('/arge', controller.deleteProject); router.delete('/faq', controller.deleteFaq); +router.delete('/members', controller.deleteMember); router.patch('/event', controller.updateEvent); router.patch('/course', controller.updateCourse);