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

organisatorisch

parent 183caa79
No related branches found
No related tags found
No related merge requests found
import { Request, Response } from "express";
import BoatType from "../db/models/BoatType";
import Boat from "../db/models/Boat";
//create boat
const createBoat = async (req: Request, res: Response) => {
try {
if (!(res.locals.user.role == "coordinator")) {
return res
.status(403)
.json({ success: false, error: "MustBeCoordinator" });
}
const newBoatInput = req.body;
const boatType = await BoatType.findByPk(newBoatInput.boattype);
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 });
}
} catch (error) {
console.error(error.message);
return res.status(500).json({ success: false, error: "serverError" });
}
};
//show all boats
const showAllBoatsController = async (req: Request, res: Response) => {
try {
......@@ -53,33 +83,6 @@ const deleteBoatById = async (req: Request, res: Response) => {
}
};
//create boat
const createBoat = async (req: Request, res: Response) => {
try {
if (!(res.locals.user.role == "coordinator")) {
return res
.status(403)
.json({ success: false, error: "MustBeCoordinator" });
}
const newBoatInput = req.body;
console.log("input von req.body: ", newBoatInput);
const newBoat = await Boat.create(newBoatInput);
const { id, name, boattype, status, active, tags } = newBoat;
if (newBoat) {
return res
.status(201)
.json({ success: true, result: { id, name, boattype, status, active, tags } });
}
} catch (error) {
console.error(error.message);
return res.status(500).json({ success: false, error: "serverError" });
}
};
const boatControllers = {
showAllBoatsController,
......
......@@ -2,23 +2,6 @@ import { Request, Response } from "express";
import BoatType from "../db/models/BoatType";
import Boat from "../db/models/Boat";
//show all BoatTypes
const showAllBoatTypes = async (req: Request, res: Response) => {
try {
if (!(res.locals.user.role === "coordinator")) {
return res
.status(403)
.json({ success: false, error: "MustBeCoordinator" });
}
const allBoatTypes = await BoatType.findAll();
return res.status(200).send({ success: true, result: allBoatTypes });
} catch (error) {
console.error("server error: ", error.message);
return res.status(500).json({ success: false, error: "serverError" });
}
};
//createBoatTypeController
const createBoatTypeController = async (req: Request, res: Response) => {
try {
......@@ -32,12 +15,15 @@ const createBoatTypeController = async (req: Request, res: Response) => {
const newBoatType = await BoatType.create(newBoatTypeInput);
const { id, name, seats } = newBoatType;
if (newBoatType) {
return res
.status(201)
.json({ success: true, result: { id, name, seats } });
return res.status(201).json({
success: true,
result: {
id: newBoatType.id,
name: newBoatType.name,
seats: newBoatType.seats,
},
});
}
} catch (error) {
console.error(error.message);
......@@ -45,6 +31,27 @@ const createBoatTypeController = async (req: Request, res: Response) => {
}
};
//show all BoatTypes
const showAllBoatTypes = async (req: Request, res: Response) => {
try {
if (!(res.locals.user.role === "coordinator")) {
return res
.status(403)
.json({ success: false, error: "MustBeCoordinator" });
}
const allBoatTypes = await BoatType.findAll();
return res.status(200).send({
success: true,
result: allBoatTypes.map((boattype) => {
return { id: boattype.id, name: boattype.name, seats: boattype.seats };
}),
});
} catch (error) {
console.error("server error: ", error.message);
return res.status(500).json({ success: false, error: "serverError" });
}
};
//delete specific BoatType using given id
const deleteBoatTypeById = async (req: Request, res: Response) => {
try {
......@@ -54,7 +61,7 @@ const deleteBoatTypeById = async (req: Request, res: Response) => {
.json({ success: false, error: "MustBeCoordinator" });
}
const givenId = req.params.id;
const boatToDelete = await BoatType.destroy({
const boatToDelete = await BoatType.destroy({
where: {
id: givenId,
},
......@@ -71,7 +78,6 @@ const deleteBoatTypeById = async (req: Request, res: Response) => {
}
};
//show type of a boat using boat's id
const showBoatTypeById = async (req: Request, res: Response) => {
try {
......@@ -81,11 +87,13 @@ const showBoatTypeById = async (req: Request, res: Response) => {
.json({ success: false, error: "MustBeCoordinator" });
}
const givenId = req.params.id;
const boatid = await Boat.findByPk(givenId);
if (boatid) {
const boattypeid = boatid.boattype;
const boat = await Boat.findByPk(givenId);
if (boat) {
const boattypeid = boat.boattype;
const boatname = await BoatType.findByPk(boattypeid);
return res.status(200).json({ success: true, result: { name:boatname.name } });
return res
.status(200)
.json({ success: true, result: { name: boatname.name } });
}
return res.status(404).json({ success: false, error: "boatIdNotFound" });
} catch (error) {
......@@ -94,11 +102,10 @@ const showBoatTypeById = async (req: Request, res: Response) => {
}
};
const boatTypeControllers = {
showAllBoatTypes,
createBoatTypeController,
deleteBoatTypeById,
deleteBoatTypeById,
showBoatTypeById,
};
......
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