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

create/update boat controllers changed according new Sport and BoatHasSport tables

parent fbe8ea2a
No related branches found
No related tags found
No related merge requests found
......@@ -15,6 +15,7 @@ const createBoat = async (req: Request, res: Response) => {
const newBoatInput = req.body;
//check if boattype exists
const boatType = await BoatType.findByPk(newBoatInput.boattype);
if (!boatType) {
return res
......@@ -22,29 +23,61 @@ const createBoat = async (req: Request, res: Response) => {
.json({ success: false, error: "boattypeNotFound" });
}
//sport array of ids
//check if given sport-ids can be found
const sportsArray = newBoatInput.sports;
//check wether each given id exists or not
for (let i = 0; i < sportsArray.length; i++) {
const found = await Sport.findByPk(sportsArray[i]);
if (!found)
return res
.status(404)
.json({ success: false, error: sportsArray[i] + " notFound" });
var check = true;
//if sports provided in the request body
if (!(sportsArray === undefined)) {
//define empty array, where we store founded/not founded names of sports assigned to boat's id
var listIfNotFound = new Array();
var listIfFound = new Array();
//check wether each new given id exists or not
for (let i = 0; i < sportsArray.length; i++) {
const found = await Sport.findByPk(sportsArray[i]);
if (!found) {
listIfNotFound.push(sportsArray[i]);
var check = false;
}
if (found) {
const showSportName = await (
await Sport.findByPk(sportsArray[i])
).name;
listIfFound.push(showSportName);
}
}
}
const newBoat = await Boat.create(newBoatInput); // create the boat
//if there is a sport, which isn't in the database, we return an array with the names of the missing sports
if (check === false) {
console.log("list", listIfNotFound);
return res.status(404).json({
success: false,
error: "The SportIDs: " + listIfNotFound + " cannotBeFound",
});
}
//create the boat
const newBoat = await Boat.create(newBoatInput);
const boatid = newBoat.id;
//another check if sports array was provided and it's with valid sport ids => create entry in BoatHasSport table with the new created boat id and corresponding sport is
if (!(sportsArray == undefined)) {
if (listIfFound.length === sportsArray.length) {
const boatid = newBoat.id;
//create entry (boatid, each id of given sportIds)
for (let i = 0; i < sportsArray.length; i++) {
const sportid = sportsArray[i];
const entry = { boatid, sportid };
//create new entry in boatHasBoatType
await BoatHasSport.create(entry);
//create entry (boatid, each id of given sportIds)
for (let i = 0; i < sportsArray.length; i++) {
const sportid = sportsArray[i];
const entry = { boatid, sportid };
//create new entry in boatHasBoatType
await BoatHasSport.create(entry);
}
}
}
//return created boat + the assigned sports
if (newBoat) {
return res.status(201).json({
success: true,
......@@ -56,7 +89,7 @@ const createBoat = async (req: Request, res: Response) => {
tags: newBoat.tags,
minP: newBoat.minP,
maxP: newBoat.maxP,
sports: newBoat.sports,
sports: listIfFound,
},
});
}
......@@ -81,7 +114,6 @@ const showAllBoatsController = async (req: Request, res: Response) => {
tags: boat.tags,
minP: boat.minP,
maxP: boat.maxP,
sports: boat.sports,
};
}),
});
......@@ -107,7 +139,6 @@ const showBoatById = async (req: Request, res: Response) => {
tags: boat.tags,
minP: boat.minP,
maxP: boat.maxP,
sports: boat.sports,
},
});
}
......@@ -127,6 +158,14 @@ const deleteBoatById = async (req: Request, res: Response) => {
.json({ success: false, error: "MustBeCoordinator" });
}
const givenId = req.params.id;
//first we need to destroy entries where the to-be-deleted boat is connected with sport in BoatHasSport table, because it violates the foreign constraint
await BoatHasSport.destroy({
where: {
boatid: givenId,
},
});
const boatToDelete = await Boat.destroy({
where: {
id: givenId,
......@@ -174,25 +213,76 @@ const updateBoatById = async (req: Request, res: Response) => {
return res.status(404).json({ success: false, error: "boatIdNotFound" });
}
//check if new boattype can be found
if (!(await BoatType.findByPk(input.boattype))) {
return res
.status(404)
.json({ success: false, error: "givenBoatTypeIdNotFound" });
const givenBoatType = input.boattype;
//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)) {
//check if new boattype can be found
const checkBoatTypeId = await BoatType.findByPk(input.boattype);
if (!checkBoatTypeId) {
return res
.status(404)
.json({ success: false, error: "givenBoatTypeIdNotFound" });
}
}
//check if new given sport-ids can be found
const newSportsArray = input.sports;
//check wether each new given id exists or not
for (let i = 0; i < newSportsArray.length; i++) {
const found = await Sport.findByPk(newSportsArray[i]);
if (!found)
if (!(newSportsArray === undefined)) {
//define empty array, where we store founded/not founded names of sports assigned to boat's id
var listIfNotFound = new Array();
var listIfFound = new Array();
//help variable in order to return the Array with the sport ids, which weren't founded
var check = true;
//check wether each new given id exists or not
for (let i = 0; i < newSportsArray.length; i++) {
const found = await Sport.findByPk(newSportsArray[i]);
// if not founded, push id's into listIfNotFound Array
if (!found) {
listIfNotFound.push(newSportsArray[i]);
var check = false;
}
if (found) {
//check if sport already assigned to boat, if yes continue, no need for error message
const getBoatIdInBoatHasSport = await BoatHasSport.findOne({
where: {
boatid: givenId,
sportid: newSportsArray[i],
},
});
//if boat is not already assigned to existing sport id => put id into listIfFound
if (getBoatIdInBoatHasSport === null) {
listIfFound.push(newSportsArray[i]);
}
}
}
//if the updated-to-be sportid(sportids) doesn't exist
if (check === false) {
console.log("list", listIfNotFound);
return res
.status(404)
.json({ success: false, error: newSportsArray[i] + " notFound" });
.json({ success: false, error: listIfNotFound + " sportIdNotFound" });
}
//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;
const sportid = listIfFound[i];
const entry = { boatid, sportid };
//create new entry in boatHasBoatType
await BoatHasSport.create(entry);
}
}
//try to update
//try to update the attributes, which are in Boat table
const updatedBoat = await Boat.update(input, {
where: {
id: givenId,
......@@ -200,7 +290,33 @@ const updateBoatById = async (req: Request, res: Response) => {
returning: true,
});
//return after updating
//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)) {
//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,
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];
return res.status(200).json({
success: true,
......@@ -212,7 +328,7 @@ const updateBoatById = async (req: Request, res: Response) => {
tags: boatDataAfterUpdate.tags,
minP: boatDataAfterUpdate.minP,
maxP: boatDataAfterUpdate.maxP,
sports: boatDataAfterUpdate.sports,
sports: listOfNames,
},
});
} catch (error) {
......
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