Skip to content

Commit

Permalink
[added] end point for delete infrastructure and its apidoc
Browse files Browse the repository at this point in the history
  • Loading branch information
rahulsingh2312 committed Jul 6, 2023
1 parent 455a656 commit 5bcf359
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 16 deletions.
4 changes: 2 additions & 2 deletions _apidoc.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
12 changes: 6 additions & 6 deletions controller/infrastructure.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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 };
6 changes: 6 additions & 0 deletions error/DataDeleteError.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export class DataDeleteError extends Error {
constructor(modelName) {
super(`Error while deleting document in ${modelName}`);
this.name = "DataDeleteError";
}
}
3 changes: 2 additions & 1 deletion error/database.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
2 changes: 1 addition & 1 deletion routes/infrastructure.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
12 changes: 6 additions & 6 deletions services/infrastructure.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");

}

0 comments on commit 5bcf359

Please sign in to comment.