From 5bcf359aa438a1b33ec55ee707f1246ff44f4aba Mon Sep 17 00:00:00 2001 From: Rahul Date: Thu, 6 Jul 2023 21:38:19 +0530 Subject: [PATCH] [added] end point for delete infrastructure and its apidoc --- _apidoc.js | 4 ++-- controller/infrastructure.js | 12 ++++++------ error/DataDeleteError.js | 6 ++++++ error/database.js | 3 ++- routes/infrastructure.js | 2 +- services/infrastructure.js | 12 ++++++------ 6 files changed, 23 insertions(+), 16 deletions(-) create mode 100644 error/DataDeleteError.js diff --git a/_apidoc.js b/_apidoc.js index 580349c..d9eb7fc 100644 --- a/_apidoc.js +++ b/_apidoc.js @@ -18,11 +18,11 @@ // History. // ------------------------------------------------------------------------------------------ /** - * @api {delete} /infrastructure/delete/:documentID Delete Infrastructure + * @api {delete} /infrastructure/delete/:infrastructureId Delete Infrastructure * @apiName DeleteInfrastructure * @apiGroup Infrastructure * - * @apiParam {String} documentID The ID of the infrastructure document to delete. + * @apiParam {String} infrastructureId The ID of the infrastructure document to delete. * * @apiSuccess {String} res Success message indicating the deletion. * diff --git a/controller/infrastructure.js b/controller/infrastructure.js index c9a6e64..061e708 100644 --- a/controller/infrastructure.js +++ b/controller/infrastructure.js @@ -1,4 +1,4 @@ -import { createInfrastructure, deleteInfrastructure } from "#services/infrastructure"; +import { createInfrastructure, deleteInfrastructureById } from "#services/infrastructure"; import { logger } from "#util"; async function addinfrastructure(req, res) { @@ -15,15 +15,15 @@ async function addinfrastructure(req, res) { } } -async function deleteinfrastructure(req, res) { - const { documentID } = req.params; +async function deleteInfrastructure(req, res) { + const { infrastructureId } = req.params; try { - await deleteInfrastructure(documentID); + await deleteInfrastructureById(infrastructureId); - res.json({ res: `Deleted infrastructure with ID ${documentID}` }); + res.json({ res: `Deleted infrastructure with ID ${infrastructureId}` }); } catch (error) { logger.error("Error while deleting", error); res.status(500).json({ error: "Error while deleting from DB" }); } } -export default { addinfrastructure, deleteinfrastructure }; +export default { addinfrastructure, deleteInfrastructure }; diff --git a/error/DataDeleteError.js b/error/DataDeleteError.js new file mode 100644 index 0000000..3252446 --- /dev/null +++ b/error/DataDeleteError.js @@ -0,0 +1,6 @@ +export class DataDeleteError extends Error { + constructor(modelName) { + super(`Error while deleting document in ${modelName}`); + this.name = "DataDeleteError"; + } + } \ No newline at end of file diff --git a/error/database.js b/error/database.js index fd3dccb..93571c8 100644 --- a/error/database.js +++ b/error/database.js @@ -2,7 +2,8 @@ import { DataEntryError } from "#error/DataEntryError"; import { DataNotFoundError } from "#error/DataNotFoundError"; import { UpdateError } from "#error/UpdateError"; import { UserDoesNotExistError } from "#error/UserDoesNotExist"; +import { DataDeleteError } from "#error/DataDeleteError"; export default { - DataEntryError, DataNotFoundError, UpdateError, UserDoesNotExistError, + DataEntryError, DataNotFoundError, UpdateError, UserDoesNotExistError,DataDeleteError, }; diff --git a/routes/infrastructure.js b/routes/infrastructure.js index fc34b9c..b43bc4f 100644 --- a/routes/infrastructure.js +++ b/routes/infrastructure.js @@ -3,6 +3,6 @@ import infrastructureController from "#controller/infrastructure"; const router = express.Router(); router.post("/add", infrastructureController.addinfrastructure); -router.post("/delete/:documentID", infrastructureController.deleteinfrastructure); +router.post("/delete/:infrastructureId", infrastructureController.deleteInfrastructure); export default router; diff --git a/services/infrastructure.js b/services/infrastructure.js index fd7cc51..e26707a 100644 --- a/services/infrastructure.js +++ b/services/infrastructure.js @@ -11,11 +11,11 @@ export async function createInfrastructure(name, type, wing, floor, capacity) { throw new databaseError.DataEntryError("infrastructure"); } -export async function deleteInfrastructure(documentID) { - try { - await infrastructure.findOneAndDelete({ _id: documentID }); - } catch (error) { - // Handle any errors that occur during the deletion process - throw new Error(`Error deleting infrastructure: ${error.message}`); +export async function deleteInfrastructureById(infrastructureId) { + const deleted = await infrastructure.remove({ _id: infrastructureId }); + if (deleted){ + return deleted; } + throw new databaseError.DataDeleteError("infrastructure"); + }