From 293a3dba44c34e47332502bd8caebca48490fb7c Mon Sep 17 00:00:00 2001
From: elit04 <elit04@fu-berlin.de>
Date: Sun, 23 Jan 2022 12:21:43 -0500
Subject: [PATCH] modified boat/boattype controllers

---
 server/src/controllers/boat.controllers.ts    | 48 +++++++++++++++----
 .../src/controllers/boatType.controllers.ts   | 14 ++++++
 2 files changed, 52 insertions(+), 10 deletions(-)

diff --git a/server/src/controllers/boat.controllers.ts b/server/src/controllers/boat.controllers.ts
index 0106367..fd0df68 100644
--- a/server/src/controllers/boat.controllers.ts
+++ b/server/src/controllers/boat.controllers.ts
@@ -23,6 +23,21 @@ const createBoat = async (req: Request, res: Response) => {
         .json({ success: false, error: "boattypeNotFound" });
     }
 
+    //check if name of boat already exists
+    if (!(newBoatInput.name === undefined)) {
+      const checkBoatName = await Boat.findOne({
+        where: {
+          name: newBoatInput.name,
+        },
+      });
+
+      if (!(checkBoatName === null)) {
+        return res
+          .status(404)
+          .json({ success: false, error: "boatNameAlreadyExists" });
+      }
+    }
+
     //check if given sport-ids can be found
     const sportsArray = newBoatInput.sports;
     var check = true;
@@ -212,10 +227,23 @@ const updateBoatById = async (req: Request, res: Response) => {
       return res.status(404).json({ success: false, error: "boatIdNotFound" });
     }
 
-    const givenBoatType = input.boattype;
+    //check if name of boat already exists
+    if (!(input.name === undefined)) {
+      const checkBoatName = await Boat.findOne({
+        where: {
+          name: input.name,
+        },
+      });
+
+      if (!(checkBoatName === null)) {
+        return res
+          .status(404)
+          .json({ success: false, error: "boatNameAlreadyExists" });
+      }
+    }
 
     //we need to check if boattype is not null(wasn't provided in body), otherwise we have server error:  Cannot read properties of undefined
-    if (!(givenBoatType === undefined)) {
+    if (!(input.boattype === undefined)) {
       //check if new boattype can be found
       const checkBoatTypeId = await BoatType.findByPk(input.boattype);
 
@@ -226,6 +254,14 @@ const updateBoatById = async (req: Request, res: Response) => {
       }
     }
 
+    //try to update the attributes, which are in Boat table
+    const updatedBoat = await Boat.update(input, {
+      where: {
+        id: givenId,
+      },
+      returning: true,
+    });
+
     //check if new given sport-ids can be found
     const newSportsArray = input.sports;
 
@@ -284,14 +320,6 @@ const updateBoatById = async (req: Request, res: Response) => {
       }
     }
 
-    //try to update the attributes, which are in Boat table
-    const updatedBoat = await Boat.update(input, {
-      where: {
-        id: givenId,
-      },
-      returning: true,
-    });
-
     //we need this special case res, because sports is not an attribute assigned to Boat table, and if only sports provided as request body error happens
     // check if in the requested body only values for sports were provided
     if (Object.keys(input).length === 1 && !(input.sports === undefined)) {
diff --git a/server/src/controllers/boatType.controllers.ts b/server/src/controllers/boatType.controllers.ts
index a139d9f..953ef22 100644
--- a/server/src/controllers/boatType.controllers.ts
+++ b/server/src/controllers/boatType.controllers.ts
@@ -144,6 +144,20 @@ const updateBoatTypeById = async (req: Request, res: Response) => {
         .json({ success: false, error: "boatTypeIdNotFound" });
     }
 
+    //check if updated-to-be name of boattype already exists in DB
+    if (!(input.name === undefined)) {
+      const checkIfNameExists = await BoatType.findOne({
+        where: {
+          name: input.name,
+        },
+      });
+      if (!(checkIfNameExists === null)) {
+        return res
+          .status(404)
+          .json({ success: false, error: "boatTypeAlreadyExists" });
+      }
+    }
+
     //try to update
     const updatedBoatType = await BoatType.update(input, {
       where: {
-- 
GitLab