Skip to content
Snippets Groups Projects
Commit b36eefd4 authored by elit04's avatar elit04
Browse files

show boat type according to given boat id Controller+Route

parent a4fa36d5
No related branches found
No related tags found
No related merge requests found
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;
......@@ -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;
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