Skip to content
Snippets Groups Projects
Commit 73667db9 authored by alrwasheda's avatar alrwasheda :speech_balloon:
Browse files

Rückgabe den API-Endpunkten angepasst

parent 510aeb02
No related branches found
No related tags found
No related merge requests found
......@@ -56,7 +56,18 @@ const showAllAccounts = async (req: Request, res: Response) => {
.json({ success: false, error: "MustBeCoordinator" });
}
const allAccounts = await Employee.findAll();
return res.status(200).send({ success: true, result: allAccounts });
return res.status(200).send({
success: true,
result: allAccounts.map((account) => {
return {
id: account.id,
first_name: account.first_name,
last_name: account.last_name,
email: account.email,
role: account.role,
};
}),
});
} catch (error) {
console.error("server error: ", error.message);
return res.status(500).json({ success: false, error: "serverError" });
......@@ -74,7 +85,16 @@ const showAccountById = async (req: Request, res: Response) => {
const givenId = req.params.id;
const account = await Employee.findByPk(givenId);
if (account) {
return res.status(200).json({ success: true, result: account });
return res.status(200).json({
success: true,
result: {
id: account.id,
first_name: account.first_name,
last_name: account.last_name,
email: account.email,
role: account.role,
},
});
}
return res.status(404).json({ success: false, error: "accountIdNotFound" });
} catch (error) {
......@@ -121,7 +141,16 @@ const updateAccount = async (req: Request, res: Response) => {
const userData = userDataAfterUpdate[1][0];
delete userData.password;
return res.status(200).json({ success: true, result: userData });
return res.status(200).json({
success: true,
result: {
id: userData.id,
first_name: userData.first_name,
last_name: userData.last_name,
email: userData.email,
role: userData.role,
},
});
} catch (error) {
console.error("server error: ", error.message);
return res.status(500).json({ success: false, error: "serverError" });
......
......@@ -14,15 +14,25 @@ const createBoat = async (req: Request, res: Response) => {
const newBoatInput = req.body;
const boatType = await BoatType.findByPk(newBoatInput.boattype);
if(!boatType){
return res.status(404).json({success: false, error: "boattypeNotFound"})
if (!boatType) {
return res
.status(404)
.json({ success: false, error: "boattypeNotFound" });
}
const newBoat = await Boat.create(newBoatInput);
if (newBoat) {
return res
.status(201)
.json({ success: true, result: newBoat });
return res.status(201).json({
success: true,
result: {
id: newBoat.id,
name: newBoat.name,
boatTypeId: newBoat.boattype,
status: newBoat.status,
active: newBoat.active,
tags: newBoat.tags,
},
});
}
} catch (error) {
console.error(error.message);
......@@ -30,12 +40,23 @@ const createBoat = async (req: Request, res: Response) => {
}
};
//show all boats
const showAllBoatsController = async (req: Request, res: Response) => {
try {
const allBoats = await Boat.findAll();
return res.status(200).send({ success: true, result: allBoats });
return res.status(200).send({
success: true,
result: allBoats.map((boat) => {
return {
id: boat.id,
name: boat.name,
boatType: boat.boattype,
status: boat.status,
active: boat.active,
tags: boat.tags,
};
}),
});
} catch (error) {
console.error("server error: ", error.message);
return res.status(500).json({ success: false, error: "serverError" });
......@@ -48,7 +69,16 @@ const showBoatById = async (req: Request, res: Response) => {
const givenId = req.params.id;
const boat = await Boat.findByPk(givenId);
if (boat) {
return res.status(200).json({ success: true, result: boat });
return res.status(200).json({
success: true,
result: {
name: boat.name,
boatTypeId: boat.boattype,
status: boat.status,
active: boat.active,
tags: boat.tags,
},
});
}
return res.status(404).json({ success: false, error: "boatIdNotFound" });
} catch (error) {
......@@ -66,7 +96,7 @@ const deleteBoatById = async (req: Request, res: Response) => {
.json({ success: false, error: "MustBeCoordinator" });
}
const givenId = req.params.id;
const boatToDelete = await Boat.destroy({
const boatToDelete = await Boat.destroy({
where: {
id: givenId,
},
......@@ -83,7 +113,6 @@ const deleteBoatById = async (req: Request, res: Response) => {
}
};
const boatControllers = {
showAllBoatsController,
showBoatById,
......
......@@ -43,7 +43,7 @@ const showAllBoatTypes = async (req: Request, res: Response) => {
return res.status(200).send({
success: true,
result: allBoatTypes.map((boattype) => {
return { id: boattype.id, name: boattype.name, seats: boattype.seats };
return { name: boattype.name, seats: boattype.seats };
}),
});
} catch (error) {
......@@ -78,7 +78,7 @@ const deleteBoatTypeById = async (req: Request, res: Response) => {
}
};
//show type of a boat using boat's id
//show boatType using boat's id
const showBoatTypeById = async (req: Request, res: Response) => {
try {
if (!(res.locals.user.role === "coordinator")) {
......
......@@ -12,14 +12,25 @@ const showCurrentUserController = async (req: Request, res: Response) => {
},
});
res.status(200).json({ success: true, result: currentUserData });
res
.status(200)
.json({
success: true,
result: {
id: currentUserData.id,
first_name: currentUserData.first_name,
last_name: currentUserData.last_name,
email: currentUserData.email,
role: currentUserData.role,
},
});
} catch (error) {
console.log("server error: ", error.message);
res.status(500).json({ success: false, error: "serverError!" });
}
};
//update current user PATCH
//update current user PATCH
const updateCurrentUser = async (req: Request, res: Response) => {
try {
//check role
......
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