From bb13fb92887e31f8e18405b220e5defd14a3ad43 Mon Sep 17 00:00:00 2001
From: elit04 <elit04@fu-berlin.de>
Date: Mon, 24 Jan 2022 13:58:38 -0500
Subject: [PATCH] check if minP is bigger than maxP then error in update boat
 controller

---
 server/src/controllers/boat.controllers.ts | 22 ++++++++++++++++++++++
 1 file changed, 22 insertions(+)

diff --git a/server/src/controllers/boat.controllers.ts b/server/src/controllers/boat.controllers.ts
index 6423439..e5b8504 100644
--- a/server/src/controllers/boat.controllers.ts
+++ b/server/src/controllers/boat.controllers.ts
@@ -267,6 +267,28 @@ const updateBoatById = async (req: Request, res: Response) => {
       }
     }
 
+    //check if minP is bigger than maxP => error
+    if (!(input.minP === undefined) || !(input.maxP === undefined)) {
+      //compare only when the two values are given (minP AND maxP)
+      if (!(input.minP === undefined) && !(input.maxP === undefined)) {
+        if (input.maxP < input.minP) {
+          return res.status(409).json({
+            success: false,
+            error:
+              "maximumNumberOfSeatsMustBeBiggerOrEqualMinimalNumberOfSeats",
+          });
+        }
+      }
+
+      //if only one entry is given for updated(minP OR maxP) we compare them with the values of minP and maxP already assigned to Boat.
+      if (input.minP > foundBoat.maxP || input.maxP < foundBoat.minP) {
+        return res.status(409).json({
+          success: false,
+          error: "maximumNumberOfSeatsMustBeBiggerOrEqualMinimalNumberOfSeats",
+        });
+      }
+    }
+
     //try to update the attributes, which are in Boat table
     const updatedBoat = await Boat.update(input, {
       where: {
-- 
GitLab