From 8e666717a8706daf01a4e0b37945cb923cd524a9 Mon Sep 17 00:00:00 2001 From: elit04 <elit04@fu-berlin.de> Date: Sun, 23 Jan 2022 07:18:20 -0500 Subject: [PATCH] sport routes+controllers changes/modifications --- server/src/controllers/sport.controllers.ts | 69 ++++++++++++++++++++- server/src/routes/sport.routes.ts | 9 ++- 2 files changed, 76 insertions(+), 2 deletions(-) diff --git a/server/src/controllers/sport.controllers.ts b/server/src/controllers/sport.controllers.ts index 93e39ba..90e1ce8 100644 --- a/server/src/controllers/sport.controllers.ts +++ b/server/src/controllers/sport.controllers.ts @@ -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; diff --git a/server/src/routes/sport.routes.ts b/server/src/routes/sport.routes.ts index ffc011d..668c45d 100644 --- a/server/src/routes/sport.routes.ts +++ b/server/src/routes/sport.routes.ts @@ -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 -- GitLab