add services & controllers
This commit is contained in:
parent
5207fd3159
commit
219ebe5a83
145
src/app/api/super-admin/RolesController.ts
Normal file
145
src/app/api/super-admin/RolesController.ts
Normal file
@ -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<Rule>(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<Rule>(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<Rule>(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<Rule>(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<Rule>(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<Rule>(roleEntity, { strategy: "excludeAll" });
|
||||
|
||||
//success
|
||||
this.httpSuccess(response, role);
|
||||
} catch (error) {
|
||||
this.httpInternalError(response, error);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
145
src/app/api/super-admin/RulesController.ts
Normal file
145
src/app/api/super-admin/RulesController.ts
Normal file
@ -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<Rule>(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<Rule>(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<Rule>(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<Rule>(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<Rule>(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<Rule>(ruleEntity, { strategy: "excludeAll" });
|
||||
|
||||
//success
|
||||
this.httpSuccess(response, rule);
|
||||
} catch (error) {
|
||||
this.httpInternalError(response, error);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
45
src/services/super-admin/RolesService/RolesService.ts
Normal file
45
src/services/super-admin/RolesService/RolesService.ts
Normal file
@ -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<Roles[]> {
|
||||
return this.roleRepository.findMany(query);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description : Create a role
|
||||
* @throws {Error} If role couldn't be created
|
||||
*/
|
||||
public create(roleEntity: Role): Promise<Roles> {
|
||||
return this.roleRepository.create(roleEntity);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description : Modify a role
|
||||
* @throws {Error} If role modification failed
|
||||
*/
|
||||
public update(roleEntity: Role): Promise<Roles> {
|
||||
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<Roles | null> {
|
||||
return this.roleRepository.findOneByUid(uid, query);
|
||||
}
|
||||
}
|
45
src/services/super-admin/RulesService/RulesService.ts
Normal file
45
src/services/super-admin/RulesService/RulesService.ts
Normal file
@ -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<Rules[]> {
|
||||
return this.ruleRepository.findMany(query);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description : Create a rule
|
||||
* @throws {Error} If rule couldn't be created
|
||||
*/
|
||||
public create(ruleEntity: Rule): Promise<Rules> {
|
||||
return this.ruleRepository.create(ruleEntity);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description : Modify a rule
|
||||
* @throws {Error} If rule modification failed
|
||||
*/
|
||||
public update(ruleEntity: Rule): Promise<Rules> {
|
||||
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<Rules | null> {
|
||||
return this.ruleRepository.findOneByUid(uid, query);
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user