diff --git a/server/src/controllers/boat.controllers.ts b/server/src/controllers/boat.controllers.ts index 01063672bffc30c3da7e79a53d4f221332b15505..fd0df6819e423a56d71580b504f2f6bd1949a363 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 a139d9fa7ceaa35826ca29a402ecae3eea1a1729..953ef22513a176281f959d9e302f6b64124ebd36 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: {