From 1657c2f1cba861bb70d00043d0670cb820f1ff3a Mon Sep 17 00:00:00 2001 From: Hricha Mehra Date: Wed, 5 Jul 2023 16:23:42 +0530 Subject: [PATCH 1/5] added a route for delete in accredation and added function for deletion to controller --- controller/accreditation.js | 14 ++++++++++++-- routes/accreditation.js | 3 ++- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/controller/accreditation.js b/controller/accreditation.js index 8d1f285..73673cd 100644 --- a/controller/accreditation.js +++ b/controller/accreditation.js @@ -15,5 +15,15 @@ async function addAccreditation(req, res) { res.json({ err: "Error while inserting in DB" }); } } - -export default { addAccreditation }; +async function deleteAccreditation(req, res) { + const { documentID } = req.params; + try { + await deleteAccreditationById(documentID); + res.json({ res: "Accreditation deleted successfully" }); + } catch (error) { + logger.error("Error while deleting", error); + res.status(500); + res.json({ err: "Error while deleting from DB" }); + } +} +export default { addAccreditation, deleteAccreditation }; diff --git a/routes/accreditation.js b/routes/accreditation.js index d8769c6..e973630 100644 --- a/routes/accreditation.js +++ b/routes/accreditation.js @@ -3,4 +3,5 @@ import accreditationController from "#controller/accreditation"; const router = express.Router(); router.post("/add", accreditationController.addAccreditation); -export default router; +router.delete("/delete", accreditationController.deleteAccreditation); +export default router; \ No newline at end of file From af57f16839acb9558cdaf6b89c95eb7ae41a506c Mon Sep 17 00:00:00 2001 From: Hricha Mehra Date: Wed, 5 Jul 2023 21:00:39 +0530 Subject: [PATCH 2/5] added a route for delete in accreditation and added function for deletion to controller and services. --- controller/accreditation.js | 7 ++++--- routes/accreditation.js | 2 +- services/accreditation.js | 11 +++++++++++ 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/controller/accreditation.js b/controller/accreditation.js index 73673cd..c5898f5 100644 --- a/controller/accreditation.js +++ b/controller/accreditation.js @@ -1,4 +1,4 @@ -import { addNewAccreditation } from "#services/accreditation"; +import { addNewAccreditation, deleteAccreditationById } from "#services/accreditation"; import { logger } from "#util"; async function addAccreditation(req, res) { @@ -16,9 +16,10 @@ async function addAccreditation(req, res) { } } async function deleteAccreditation(req, res) { - const { documentID } = req.params; + const { accreditationID } = req.params; + console.log(accreditationID) try { - await deleteAccreditationById(documentID); + await deleteAccreditationById(accreditationID); res.json({ res: "Accreditation deleted successfully" }); } catch (error) { logger.error("Error while deleting", error); diff --git a/routes/accreditation.js b/routes/accreditation.js index e973630..849b0ed 100644 --- a/routes/accreditation.js +++ b/routes/accreditation.js @@ -3,5 +3,5 @@ import accreditationController from "#controller/accreditation"; const router = express.Router(); router.post("/add", accreditationController.addAccreditation); -router.delete("/delete", accreditationController.deleteAccreditation); +router.delete("/delete/:accreditationID", accreditationController.deleteAccreditation); export default router; \ No newline at end of file diff --git a/services/accreditation.js b/services/accreditation.js index 4563a25..4d73e8b 100644 --- a/services/accreditation.js +++ b/services/accreditation.js @@ -1,5 +1,8 @@ import accreditation from "#models/accreditation"; import databaseError from "#error/database"; +import { logger } from "#util"; +import database from "#error/database"; +import router from "#routes/index"; export async function addNewAccreditation(name, agencyName, dateofAccreditation, dateofExpiry) { const newAccreditation = await accreditation.create( @@ -13,3 +16,11 @@ export async function addNewAccreditation(name, agencyName, dateofAccreditation, } throw new databaseError.DataEntryError("Accreditation"); } + +export async function deleteAccreditationById(accreditationID){ + const result = await router.delete('/accreditations/:accreditationID', (req, res) => { + const { accreditationID } = req.params; + });} +export default { + deleteAccreditationById, addNewAccreditation +} \ No newline at end of file From ce0beebd5a1d08b572163acfee61457748aa1e9d Mon Sep 17 00:00:00 2001 From: Hricha Mehra Date: Thu, 6 Jul 2023 19:38:43 +0530 Subject: [PATCH 3/5] updated module --- models/accreditation.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/models/accreditation.js b/models/accreditation.js index 26df3f1..12bcb51 100644 --- a/models/accreditation.js +++ b/models/accreditation.js @@ -9,8 +9,8 @@ const accreditationSchema = { const Accreditation = connector.model("Accreditation", accreditationSchema); -async function remove(filter) { - const res = await Accreditation.findOneAndDelete(filter); +async function remove(accreditationID) { + const res = await Accreditation.findOneAndDelete(accreditationID); return res; } From 2baed6624c3ff78204cd574de79e0fa71d788c63 Mon Sep 17 00:00:00 2001 From: Hricha Mehra Date: Thu, 6 Jul 2023 20:13:14 +0530 Subject: [PATCH 4/5] updated --- controller/accreditation.js | 6 +++--- models/accreditation.js | 4 ++-- routes/accreditation.js | 2 +- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/controller/accreditation.js b/controller/accreditation.js index c5898f5..c5f6741 100644 --- a/controller/accreditation.js +++ b/controller/accreditation.js @@ -16,10 +16,10 @@ async function addAccreditation(req, res) { } } async function deleteAccreditation(req, res) { - const { accreditationID } = req.params; - console.log(accreditationID) + const { filter } = req.params; + console.log(filter) try { - await deleteAccreditationById(accreditationID); + await deleteAccreditationById(filter); res.json({ res: "Accreditation deleted successfully" }); } catch (error) { logger.error("Error while deleting", error); diff --git a/models/accreditation.js b/models/accreditation.js index 12bcb51..26df3f1 100644 --- a/models/accreditation.js +++ b/models/accreditation.js @@ -9,8 +9,8 @@ const accreditationSchema = { const Accreditation = connector.model("Accreditation", accreditationSchema); -async function remove(accreditationID) { - const res = await Accreditation.findOneAndDelete(accreditationID); +async function remove(filter) { + const res = await Accreditation.findOneAndDelete(filter); return res; } diff --git a/routes/accreditation.js b/routes/accreditation.js index 849b0ed..cce87e9 100644 --- a/routes/accreditation.js +++ b/routes/accreditation.js @@ -3,5 +3,5 @@ import accreditationController from "#controller/accreditation"; const router = express.Router(); router.post("/add", accreditationController.addAccreditation); -router.delete("/delete/:accreditationID", accreditationController.deleteAccreditation); +router.delete("/delete/:filter", accreditationController.deleteAccreditation); export default router; \ No newline at end of file From 41f502c251af0058711d04e2b496846371432a4d Mon Sep 17 00:00:00 2001 From: Hricha Mehra Date: Thu, 6 Jul 2023 21:51:05 +0530 Subject: [PATCH 5/5] added service function --- controller/accreditation.js | 5 ++--- routes/accreditation.js | 4 ++-- services/accreditation.js | 14 +++++++------- 3 files changed, 11 insertions(+), 12 deletions(-) diff --git a/controller/accreditation.js b/controller/accreditation.js index c5f6741..b3a5209 100644 --- a/controller/accreditation.js +++ b/controller/accreditation.js @@ -16,10 +16,9 @@ async function addAccreditation(req, res) { } } async function deleteAccreditation(req, res) { - const { filter } = req.params; - console.log(filter) + const { accredationId} = req.params; try { - await deleteAccreditationById(filter); + await deleteAccreditationById(accredationId); res.json({ res: "Accreditation deleted successfully" }); } catch (error) { logger.error("Error while deleting", error); diff --git a/routes/accreditation.js b/routes/accreditation.js index cce87e9..94e72c4 100644 --- a/routes/accreditation.js +++ b/routes/accreditation.js @@ -3,5 +3,5 @@ import accreditationController from "#controller/accreditation"; const router = express.Router(); router.post("/add", accreditationController.addAccreditation); -router.delete("/delete/:filter", accreditationController.deleteAccreditation); -export default router; \ No newline at end of file +router.delete("/delete/:accredationId", accreditationController.deleteAccreditation); +export default router; diff --git a/services/accreditation.js b/services/accreditation.js index 4d73e8b..51c8664 100644 --- a/services/accreditation.js +++ b/services/accreditation.js @@ -1,8 +1,5 @@ import accreditation from "#models/accreditation"; import databaseError from "#error/database"; -import { logger } from "#util"; -import database from "#error/database"; -import router from "#routes/index"; export async function addNewAccreditation(name, agencyName, dateofAccreditation, dateofExpiry) { const newAccreditation = await accreditation.create( @@ -17,10 +14,13 @@ export async function addNewAccreditation(name, agencyName, dateofAccreditation, throw new databaseError.DataEntryError("Accreditation"); } -export async function deleteAccreditationById(accreditationID){ - const result = await router.delete('/accreditations/:accreditationID', (req, res) => { - const { accreditationID } = req.params; - });} +export async function deleteAccreditationById(accredationId){ + const deleted = await accreditation.remove({_id: accredationId}); + if (deleted) { + return deleted + } + throw new databaseError.DataDeleteError("Accreditation"); +} export default { deleteAccreditationById, addNewAccreditation } \ No newline at end of file