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

better way to return updated data from update boat controller

parent 72753a88
No related branches found
No related tags found
No related merge requests found
......@@ -279,7 +279,7 @@ const updateBoatById = async (req: Request, res: Response) => {
const newSportsArray = input.sports;
if (!(newSportsArray === undefined)) {
//define empty array, where we store founded/not founded names of sports assigned to boat's id
//define empty array, where we store founded/not founded id's of sports assigned to boat's id
var listIfNotFound = new Array();
var listIfFound = new Array();
......@@ -317,8 +317,6 @@ 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;
......@@ -327,12 +325,24 @@ 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);
}
}
//get names of assigned sports to boat for the response message
// help Array in order to print as response the name of the updated + previously added new sports to boat
var listOfNames = new Array();
const findAllSportsAssignedToBoat = await BoatHasSport.findAll({
where: {
boatid: givenId,
},
});
for (let i = 0; i < findAllSportsAssignedToBoat.length; i++) {
const foundedSportName = await Sport.findByPk(
findAllSportsAssignedToBoat[i].sportid
);
listOfNames.push(foundedSportName.name);
}
//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)) {
......
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