From 2ba1a654d8b87defe31cacdb23143b88623ab578 Mon Sep 17 00:00:00 2001 From: Hanen Alrwasheda <alrwasheda@mi.fu-berlin.de> Date: Sun, 16 Jan 2022 04:05:18 +0100 Subject: [PATCH] exporting controller directly + some fixes --- .../createLogEntryControllers.controllers.ts | 68 ++++++++----------- .../src/routes/createLogEntryRoutes.routes.ts | 26 +++---- 2 files changed, 43 insertions(+), 51 deletions(-) diff --git a/server/src/controllers/createLogEntryControllers.controllers.ts b/server/src/controllers/createLogEntryControllers.controllers.ts index dc45b89..0314d4f 100644 --- a/server/src/controllers/createLogEntryControllers.controllers.ts +++ b/server/src/controllers/createLogEntryControllers.controllers.ts @@ -3,44 +3,36 @@ import CheckIn from "../db/models/CheckIn"; import Boat from "../db/models/Boat"; //create log entry -const createLogEntry = async (req: Request, res: Response) => { +const createLogEntryController = async (req: Request, res: Response) => { + try { + const logEntry = req.body; + const boatid = await Boat.findByPk(logEntry.boatID); - try{ - - const logEntry = req.body; - const boatid = Boat.findByPk(logEntry.boatID); - - //get status of boat - const bootAvailability = (await boatid).status; - - //if boat is not available => it's locked - if(!(bootAvailability === true)) { - return res.status(409).json({ success: false, error: "BoatLocked" }); - }; - - const newLogEntry = await CheckIn.create(logEntry); - - return res.status(201).json({ - success: true, - account: { - startTime: newLogEntry.startTime, - estimatedEndTime: newLogEntry.estimatedEndTime, - email: newLogEntry.email, - firstName: newLogEntry.firstName, - lastName: newLogEntry.lastName, - additionalClients: newLogEntry.additionalClients, - boatID: newLogEntry.boatID, - }, - }); - } catch (error) { - console.error(error.message); - return res.status(500).json({ success: false, error: "serverError" }); + //get status of boat + const bootAvailability = boatid.status; + + //if boat is not available => it's locked + if (bootAvailability === false) { + return res.status(409).json({ success: false, error: "BoatLocked" }); } + + const newLogEntry = await CheckIn.create(logEntry); + + return res.status(201).json({ + success: true, + result: { + startTime: newLogEntry.startTime, + estimatedEndTime: newLogEntry.estimatedEndTime, + email: newLogEntry.email, + firstName: newLogEntry.firstName, + lastName: newLogEntry.lastName, + additionalClients: newLogEntry.additionalClients, + boatID: newLogEntry.boatID, + }, + }); + } catch (error) { + console.error(error.message); + return res.status(500).json({ success: false, error: "serverError" }); + } }; - -const createLogEntryControllers = { - createLogEntry, -}; - - -export default createLogEntryControllers; \ No newline at end of file +export default createLogEntryController; diff --git a/server/src/routes/createLogEntryRoutes.routes.ts b/server/src/routes/createLogEntryRoutes.routes.ts index 30dae54..55e7fa5 100644 --- a/server/src/routes/createLogEntryRoutes.routes.ts +++ b/server/src/routes/createLogEntryRoutes.routes.ts @@ -1,22 +1,22 @@ import { Router } from "express"; import { body } from "express-validator"; import handleValidationResult from "../middleware/handleValidationResult"; -import createLogEntryControllers from "../controllers/createLogEntryControllers.controllers"; +import createLogEntryController from "../controllers/createLogEntryControllers.controllers"; const entryRouter = Router(); //create new log entry route entryRouter.post( - "/api/logentry/", - //isISO8601() checks if string is a valid date+time - body("startTime").isISO8601(), - body("estimatedEndTime").isISO8601(), - body("email").isEmail().normalizeEmail(), - body("firstName").not().isEmpty(), - body("lastName").not().isEmpty(), - body("boatID").isUUID(), - handleValidationResult, - createLogEntryControllers.createLogEntry - ); + "/api/logentry/", + //isISO8601() checks if string is a valid date+time + body("startTime").isISO8601(), + body("estimatedEndTime").isISO8601(), + body("email").isEmail().normalizeEmail(), + body("firstName").not().isEmpty(), + body("lastName").not().isEmpty(), + body("boatID").isUUID(), + handleValidationResult, + createLogEntryController +); - export default entryRouter; \ No newline at end of file +export default entryRouter; -- GitLab