diff --git a/server/src/controllers/boat.controllers.ts b/server/src/controllers/boat.controllers.ts index 9406abdbbedba95572a23e57ba0e95921ebe3c93..1390153c6860bb4fbe1e29c15653d96a123d95f4 100644 --- a/server/src/controllers/boat.controllers.ts +++ b/server/src/controllers/boat.controllers.ts @@ -270,7 +270,8 @@ const updateBoatById = async (req: Request, res: Response) => { .status(404) .json({ success: false, error: listIfNotFound + " sportIdNotFound" }); } - + // help Array in order to print as response the name of the updated new sports to boat + var listOfNames = new Array(); //if sports Array is with valid id's, assign them to boat; create entry (boatid, each id of given sportIds) inf BoatHasSport table for (let i = 0; i < listIfFound.length; i++) { const boatid = givenId; @@ -279,6 +280,9 @@ const updateBoatById = async (req: Request, res: Response) => { //create new entry in boatHasBoatType await BoatHasSport.create(entry); + + const findSportName = await (await Sport.findByPk(listIfFound[i])).name; + listOfNames.push(findSportName); } } @@ -290,34 +294,27 @@ const updateBoatById = async (req: Request, res: Response) => { returning: true, }); - //help Array in order to print as response the name of the updated new sports to boat - var listOfNames = new Array(); - const findBoatInfo = await Boat.findByPk(givenId); - for (let i = 0; i < listIfFound.length; i++) { - const findSportName = await (await Sport.findByPk(listIfFound[i])).name; - listOfNames.push(findSportName); - } - //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 - if (!(updatedBoat === undefined)) { + if (updatedBoat === undefined) { //return after updating return res.status(200).json({ success: true, result: { id: givenId, - name: findBoatInfo.name, - boattype: findBoatInfo.boattype, - status: findBoatInfo.status, - tags: findBoatInfo.tags, - minP: findBoatInfo.minP, - maxP: findBoatInfo.maxP, + name: foundBoat.name, + boattype: foundBoat.boattype, + status: foundBoat.status, + tags: foundBoat.tags, + minP: foundBoat.minP, + maxP: foundBoat.maxP, sports: listOfNames, }, }); } - + //case where in request body attributes from Boat provided + sports which is an attribute in Sport table and connection between Boat and Sport is saved in BoatHasSport table const boatDataAfterUpdate = updatedBoat[1][0]; + console.log("boatDataAfterUpdate ", boatDataAfterUpdate); return res.status(200).json({ success: true, result: {