Skip to content

Commit

Permalink
Fixed formatting errors
Browse files Browse the repository at this point in the history
  • Loading branch information
DanZfsd committed Jun 22, 2024
1 parent eef616c commit ea28d6a
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 98 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
};
58 changes: 24 additions & 34 deletions backend/typescript/rest/behaviourRoutes.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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) => {
Expand Down Expand Up @@ -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) => {
Expand Down
2 changes: 1 addition & 1 deletion backend/typescript/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
14 changes: 8 additions & 6 deletions backend/typescript/services/implementations/behaviourService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
}

Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -62,7 +64,7 @@ class BehaviourService implements IBehaviourService {
}
return {
id: String(newBehaviour.id),
behaviourName: newBehaviour?.behaviour_name,
behaviourName: newBehaviour?.behaviour_name,
};
}

Expand Down Expand Up @@ -92,7 +94,7 @@ class BehaviourService implements IBehaviourService {
}
return {
id: String(resultingBehaviour.id),
behaviourName: resultingBehaviour?.behaviour_name,
behaviourName: resultingBehaviour?.behaviour_name,
};
}

Expand Down
111 changes: 55 additions & 56 deletions backend/typescript/services/interfaces/behaviourService.ts
Original file line number Diff line number Diff line change
@@ -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<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>;
}

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>;
}

0 comments on commit ea28d6a

Please sign in to comment.