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

sport routes+controllers changes/modifications

parent 32bc4236
No related branches found
No related tags found
No related merge requests found
......@@ -25,6 +25,18 @@ const createSportController = async (req: Request, res: Response) => {
.json({ success: false, error: "sportAlreadyExists" });
}
//check if color already assigned to another sport (for the statistics)
const findIfColorAlreadyAssigned = await Sport.findOne({
where: {
color: newSportInput.color,
},
});
if (findIfColorAlreadyAssigned) {
return res
.status(404)
.json({ success: false, error: "givenSportColorAlreadyExists" });
}
const newSport = await Sport.create(newSportInput);
if (newSport) {
......@@ -33,6 +45,7 @@ const createSportController = async (req: Request, res: Response) => {
result: {
id: newSport.id,
name: newSport.name,
color: newSport.color,
},
});
}
......@@ -91,7 +104,7 @@ const deleteSportById = async (req: Request, res: Response) => {
if (sportToDelete == 0) {
return res
.status(404)
.json({ success: false, error: "accountIdDoesNotExist" });
.json({ success: false, error: "sportIdDoesNotExist" });
}
return res.status(204).json({ success: true });
......@@ -131,6 +144,34 @@ const updateSportById = async (req: Request, res: Response) => {
return res.status(404).json({ success: false, error: "sportIdNotFound" });
}
//check if updated name of the new sport already exists in DB
if (!(input.name === undefined)) {
const findIfNameAlreadyExists = await Sport.findOne({
where: {
name: input.name,
},
});
if (findIfNameAlreadyExists) {
return res
.status(404)
.json({ success: false, error: "givenSportNameAlreadyExists" });
}
}
//check if color already assigned to another sport (for the statistics)
if (!(input.color === undefined)) {
const findIfColorAlreadyAssigned = await Sport.findOne({
where: {
color: input.color,
},
});
if (findIfColorAlreadyAssigned) {
return res
.status(404)
.json({ success: false, error: "givenSportColorAlreadyExists" });
}
}
//try to update
const updatedSport = await Sport.update(input, {
where: {
......@@ -183,6 +224,7 @@ const showSportByBoatId = async (req: Request, res: Response) => {
//define empty array, where we store founded names of sports assigned to boat's id
var list = new Array();
for (let i = 0; i < findAllBoatId.length; i++) {
const found = await Sport.findByPk(findAllBoatId[i].sportid);
list.push(found.name);
......@@ -197,12 +239,37 @@ const showSportByBoatId = async (req: Request, res: Response) => {
}
};
const showSportBySportId = async (req: Request, res: Response) => {
try {
if (!(res.locals.user.role === "coordinator")) {
return res
.status(403)
.json({ success: false, error: "MustBeCoordinator" });
}
const givenId = req.params.id;
const sport = await Sport.findByPk(givenId);
if (sport) {
return res
.status(200)
.json({
success: true,
result: { id: givenId, name: sport.name, color: sport.color },
});
}
return res.status(404).json({ success: false, error: "sportIdNotFound" });
} catch (error) {
console.error("server error: ", error.message);
return res.status(500).json({ success: false, error: "serverError" });
}
};
const sportControllers = {
createSportController,
showAllSports,
deleteSportById,
updateSportById,
showSportByBoatId,
showSportBySportId,
};
export default sportControllers;
......@@ -33,7 +33,7 @@ sportRouter.delete(
//update a sport
sportRouter.patch(
"/api/sport/:id/",
body("name").if(body("color").exists()).not().isEmpty(),
body("name").if(body("name").exists()).not().isEmpty(),
body("color").if(body("color").exists()).not().isEmpty(),
handleValidationResult,
validateToken,
......@@ -47,4 +47,11 @@ sportRouter.get(
sportControllers.showSportByBoatId
);
//show specific sport by sport id
sportRouter.get(
"/api/sport/id/:id/",
validateToken,
sportControllers.showSportBySportId
)
export default sportRouter;
\ No newline at end of file
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