Skip to content
Snippets Groups Projects
Commit 9ac880d7 authored by alrwasheda's avatar alrwasheda :speech_balloon:
Browse files

deleteBoatById Controller

parent d713a63c
No related branches found
No related tags found
No related merge requests found
import { Request, Response } from "express";
import Boat from "../db/models/Boat";
//show all boats
const showAllBoatsController = async (req: Request, res: Response) => {
try {
const allBoats = await Boat.findAll();
return res.status(200).send({ success: true, allBoats });
return res.status(200).send({ success: true, result: allBoats });
} catch (error) {
console.error("server error: ", error.message);
return res.status(500).json({ success: false, error: "serverError" });
}
};
//show specific boat using given id
const showBoatById = async (req: Request, res: Response) => {
try {
const givenId = req.params.id;
......@@ -25,6 +27,35 @@ const showBoatById = async (req: Request, res: Response) => {
}
};
const boatControllers = { showAllBoatsController, showBoatById };
//delete specific boat using given id
const deleteBoatById = async (req: Request, res: Response) => {
try {
if (!(res.locals.user.role === "coordinator")) {
return res
.status(403)
.json({ success: false, error: "MustBeCoordinator" });
}
const givenId = req.params.id;
const boatToDelete = await Boat.destroy({
where: {
id: givenId,
},
});
if (boatToDelete < 1) {
return res
.status(404)
.json({ success: false, error: "BoatIdDoesNotExist" });
}
return res.status(204).json({ success: true });
} catch (error) {
console.error("server error: ", error.message);
return res.status(500).json({ success: false, error: "serverError" });
}
};
const boatControllers = {
showAllBoatsController,
showBoatById,
deleteBoatById,
};
export default boatControllers;
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment