diff --git a/server/src/controllers/boatControllers.controllers.ts b/server/src/controllers/boatControllers.controllers.ts index 64d17ad531215c41f3894eeb1e96b87ae0c15860..3b204482f832d828d9d60b386b1823346bafc786 100644 --- a/server/src/controllers/boatControllers.controllers.ts +++ b/server/src/controllers/boatControllers.controllers.ts @@ -1,16 +1,18 @@ 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;