Skip to content
Snippets Groups Projects
Commit a4fa36d5 authored by elit04's avatar elit04
Browse files

create new boat Controller+Route

parent 45d44261
No related branches found
No related tags found
No related merge requests found
......@@ -52,10 +52,40 @@ const deleteBoatById = async (req: Request, res: Response) => {
return res.status(500).json({ success: false, error: "serverError" });
}
};
//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,
showBoatById,
deleteBoatById,
createBoat,
};
export default boatControllers;
import { Router } from "express";
import { body } from "express-validator";
import validateToken from "../middleware/validateToken";
import boatControllers from "../controllers/boatControllers.controllers";
import handleValidationResult from "../middleware/handleValidationResult";
const boatsRouter = Router();
//show all boats
boatsRouter.get("/api/boat/", boatControllers.showAllBoatsController);
//show boat by given id
boatsRouter.get("/api/boat/:id", boatControllers.showBoatById);
//delete a boat
boatsRouter.delete(
"/api/boat/:id",
validateToken,
boatControllers.deleteBoatById
);
//create boat
boatsRouter.post(
"/api/boat/",
body("name").not().isEmpty(),
body("boattype").not().isEmpty(),
body("status").not().isEmpty(),
body("active").not().isEmpty(),
body("tags").not().isEmpty(),
handleValidationResult,
validateToken,
boatControllers.createBoat
);
export default boatsRouter;
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