Newer
Older
import BoatHasSport from "../db/models/BoatHasSport";
import Sport from "../db/models/Sport";
const createSportController = async (req: Request, res: Response) => {
try {
if (!(res.locals.user.role == "coordinator")) {
return res
.status(403)
.json({ success: false, error: "MustBeCoordinator" });
}
//check if Sport already exists in DB to avoid duplicates
const findIfSportExists = await Sport.findOne({
where: { name: newSportInput.name },
});
if (findIfSportExists) {
return res
.status(404)
.json({ success: false, error: "sportAlreadyExists" });
//check if color already assigned to another sport (for the statistics)
if (!(newSportInput.color === undefined)) {
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) {
return res.status(201).json({
} catch (error) {
console.error(error.message);
return res.status(500).json({ success: false, error: "serverError" });
}
};
//show all Sports
const showAllSports = async (req: Request, res: Response) => {
try {
if (!(res.locals.user.role === "coordinator")) {
return res
.status(403)
.json({ success: false, error: "MustBeCoordinator" });
}
const allSports = await Sport.findAll();
return res.status(200).send({
success: true,
result: allSports.map((Sport) => {
return { id: Sport.id, name: Sport.name };
}),
});
} catch (error) {
console.error("server error: ", error.message);
return res.status(500).json({ success: false, error: "serverError" });
}
};
// delete a Sport by using given sport ID
const deleteSportById = async (req: Request, res: Response) => {
try {
//check authority
if (!(res.locals.user.role === "coordinator")) {
return res
.status(403)
.json({ success: false, error: "MustBeCoordinator" });
}
//delete using given id
const givenId = req.params.id;
//first we need to destroy entries where the to-be-deleted sport is assigned to boat in BoatHasSport table, because it violates the foreign constraint
await BoatHasSport.destroy({
where: {
sportid: givenId,
},
});
const sportToDelete = await Sport.destroy({
where: {
id: givenId,
},
});
if (sportToDelete == 0) {
return res
.status(404)
.json({ success: false, error: "sportIdDoesNotExist" });
}
return res.status(204).json({ success: true });
} catch (error) {
console.error("server error: ", error.message);
return res.status(500).json({ success: false, error: "serverError" });
}
};
const updateSportById = async (req: Request, res: Response) => {
try {
//check authority
if (!(res.locals.user.role === "coordinator")) {
return res
.status(403)
.json({ success: false, error: "MustBeCoordinator" });
}
//get new input
const input = req.body;
//return 200 with empty response if no data was given
if (Object.keys(input).length === 0) {
return res
.status(200)
.json({ success: true, result: {}, message: "noInputFound" });
}
//get id
const givenId = req.params.id;
//check if sport can be found using givenId
if (!foundSport) {
return res.status(404).json({ success: false, error: "sportIdNotFound" });
}
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
//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" });
}
}
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
//try to update
const updatedSport = await Sport.update(input, {
where: {
id: givenId,
},
returning: true,
});
//return after updating
const sportDataAfterUpdate = updatedSport[1][0];
return res.status(200).json({
success: true,
result: {
id: sportDataAfterUpdate.id,
name: sportDataAfterUpdate.name,
color: sportDataAfterUpdate.color,
},
});
} catch (error) {
console.error("server error: ", error.message);
return res.status(500).json({ success: false, error: "serverError" });
}
};
//show all sports assigned to boat by given boatid
const showSportByBoatId = 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 boat = await Boat.findByPk(givenId);
if (boat) {
//if boat exists check in BoatHasSport table if there are any sports assigned to Boat
const boatIdInBoatHasSport = await BoatHasSport.findByPk(givenId);
if (!boatIdInBoatHasSport) {
return res
.status(404)
.json({ success: false, error: "noAssignedSportToBoatId" });
const findAllBoatId = await BoatHasSport.findAll({
where: {
boatid: givenId,
//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);
return res.status(200).json({ success: true, result: { sport: list } });
}
return res.status(404).json({ success: false, error: "boatIdNotFound" });
} catch (error) {
console.error("server error: ", error.message);
return res.status(500).json({ success: false, error: "serverError" });
}
};
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,