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

update boat by id controller

parent 8f7c7643
No related branches found
No related tags found
No related merge requests found
...@@ -110,11 +110,61 @@ const deleteBoatById = async (req: Request, res: Response) => { ...@@ -110,11 +110,61 @@ const deleteBoatById = async (req: Request, res: Response) => {
} }
}; };
//update boat by given id
const updateBoatById = async (req: Request, res: Response) => {
try {
//check authority
if (!(res.locals.user.role === "coordinator")) {
return res
.status(403)
.json({ success: false, error: "MustBeCoordinator" });
}
//get id
const givenId = req.params.id;
//check if boat can be found using givenId
const foundBoat = await Boat.findByPk(givenId);
if (!foundBoat) {
return res.status(404).json({ success: false, error: "boatIdNotFound" });
}
//get new input
const input = req.body;
//try to update
const updatedBoat = await Boat.update(input, {
where: {
id: givenId,
},
returning: true,
});
//return after updating
const boatDataAfterUpdate = updatedBoat[1][0];
return res.status(200).json({
success: true,
result: {
id: boatDataAfterUpdate.id,
name: boatDataAfterUpdate.name,
boattype: boatDataAfterUpdate.boattype,
status: boatDataAfterUpdate.status,
tags: boatDataAfterUpdate.tags,
},
});
} catch (error) {
console.error("server error: ", error.message);
return res.status(500).json({ success: false, error: "serverError" });
}
};
const boatControllers = { const boatControllers = {
showAllBoatsController, showAllBoatsController,
showBoatById, showBoatById,
deleteBoatById, deleteBoatById,
createBoat, createBoat,
updateBoatById,
}; };
export default boatControllers; 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