Something went wrong on our end
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
sport.controllers.ts 5.86 KiB
import { Request, Response } from "express";
import Boat from "../db/models/Boat";
import BoatHasSport from "../db/models/BoatHasSport";
import Sport from "../db/models/Sport";
//create SportController
const createSportController = async (req: Request, res: Response) => {
try {
if (!(res.locals.user.role == "coordinator")) {
return res
.status(403)
.json({ success: false, error: "MustBeCoordinator" });
}
const newSportInput = req.body;
//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" });
}
const newSport = await Sport.create(newSportInput);
if (newSport) {
return res.status(201).json({
success: true,
result: {
id: newSport.id,
name: newSport.name,
},
});
}
} 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: "accountIdDoesNotExist" });
}
return res.status(204).json({ success: true });
} catch (error) {
console.error("server error: ", error.message);
return res.status(500).json({ success: false, error: "serverError" });
}
};
//update sport by given sport id
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
const foundSport= await Sport.findByPk(givenId);
if (!foundSport) {
return res.status(404).json({ success: false, error: "sportIdNotFound" });
}
//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 as response the names of the sports
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 sportControllers = {
createSportController,
showAllSports,
deleteSportById,
updateSportById,
showSportByBoatId
};
export default sportControllers;