From dfbe560cb1a6560f6489279d4f482c924cce5016 Mon Sep 17 00:00:00 2001
From: elit04 <elit04@fu-berlin.de>
Date: Wed, 19 Jan 2022 09:58:34 -0500
Subject: [PATCH] modify show boatType using boat's id controller

---
 server/src/controllers/boatType.controllers.ts | 16 +++++++++++++++-
 1 file changed, 15 insertions(+), 1 deletion(-)

diff --git a/server/src/controllers/boatType.controllers.ts b/server/src/controllers/boatType.controllers.ts
index d589a24..b42f8d4 100644
--- a/server/src/controllers/boatType.controllers.ts
+++ b/server/src/controllers/boatType.controllers.ts
@@ -1,6 +1,7 @@
 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 } });
-- 
GitLab