-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
293 additions
and
2 deletions.
There are no files selected for viewing
20 changes: 20 additions & 0 deletions
20
backend/typescript/middlewares/validators/behaviourValidators.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { Request, Response, NextFunction } from "express"; | ||
import { | ||
getApiValidationError, | ||
validateArray, | ||
validatePrimitive, | ||
} from "./util"; | ||
|
||
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */ | ||
/* eslint-disable-next-line import/prefer-default-export */ | ||
export const behaviourRequestDtoValidators = async ( | ||
req: Request, | ||
res: Response, | ||
next: NextFunction, | ||
) => { | ||
const { body } = req; | ||
if (!validatePrimitive(body.behaviourName, "string")) { | ||
return res.status(400).send(getApiValidationError("behaviourName", "string")); | ||
} | ||
return next(); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
import { Router } from "express"; | ||
import { isAuthorizedByRole } from "../middlewares/auth"; | ||
import { behaviourRequestDtoValidators } from "../middlewares/validators/behaviourValidators"; | ||
import BehaviourService from "../services/implementations/behaviourService"; | ||
import { | ||
BehaviourResponseDTO, | ||
IBehaviourService, | ||
} from "../services/interfaces/behaviourService"; | ||
import { getErrorMessage } from "../utilities/errorUtils"; | ||
import { sendResponseByMimeType } from "../utilities/responseUtil"; | ||
|
||
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)); | ||
} | ||
}, | ||
); | ||
|
||
/* Get all Behaviours */ | ||
behaviourRouter.get("/", async (req, res) => { | ||
const contentType = req.headers["content-type"]; | ||
try { | ||
const behaviours = await behaviourService.getBehaviours(); | ||
await sendResponseByMimeType<BehaviourResponseDTO>( | ||
res, | ||
200, | ||
contentType, | ||
behaviours, | ||
); | ||
} catch (e: unknown) { | ||
await sendResponseByMimeType(res, 500, contentType, [ | ||
{ | ||
error: getErrorMessage(e), | ||
}, | ||
]); | ||
} | ||
}); | ||
|
||
/* Get Behaviour by id */ | ||
behaviourRouter.get("/:id", async (req, res) => { | ||
const { id } = req.params; | ||
|
||
try { | ||
const behaviour = await behaviourService.getBehaviour(id); | ||
res.status(200).json(behaviour); | ||
} catch (e: unknown) { | ||
res.status(500).send(getErrorMessage(e)); | ||
} | ||
}); | ||
|
||
/* 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)); | ||
} | ||
}, | ||
); | ||
|
||
/* Delete Behaviour by id */ | ||
behaviourRouter.delete("/:id", async (req, res) => { | ||
const { id } = req.params; | ||
|
||
try { | ||
const deletedId = await behaviourService.deleteBehaviour(id); | ||
res.status(200).json({ id: deletedId }); | ||
} catch (e: unknown) { | ||
res.status(500).send(getErrorMessage(e)); | ||
} | ||
}); | ||
|
||
export default behaviourRouter; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
117 changes: 117 additions & 0 deletions
117
backend/typescript/services/implementations/behaviourService.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
import PgBehaviour from "../../models/behaviour.model"; | ||
import { | ||
IBehaviourService, | ||
BehaviourRequestDTO, | ||
BehaviourResponseDTO, | ||
} from "../interfaces/behaviourService"; | ||
import { getErrorMessage } from "../../utilities/errorUtils"; | ||
import logger from "../../utilities/logger"; | ||
|
||
const Logger = logger(__filename); | ||
|
||
class BehaviourService implements IBehaviourService { | ||
/* eslint-disable class-methods-use-this */ | ||
async getBehaviour(id: string): Promise<BehaviourResponseDTO> { | ||
let behaviour: PgBehaviour | null; | ||
try { | ||
behaviour = await PgBehaviour.findByPk(id, { raw: true }); | ||
if (!behaviour) { | ||
throw new Error(`Behaviour id ${id} not found`); | ||
} | ||
} catch (error: unknown) { | ||
Logger.error(`Failed to get behaviour. Reason = ${getErrorMessage(error)}`); | ||
throw error; | ||
} | ||
|
||
return { | ||
id: String(behaviour.id), | ||
behaviourName: behaviour.behaviour_name, | ||
}; | ||
} | ||
|
||
async getBehaviours(): Promise<BehaviourResponseDTO[]> { | ||
try { | ||
const behaviours: Array<PgBehaviour> = await PgBehaviour.findAll({ | ||
raw: true, | ||
}); | ||
return behaviours.map((behaviour) => ({ | ||
id: String(behaviour.id), | ||
behaviourName: behaviour.behaviour_name, | ||
})); | ||
} catch (error: unknown) { | ||
Logger.error( | ||
`Failed to get behaviours. Reason = ${getErrorMessage(error)}`, | ||
); | ||
throw error; | ||
} | ||
} | ||
|
||
async createBehaviour( | ||
behaviour: BehaviourRequestDTO, | ||
): Promise<BehaviourResponseDTO> { | ||
let newBehaviour: PgBehaviour | null; | ||
try { | ||
newBehaviour = await PgBehaviour.create({ | ||
behaviour_name: behaviour.behaviourName, | ||
}); | ||
} catch (error: unknown) { | ||
Logger.error( | ||
`Failed to create behaviour. Reason = ${getErrorMessage(error)}`, | ||
); | ||
throw error; | ||
} | ||
return { | ||
id: String(newBehaviour.id), | ||
behaviourName: newBehaviour?.behaviour_name, | ||
}; | ||
} | ||
|
||
async updateBehaviour( | ||
id: string, | ||
behaviour: BehaviourRequestDTO, | ||
): Promise<BehaviourResponseDTO | null> { | ||
let resultingBehaviour: PgBehaviour | null; | ||
let updateResult: [number, PgBehaviour[]] | null; | ||
try { | ||
updateResult = await PgBehaviour.update( | ||
{ | ||
behaviour_name: behaviour.behaviourName, | ||
}, | ||
{ where: { id }, returning: true }, | ||
); | ||
|
||
if (!updateResult[0]) { | ||
throw new Error(`Behaviour id ${id} not found`); | ||
} | ||
[, [resultingBehaviour]] = updateResult; | ||
} catch (error: unknown) { | ||
Logger.error( | ||
`Failed to update behaviour. Reason = ${getErrorMessage(error)}`, | ||
); | ||
throw error; | ||
} | ||
return { | ||
id: String(resultingBehaviour.id), | ||
behaviourName: resultingBehaviour?.behaviour_name, | ||
}; | ||
} | ||
|
||
async deleteBehaviour(id: string): Promise<string> { | ||
try { | ||
const deleteResult: number | null = await PgBehaviour.destroy({ | ||
where: { id }, | ||
}); | ||
if (!deleteResult) { | ||
throw new Error(`Behaviour id ${id} not found`); | ||
} | ||
return id; | ||
} catch (error: unknown) { | ||
Logger.error( | ||
`Failed to delete behaviour. Reason = ${getErrorMessage(error)}`, | ||
); | ||
throw error; | ||
} | ||
} | ||
} | ||
|
||
export default BehaviourService; |
57 changes: 57 additions & 0 deletions
57
backend/typescript/services/interfaces/behaviourService.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
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<BehaviourResponseDTO>; | ||
|
||
/** | ||
* retrieve all Behaviours | ||
* @param | ||
* @returns returns array of Behaviours | ||
* @throws Error if retrieval fails | ||
*/ | ||
getBehaviours(): Promise<BehaviourResponseDTO[]>; | ||
|
||
/** | ||
* 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<BehaviourResponseDTO>; | ||
|
||
/** | ||
* 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<BehaviourResponseDTO | null>; | ||
|
||
/** | ||
* 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<string>; | ||
} | ||
|