diff --git a/backend/typescript/middlewares/validators/behaviourValidators.ts b/backend/typescript/middlewares/validators/behaviourValidators.ts index 912e0d5..f9f101b 100644 --- a/backend/typescript/middlewares/validators/behaviourValidators.ts +++ b/backend/typescript/middlewares/validators/behaviourValidators.ts @@ -14,7 +14,9 @@ export const behaviourRequestDtoValidators = async ( ) => { const { body } = req; if (!validatePrimitive(body.behaviourName, "string")) { - return res.status(400).send(getApiValidationError("behaviourName", "string")); + return res + .status(400) + .send(getApiValidationError("behaviourName", "string")); } return next(); }; diff --git a/backend/typescript/rest/behaviourRoutes.ts b/backend/typescript/rest/behaviourRoutes.ts index a51cbad..198e747 100644 --- a/backend/typescript/rest/behaviourRoutes.ts +++ b/backend/typescript/rest/behaviourRoutes.ts @@ -1,5 +1,4 @@ import { Router } from "express"; -import { isAuthorizedByRole } from "../middlewares/auth"; import { behaviourRequestDtoValidators } from "../middlewares/validators/behaviourValidators"; import BehaviourService from "../services/implementations/behaviourService"; import { @@ -9,26 +8,21 @@ import { import { getErrorMessage } from "../utilities/errorUtils"; import { sendResponseByMimeType } from "../utilities/responseUtil"; -const behaviourRouter: Router = Router(); - +const behaviourRouter: Router = Router(); const behaviourService: IBehaviourService = new BehaviourService(); /* Create Behaviour */ -behaviourRouter.post( - "/", - behaviourRequestDtoValidators, - async (req, res) => { - try { - const { body } = req; - const newBehaviour = await behaviourService.createBehaviour({ - behaviourName: body.behaviourName, - }); - res.status(201).json(newBehaviour); - } catch (e: unknown) { - res.status(500).send(getErrorMessage(e)); - } - }, -); +behaviourRouter.post("/", behaviourRequestDtoValidators, async (req, res) => { + try { + const { body } = req; + const newBehaviour = await behaviourService.createBehaviour({ + behaviourName: body.behaviourName, + }); + res.status(201).json(newBehaviour); + } catch (e: unknown) { + res.status(500).send(getErrorMessage(e)); + } +}); /* Get all Behaviours */ behaviourRouter.get("/", async (req, res) => { @@ -63,22 +57,18 @@ behaviourRouter.get("/:id", async (req, res) => { }); /* Update Behaviour by id */ -behaviourRouter.put( - "/:id", - behaviourRequestDtoValidators, - async (req, res) => { - const { id } = req.params; - try { - const { body } = req; - const behaviour = await behaviourService.updateBehaviour(id, { - behaviourName: body.behaviourName, - }); - res.status(200).json(behaviour); - } catch (e: unknown) { - res.status(500).send(getErrorMessage(e)); - } - }, -); +behaviourRouter.put("/:id", behaviourRequestDtoValidators, async (req, res) => { + const { id } = req.params; + try { + const { body } = req; + const behaviour = await behaviourService.updateBehaviour(id, { + behaviourName: body.behaviourName, + }); + res.status(200).json(behaviour); + } catch (e: unknown) { + res.status(500).send(getErrorMessage(e)); + } +}); /* Delete Behaviour by id */ behaviourRouter.delete("/:id", async (req, res) => { diff --git a/backend/typescript/server.ts b/backend/typescript/server.ts index 4938045..338bfd3 100644 --- a/backend/typescript/server.ts +++ b/backend/typescript/server.ts @@ -10,7 +10,7 @@ import authRouter from "./rest/authRoutes"; import behaviourRouter from "./rest/behaviourRoutes"; import entityRouter from "./rest/entityRoutes"; import simpleEntityRouter from "./rest/simpleEntityRoutes"; -import userRouter from "./rest/userRoutes"; +import userRouter from "./rest/userRoutes"; const CORS_ALLOW_LIST = [ "http://localhost:3000", diff --git a/backend/typescript/services/implementations/behaviourService.ts b/backend/typescript/services/implementations/behaviourService.ts index 41010f0..cda7b77 100644 --- a/backend/typescript/services/implementations/behaviourService.ts +++ b/backend/typescript/services/implementations/behaviourService.ts @@ -19,13 +19,15 @@ class BehaviourService implements IBehaviourService { throw new Error(`Behaviour id ${id} not found`); } } catch (error: unknown) { - Logger.error(`Failed to get behaviour. Reason = ${getErrorMessage(error)}`); + Logger.error( + `Failed to get behaviour. Reason = ${getErrorMessage(error)}`, + ); throw error; } return { id: String(behaviour.id), - behaviourName: behaviour.behaviour_name, + behaviourName: behaviour.behaviour_name, }; } @@ -36,7 +38,7 @@ class BehaviourService implements IBehaviourService { }); return behaviours.map((behaviour) => ({ id: String(behaviour.id), - behaviourName: behaviour.behaviour_name, + behaviourName: behaviour.behaviour_name, })); } catch (error: unknown) { Logger.error( @@ -52,7 +54,7 @@ class BehaviourService implements IBehaviourService { let newBehaviour: PgBehaviour | null; try { newBehaviour = await PgBehaviour.create({ - behaviour_name: behaviour.behaviourName, + behaviour_name: behaviour.behaviourName, }); } catch (error: unknown) { Logger.error( @@ -62,7 +64,7 @@ class BehaviourService implements IBehaviourService { } return { id: String(newBehaviour.id), - behaviourName: newBehaviour?.behaviour_name, + behaviourName: newBehaviour?.behaviour_name, }; } @@ -92,7 +94,7 @@ class BehaviourService implements IBehaviourService { } return { id: String(resultingBehaviour.id), - behaviourName: resultingBehaviour?.behaviour_name, + behaviourName: resultingBehaviour?.behaviour_name, }; } diff --git a/backend/typescript/services/interfaces/behaviourService.ts b/backend/typescript/services/interfaces/behaviourService.ts index f93bdf5..647aa54 100644 --- a/backend/typescript/services/interfaces/behaviourService.ts +++ b/backend/typescript/services/interfaces/behaviourService.ts @@ -1,57 +1,56 @@ export interface BehaviourRequestDTO { - behaviourName: string; - } - - export interface BehaviourResponseDTO { - id: string; - behaviourName: string; - } - - export interface IBehaviourService { - /** - * retrieve the Behaviour with the given id - * @param id Behaviour id - * @returns requested Behaviour - * @throws Error if retrieval fails - */ - getBehaviour(id: string): Promise; - - /** - * retrieve all Behaviours - * @param - * @returns returns array of Behaviours - * @throws Error if retrieval fails - */ - getBehaviours(): Promise; - - /** - * create a Behaviour with the fields given in the DTO, return created Behaviour - * @param behaviour new Behaviour - * @returns the created Behaviour - * @throws Error if creation fails - */ - createBehaviour( - behaviour: BehaviourRequestDTO, - ): Promise; - - /** - * update the Behaviour with the given id with fields in the DTO, return updated Behaviour - * @param id Behaviour id - * @param behaviour Updated Behaviour - * @returns the updated Behaviour - * @throws Error if update fails - */ - updateBehaviour( - id: string, - behaviour: BehaviourRequestDTO, - ): Promise; - - /** - * delete the Behaviour with the given id - * @param id Behaviour id - * @returns id of the Behaviour deleted - * @throws Error if deletion fails - */ - deleteBehaviour(id: string): Promise; - } - \ No newline at end of file + behaviourName: string; +} + +export interface BehaviourResponseDTO { + id: string; + behaviourName: string; +} + +export interface IBehaviourService { + /** + * retrieve the Behaviour with the given id + * @param id Behaviour id + * @returns requested Behaviour + * @throws Error if retrieval fails + */ + getBehaviour(id: string): Promise; + + /** + * retrieve all Behaviours + * @param + * @returns returns array of Behaviours + * @throws Error if retrieval fails + */ + getBehaviours(): Promise; + + /** + * create a Behaviour with the fields given in the DTO, return created Behaviour + * @param behaviour new Behaviour + * @returns the created Behaviour + * @throws Error if creation fails + */ + createBehaviour( + behaviour: BehaviourRequestDTO, + ): Promise; + + /** + * update the Behaviour with the given id with fields in the DTO, return updated Behaviour + * @param id Behaviour id + * @param behaviour Updated Behaviour + * @returns the updated Behaviour + * @throws Error if update fails + */ + updateBehaviour( + id: string, + behaviour: BehaviourRequestDTO, + ): Promise; + + /** + * delete the Behaviour with the given id + * @param id Behaviour id + * @returns id of the Behaviour deleted + * @throws Error if deletion fails + */ + deleteBehaviour(id: string): Promise; +}