import express, { NextFunction, Request, Response } from "express"; import VacationModal from "../4-models/vacation-model"; import logic from "../5-logic/vacations-logic"; import path from "path"; import fs from "fs"; import { RouteNotFoundError } from "../4-models/errors-model"; import verifyAdmin from "../3-middleware/verify-admin"; import verifyLoggedIn from "../3-middleware/verify-logged-in"; const router = express.Router(); // http://localhost:3000/api/vacations router.get("/vacations",verifyLoggedIn ,async (request: Request, response: Response, next: NextFunction) => { try { const vacations = await logic.getAllVacations(); response.json(vacations); } catch (err: any) { next(err); } }) // http://localhost:3000/api/vacations/2 router.get("/vacations/:id([0-9]+)", async (request: Request, response: Response, next: NextFunction) => { try { const id = +request.params.id; const vacation = await logic.getOneVacationById(id); response.json(vacation); } catch (err: any) { next(err); } }) // http://localhost:3000/api/vacations router.post("/vacations", [verifyAdmin],async (request: Request, response: Response, next: NextFunction) => { try { request.body.image = request.files.image; const vacation = new VacationModal(request.body); const addedVacation = await logic.addVacation(vacation); response.status(201).json(addedVacation); } catch (err: any) { next(err); } }) // http://localhost:3000/api/vacations/2 router.put("/vacations/:id([0-9]+)", verifyAdmin, async (request: Request, response: Response, next: NextFunction) => { try { request.body.id = +request.params.id; request.body.image = request.files?.image; const vacation = new VacationModal(request.body); const updatedVacation = await logic.updateFullVacationDetails(vacation); response.json(updatedVacation); } catch (err: any) { next(err); } }) // http://localhost:3000/api/vacations/2 router.patch("/vacations/:id([0-9]+)", verifyAdmin, async (request: Request, response: Response, next: NextFunction) => { try { request.body.id = +request.params.id; request.body.image = request.files?.image; const vacation = new VacationModal(request.body); const updatedVacation = await logic.updatePartialVacationDetails(vacation); response.json(updatedVacation); } catch (err: any) { next(err); } }) // http://localhost:3000/api/vacations/2 router.delete("/vacations/:id([0-9]+)", verifyAdmin, async (request: Request, response: Response, next: NextFunction) => { try { const id = +request.params.id; await logic.deleteVacation(id); response.sendStatus(204); } catch (err: any) { next(err); } }) router.get("/vacations/images/:imageName", async (request: Request, response: Response, next: NextFunction) => { try { const imageName = request.params.imageName; const absolutePath = path.join(__dirname, "..", "1-assets", "images", imageName); if(!fs.existsSync(absolutePath)) { throw new RouteNotFoundError(request.method, request.originalUrl) } response.sendFile(absolutePath) } catch (err: any) { next(err); } }) export default router;