diff --git a/server/src/controllers/boatTypeControllers.controllers.ts b/server/src/controllers/boatTypeControllers.controllers.ts index 28030d41e6bc7e56ce0db164103689592aa08178..9e80d2c4c4176432179c3a4bf471e7ca8d5ffd8d 100644 --- a/server/src/controllers/boatTypeControllers.controllers.ts +++ b/server/src/controllers/boatTypeControllers.controllers.ts @@ -1,5 +1,7 @@ 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) => { @@ -28,8 +30,6 @@ const createBoatTypeController = async (req: Request, res: Response) => { const newBoatTypeInput = req.body; - console.log("input von req.body: ", newBoatTypeInput); - const newBoatType = await BoatType.create(newBoatTypeInput); const { id, name, seats } = newBoatType; @@ -72,10 +72,34 @@ const deleteBoatTypeById = async (req: Request, res: Response) => { }; +//show type of a boat using boat's id +const showBoatTypeById = 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 boatid = await Boat.findByPk(givenId); + if (boatid) { + const boattypeid = boatid.boattype; + const boatname = await BoatType.findByPk(boattypeid); + return res.status(200).json({ success: true, result: { name:boatname.name } }); + } + return res.status(404).json({ success: false, error: "boatIdNotFound" }); + } catch (error) { + console.error("server error: ", error.message); + return res.status(500).json({ success: false, error: "serverError" }); + } +}; + + const boatTypeControllers = { showAllBoatTypes, createBoatTypeController, - deleteBoatTypeById + deleteBoatTypeById, + showBoatTypeById, }; export default boatTypeControllers; diff --git a/server/src/routes/boatTypeRoutes.routes.ts b/server/src/routes/boatTypeRoutes.routes.ts index 4cb570b434c11002b0f109cd4d54611e6f050e81..69d006e068207eca8e634e0e08bfbb64623c342e 100644 --- a/server/src/routes/boatTypeRoutes.routes.ts +++ b/server/src/routes/boatTypeRoutes.routes.ts @@ -30,4 +30,11 @@ boatTypeRouter.delete( boatTypeControllers.deleteBoatTypeById ); +//show specific boat type by given boat id +boatTypeRouter.get( + "/api/boattype/:id/", + validateToken, + boatTypeControllers.showBoatTypeById +); + export default boatTypeRouter;