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

modify show boatType using boat's id controller

parent 5b09f53f
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";
import BoatHasBoatType from "../db/models/BoatHasBoatType";
//createBoatTypeController
const createBoatTypeController = async (req: Request, res: Response) => {
......@@ -87,9 +88,22 @@ const showBoatTypeById = async (req: Request, res: Response) => {
}
const givenId = req.params.id;
const boat = await Boat.findByPk(givenId);
if (boat) {
const boattypeid = boat.boattype;
//find if boatid exists in BoatHasBoatType table => if not then boat doesn't have assigned boattype
const boatid = await BoatHasBoatType.findByPk(boat.id);
if (!boatid) {
return res.status(404).json({ success: false, error: "boatIdNotFoundInBoatHasBoatIdTable" });
}
//get boattypeid from BoatHasBoatType table which is assigned to boatid
const boattypeid = boatid.boattypeid;
//find this key in the BoatType table
const boatname = await BoatType.findByPk(boattypeid);
//return the type/name of the given boatid
return res
.status(200)
.json({ success: true, result: { name: boatname.name } });
......
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