Skip to content
Snippets Groups Projects
Commit 304706c1 authored by elit04's avatar elit04
Browse files

small modification in update boat controller

parent 8e666717
No related branches found
No related tags found
No related merge requests found
......@@ -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: {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment