diff --git a/server/src/controllers/boatControllers.controllers.ts b/server/src/controllers/boatControllers.controllers.ts index 0416a3611e77a44c56028d26d389ac41c9c4cf1c..852337d1dd48b8d0ed71ca36bab0288e42410e69 100644 --- a/server/src/controllers/boatControllers.controllers.ts +++ b/server/src/controllers/boatControllers.controllers.ts @@ -1,6 +1,36 @@ import { Request, Response } from "express"; +import BoatType from "../db/models/BoatType"; import Boat from "../db/models/Boat"; +//create boat +const createBoat = async (req: Request, res: Response) => { + try { + if (!(res.locals.user.role == "coordinator")) { + return res + .status(403) + .json({ success: false, error: "MustBeCoordinator" }); + } + + const newBoatInput = req.body; + + const boatType = await BoatType.findByPk(newBoatInput.boattype); + if(!boatType){ + return res.status(404).json({success: false, error: "boattypeNotFound"}) + } + const newBoat = await Boat.create(newBoatInput); + + if (newBoat) { + return res + .status(201) + .json({ success: true, result: newBoat }); + } + } catch (error) { + console.error(error.message); + return res.status(500).json({ success: false, error: "serverError" }); + } +}; + + //show all boats const showAllBoatsController = async (req: Request, res: Response) => { try { @@ -53,33 +83,6 @@ const deleteBoatById = async (req: Request, res: Response) => { } }; -//create boat -const createBoat = async (req: Request, res: Response) => { - try { - if (!(res.locals.user.role == "coordinator")) { - return res - .status(403) - .json({ success: false, error: "MustBeCoordinator" }); - } - - const newBoatInput = req.body; - - console.log("input von req.body: ", newBoatInput); - - const newBoat = await Boat.create(newBoatInput); - - const { id, name, boattype, status, active, tags } = newBoat; - - if (newBoat) { - return res - .status(201) - .json({ success: true, result: { id, name, boattype, status, active, tags } }); - } - } catch (error) { - console.error(error.message); - return res.status(500).json({ success: false, error: "serverError" }); - } -}; const boatControllers = { showAllBoatsController, diff --git a/server/src/controllers/boatTypeControllers.controllers.ts b/server/src/controllers/boatTypeControllers.controllers.ts index 9e80d2c4c4176432179c3a4bf471e7ca8d5ffd8d..c020d3d094aa45282a2b3615ec70346f7b4cef36 100644 --- a/server/src/controllers/boatTypeControllers.controllers.ts +++ b/server/src/controllers/boatTypeControllers.controllers.ts @@ -2,23 +2,6 @@ import { Request, Response } from "express"; import BoatType from "../db/models/BoatType"; import Boat from "../db/models/Boat"; - -//show all BoatTypes -const showAllBoatTypes = async (req: Request, res: Response) => { - try { - if (!(res.locals.user.role === "coordinator")) { - return res - .status(403) - .json({ success: false, error: "MustBeCoordinator" }); - } - const allBoatTypes = await BoatType.findAll(); - return res.status(200).send({ success: true, result: allBoatTypes }); - } catch (error) { - console.error("server error: ", error.message); - return res.status(500).json({ success: false, error: "serverError" }); - } -}; - //createBoatTypeController const createBoatTypeController = async (req: Request, res: Response) => { try { @@ -32,12 +15,15 @@ const createBoatTypeController = async (req: Request, res: Response) => { const newBoatType = await BoatType.create(newBoatTypeInput); - const { id, name, seats } = newBoatType; - if (newBoatType) { - return res - .status(201) - .json({ success: true, result: { id, name, seats } }); + return res.status(201).json({ + success: true, + result: { + id: newBoatType.id, + name: newBoatType.name, + seats: newBoatType.seats, + }, + }); } } catch (error) { console.error(error.message); @@ -45,6 +31,27 @@ const createBoatTypeController = async (req: Request, res: Response) => { } }; +//show all BoatTypes +const showAllBoatTypes = async (req: Request, res: Response) => { + try { + if (!(res.locals.user.role === "coordinator")) { + return res + .status(403) + .json({ success: false, error: "MustBeCoordinator" }); + } + const allBoatTypes = await BoatType.findAll(); + return res.status(200).send({ + success: true, + result: allBoatTypes.map((boattype) => { + return { id: boattype.id, name: boattype.name, seats: boattype.seats }; + }), + }); + } catch (error) { + console.error("server error: ", error.message); + return res.status(500).json({ success: false, error: "serverError" }); + } +}; + //delete specific BoatType using given id const deleteBoatTypeById = async (req: Request, res: Response) => { try { @@ -54,7 +61,7 @@ const deleteBoatTypeById = async (req: Request, res: Response) => { .json({ success: false, error: "MustBeCoordinator" }); } const givenId = req.params.id; - const boatToDelete = await BoatType.destroy({ + const boatToDelete = await BoatType.destroy({ where: { id: givenId, }, @@ -71,7 +78,6 @@ const deleteBoatTypeById = async (req: Request, res: Response) => { } }; - //show type of a boat using boat's id const showBoatTypeById = async (req: Request, res: Response) => { try { @@ -81,11 +87,13 @@ const showBoatTypeById = async (req: Request, res: Response) => { .json({ success: false, error: "MustBeCoordinator" }); } const givenId = req.params.id; - const boatid = await Boat.findByPk(givenId); - if (boatid) { - const boattypeid = boatid.boattype; + const boat = await Boat.findByPk(givenId); + if (boat) { + const boattypeid = boat.boattype; const boatname = await BoatType.findByPk(boattypeid); - return res.status(200).json({ success: true, result: { name:boatname.name } }); + return res + .status(200) + .json({ success: true, result: { name: boatname.name } }); } return res.status(404).json({ success: false, error: "boatIdNotFound" }); } catch (error) { @@ -94,11 +102,10 @@ const showBoatTypeById = async (req: Request, res: Response) => { } }; - const boatTypeControllers = { showAllBoatTypes, createBoatTypeController, - deleteBoatTypeById, + deleteBoatTypeById, showBoatTypeById, };