From 219ebe5a83f233a1664703e4d612b06c0fdd404d Mon Sep 17 00:00:00 2001 From: OxSaitama Date: Mon, 19 Jun 2023 18:33:28 +0200 Subject: [PATCH] add services & controllers --- src/app/api/super-admin/RolesController.ts | 145 ++++++++++++++++++ src/app/api/super-admin/RulesController.ts | 145 ++++++++++++++++++ .../super-admin/RolesService/RolesService.ts | 45 ++++++ .../super-admin/RulesService/RulesService.ts | 45 ++++++ 4 files changed, 380 insertions(+) create mode 100644 src/app/api/super-admin/RolesController.ts create mode 100644 src/app/api/super-admin/RulesController.ts create mode 100644 src/services/super-admin/RolesService/RolesService.ts create mode 100644 src/services/super-admin/RulesService/RulesService.ts diff --git a/src/app/api/super-admin/RolesController.ts b/src/app/api/super-admin/RolesController.ts new file mode 100644 index 00000000..487511a2 --- /dev/null +++ b/src/app/api/super-admin/RolesController.ts @@ -0,0 +1,145 @@ +import { Response, Request } from "express"; +import { Controller, Get, Post, Put } from "@ControllerPattern/index"; +import ApiController from "@Common/system/controller-pattern/ApiController"; +import RulesService from "@Services/super-admin/RulesService/RulesService"; +import { Service } from "typedi"; +import { validateOrReject } from "class-validator"; +import { Rule } from "le-coffre-resources/dist/Notary"; +import { Rules } from "@prisma/client"; + +@Controller() +@Service() +export default class RulesController extends ApiController { + constructor(private rolesService: RulesService) { + super(); + } + + /** + * @description Get all roles + */ + @Get("/api/v1/super-admin/roles") + protected async get(req: Request, response: Response) { + try { + //get query + const query = JSON.parse(req.query["q"] as string); + + //call service to get prisma entity + const rolesEntities = await this.rolesService.get(query); + + //Hydrate ressource with prisma entity + const roles = Rule.hydrateArray(rolesEntities, { strategy: "excludeAll" }); + + //success + this.httpSuccess(response, roles); + } catch (error) { + this.httpInternalError(response, error); + return; + } + } + + /** + * @description Create a new role + */ + @Post("/api/v1/super-admin/roles") + protected async getAddresses(req: Request, response: Response) { + try { + //init IRule resource with request body values + const roleEntity = Rule.hydrate(req.body); + + //validate role + await validateOrReject(roleEntity, { groups: ["createRule"] }); + + //call service to get prisma entity + const roleEntityCreated = await this.rolesService.create(roleEntity); + + //Hydrate ressource with prisma entity + const role = Rule.hydrate(roleEntityCreated, { + strategy: "excludeAll", + }); + + //success + this.httpCreated(response, role); + } catch (error) { + this.httpInternalError(response, error); + return; + } + } + + /** + * @description Modify a specific role by uid + */ + @Put("/api/v1/super-admin/roles/:uid") + protected async put(req: Request, response: Response) { + try { + const uid = req.params["uid"]; + if (!uid) { + this.httpBadRequest(response, "No uid provided"); + return; + } + + const roleFound = await this.rolesService.getByUid(uid); + + if (!roleFound) { + this.httpNotFoundRequest(response, "role not found"); + return; + } + + //init IRule resource with request body values + const roleEntity = Rule.hydrate(req.body); + + //validate role + await validateOrReject(roleEntity, { groups: ["update"] }); + + //call service to get prisma entity + const roleEntityUpdated = await this.rolesService.update(roleEntity); + + //Hydrate ressource with prisma entity + const role = Rule.hydrate(roleEntityUpdated, { + strategy: "excludeAll", + }); + + //success + this.httpSuccess(response, role); + } catch (error) { + this.httpInternalError(response, error); + return; + } + } + + /** + * @description Get a specific role by uid + */ + @Get("/api/v1/super-admin/roles/:uid") + protected async getOneByUid(req: Request, response: Response) { + try { + const uid = req.params["uid"]; + if (!uid) { + this.httpBadRequest(response, "No uid provided"); + return; + } + let roleEntity: Rules | null; + //get query + if (req.query["q"]) { + const query = JSON.parse(req.query["q"] as string); + roleEntity = await this.rolesService.getByUid(uid, query); + } else { + //call service to get prisma entity + roleEntity = await this.rolesService.getByUid(uid); + } + + if (!roleEntity) { + this.httpNotFoundRequest(response, "role not found"); + return; + } + + //Hydrate ressource with prisma entity + const role = Rule.hydrate(roleEntity, { strategy: "excludeAll" }); + + //success + this.httpSuccess(response, role); + } catch (error) { + this.httpInternalError(response, error); + return; + } + } +} diff --git a/src/app/api/super-admin/RulesController.ts b/src/app/api/super-admin/RulesController.ts new file mode 100644 index 00000000..b1858c0e --- /dev/null +++ b/src/app/api/super-admin/RulesController.ts @@ -0,0 +1,145 @@ +import { Response, Request } from "express"; +import { Controller, Get, Post, Put } from "@ControllerPattern/index"; +import ApiController from "@Common/system/controller-pattern/ApiController"; +import RulesService from "@Services/super-admin/RulesService/RulesService"; +import { Service } from "typedi"; +import { validateOrReject } from "class-validator"; +import { Rule } from "le-coffre-resources/dist/Notary"; +import { Rules } from "@prisma/client"; + +@Controller() +@Service() +export default class RulesController extends ApiController { + constructor(private rulesService: RulesService) { + super(); + } + + /** + * @description Get all rules + */ + @Get("/api/v1/super-admin/rules") + protected async get(req: Request, response: Response) { + try { + //get query + const query = JSON.parse(req.query["q"] as string); + + //call service to get prisma entity + const rulesEntities = await this.rulesService.get(query); + + //Hydrate ressource with prisma entity + const rules = Rule.hydrateArray(rulesEntities, { strategy: "excludeAll" }); + + //success + this.httpSuccess(response, rules); + } catch (error) { + this.httpInternalError(response, error); + return; + } + } + + /** + * @description Create a new rule + */ + @Post("/api/v1/super-admin/rules") + protected async getAddresses(req: Request, response: Response) { + try { + //init IRule resource with request body values + const ruleEntity = Rule.hydrate(req.body); + + //validate rule + await validateOrReject(ruleEntity, { groups: ["createRule"] }); + + //call service to get prisma entity + const ruleEntityCreated = await this.rulesService.create(ruleEntity); + + //Hydrate ressource with prisma entity + const rule = Rule.hydrate(ruleEntityCreated, { + strategy: "excludeAll", + }); + + //success + this.httpCreated(response, rule); + } catch (error) { + this.httpInternalError(response, error); + return; + } + } + + /** + * @description Modify a specific rule by uid + */ + @Put("/api/v1/super-admin/rules/:uid") + protected async put(req: Request, response: Response) { + try { + const uid = req.params["uid"]; + if (!uid) { + this.httpBadRequest(response, "No uid provided"); + return; + } + + const ruleFound = await this.rulesService.getByUid(uid); + + if (!ruleFound) { + this.httpNotFoundRequest(response, "rule not found"); + return; + } + + //init IRule resource with request body values + const ruleEntity = Rule.hydrate(req.body); + + //validate rule + await validateOrReject(ruleEntity, { groups: ["update"] }); + + //call service to get prisma entity + const ruleEntityUpdated = await this.rulesService.update(ruleEntity); + + //Hydrate ressource with prisma entity + const rule = Rule.hydrate(ruleEntityUpdated, { + strategy: "excludeAll", + }); + + //success + this.httpSuccess(response, rule); + } catch (error) { + this.httpInternalError(response, error); + return; + } + } + + /** + * @description Get a specific rule by uid + */ + @Get("/api/v1/super-admin/rules/:uid") + protected async getOneByUid(req: Request, response: Response) { + try { + const uid = req.params["uid"]; + if (!uid) { + this.httpBadRequest(response, "No uid provided"); + return; + } + let ruleEntity: Rules | null; + //get query + if (req.query["q"]) { + const query = JSON.parse(req.query["q"] as string); + ruleEntity = await this.rulesService.getByUid(uid, query); + } else { + //call service to get prisma entity + ruleEntity = await this.rulesService.getByUid(uid); + } + + if (!ruleEntity) { + this.httpNotFoundRequest(response, "rule not found"); + return; + } + + //Hydrate ressource with prisma entity + const rule = Rule.hydrate(ruleEntity, { strategy: "excludeAll" }); + + //success + this.httpSuccess(response, rule); + } catch (error) { + this.httpInternalError(response, error); + return; + } + } +} diff --git a/src/services/super-admin/RolesService/RolesService.ts b/src/services/super-admin/RolesService/RolesService.ts new file mode 100644 index 00000000..bbe52248 --- /dev/null +++ b/src/services/super-admin/RolesService/RolesService.ts @@ -0,0 +1,45 @@ +import BaseService from "@Services/BaseService"; +import "reflect-metadata"; +import { Service } from "typedi"; +import RolesRepository from "@Repositories/RolesRepository"; +import { Role } from "le-coffre-resources/dist/SuperAdmin"; +import {Prisma, Roles } from "@prisma/client"; + +@Service() +export default class RolesService extends BaseService { + constructor(private roleRepository: RolesRepository) { + super(); + } + + /** + * @description : Get all roles + * @throws {Error} If roles cannot be get + */ + public get(query: Prisma.RolesFindManyArgs): Promise { + return this.roleRepository.findMany(query); + } + + /** + * @description : Create a role + * @throws {Error} If role couldn't be created + */ + public create(roleEntity: Role): Promise { + return this.roleRepository.create(roleEntity); + } + + /** + * @description : Modify a role + * @throws {Error} If role modification failed + */ + public update(roleEntity: Role): Promise { + return this.roleRepository.update(roleEntity); + } + + /** + * @description : Get a role by uid + * @throws {Error} If role cannot be get by uid + */ + public getByUid(uid: string, query?: any): Promise { + return this.roleRepository.findOneByUid(uid, query); + } +} diff --git a/src/services/super-admin/RulesService/RulesService.ts b/src/services/super-admin/RulesService/RulesService.ts new file mode 100644 index 00000000..edc4ce64 --- /dev/null +++ b/src/services/super-admin/RulesService/RulesService.ts @@ -0,0 +1,45 @@ +import BaseService from "@Services/BaseService"; +import "reflect-metadata"; +import { Service } from "typedi"; +import RulesRepository from "@Repositories/RulesRepository"; +import { Rule } from "le-coffre-resources/dist/SuperAdmin"; +import {Prisma, Rules } from "@prisma/client"; + +@Service() +export default class RulesService extends BaseService { + constructor(private ruleRepository: RulesRepository) { + super(); + } + + /** + * @description : Get all rules + * @throws {Error} If rules cannot be get + */ + public get(query: Prisma.RulesFindManyArgs): Promise { + return this.ruleRepository.findMany(query); + } + + /** + * @description : Create a rule + * @throws {Error} If rule couldn't be created + */ + public create(ruleEntity: Rule): Promise { + return this.ruleRepository.create(ruleEntity); + } + + /** + * @description : Modify a rule + * @throws {Error} If rule modification failed + */ + public update(ruleEntity: Rule): Promise { + return this.ruleRepository.update(ruleEntity); + } + + /** + * @description : Get a rule by uid + * @throws {Error} If rule cannot be get by uid + */ + public getByUid(uid: string, query?: any): Promise { + return this.ruleRepository.findOneByUid(uid, query); + } +}