refacto hydratation for document controller
This commit is contained in:
parent
c608de5adc
commit
22d0239306
@ -4,7 +4,6 @@ import ApiController from "@Common/system/controller-pattern/ApiController";
|
|||||||
import { Service } from "typedi";
|
import { Service } from "typedi";
|
||||||
import DocumentsService from "@Services/customer/DocumentsService/DocumentsService";
|
import DocumentsService from "@Services/customer/DocumentsService/DocumentsService";
|
||||||
import { Documents } from "@prisma/client";
|
import { Documents } from "@prisma/client";
|
||||||
import ObjectHydrate from "@Common/helpers/ObjectHydrate";
|
|
||||||
import { Document } from "le-coffre-resources/dist/Customer";
|
import { Document } from "le-coffre-resources/dist/Customer";
|
||||||
import { validateOrReject } from "class-validator";
|
import { validateOrReject } from "class-validator";
|
||||||
|
|
||||||
@ -24,13 +23,12 @@ export default class DocumentsController extends ApiController {
|
|||||||
try {
|
try {
|
||||||
//get query
|
//get query
|
||||||
const query = JSON.parse(req.query["q"] as string);
|
const query = JSON.parse(req.query["q"] as string);
|
||||||
|
|
||||||
|
|
||||||
//call service to get prisma entity
|
//call service to get prisma entity
|
||||||
const prismaEntity: Documents[] = await this.documentsService.get(query);
|
const prismaEntity: Documents[] = await this.documentsService.get(query);
|
||||||
|
|
||||||
//Hydrate ressource with prisma entity
|
//Hydrate ressource with prisma entity
|
||||||
const documents = ObjectHydrate.map<Document>(Document, prismaEntity, { strategy: "exposeAll" });
|
const documents = Document.map<Document>(Document, prismaEntity, { strategy: "excludeAll" });
|
||||||
|
|
||||||
//success
|
//success
|
||||||
this.httpSuccess(response, documents);
|
this.httpSuccess(response, documents);
|
||||||
@ -49,17 +47,17 @@ export default class DocumentsController extends ApiController {
|
|||||||
try {
|
try {
|
||||||
//init Document resource with request body values
|
//init Document resource with request body values
|
||||||
const documentEntity = new Document();
|
const documentEntity = new Document();
|
||||||
ObjectHydrate.hydrate(documentEntity, req.body);
|
Document.hydrate(documentEntity, req.body);
|
||||||
|
|
||||||
//validate document
|
//validate document
|
||||||
await validateOrReject(documentEntity, { groups: ["create"] });
|
await validateOrReject(documentEntity, { groups: ["createDocument"] });
|
||||||
|
|
||||||
//call service to get prisma entity
|
//call service to get prisma entity
|
||||||
const prismaEntityCreated = await this.documentsService.create(documentEntity);
|
const prismaEntityCreated = await this.documentsService.create(documentEntity);
|
||||||
|
|
||||||
//Hydrate ressource with prisma entity
|
//Hydrate ressource with prisma entity
|
||||||
const documentEntityCreated = ObjectHydrate.hydrate<Document>(new Document(), prismaEntityCreated, {
|
const documentEntityCreated = Document.hydrate<Document>(prismaEntityCreated, {
|
||||||
strategy: "exposeAll",
|
strategy: "excludeAll",
|
||||||
});
|
});
|
||||||
|
|
||||||
//success
|
//success
|
||||||
@ -83,16 +81,16 @@ export default class DocumentsController extends ApiController {
|
|||||||
|
|
||||||
//init Document resource with request body values
|
//init Document resource with request body values
|
||||||
const documentEntity = new Document();
|
const documentEntity = new Document();
|
||||||
ObjectHydrate.hydrate(documentEntity, req.body);
|
Document.hydrate(documentEntity, req.body);
|
||||||
|
|
||||||
//validate document
|
//validate document
|
||||||
await validateOrReject(documentEntity, { groups: ["create"] });
|
await validateOrReject(documentEntity, { groups: ["createDocument"] });
|
||||||
|
|
||||||
//call service to get prisma entity
|
//call service to get prisma entity
|
||||||
const prismaEntityUpdated: Documents = await this.documentsService.update(uid, documentEntity);
|
const prismaEntityUpdated: Documents = await this.documentsService.update(uid, documentEntity);
|
||||||
|
|
||||||
//Hydrate ressource with prisma entity
|
//Hydrate ressource with prisma entity
|
||||||
const document = ObjectHydrate.hydrate<Document>(new Document(), prismaEntityUpdated, { strategy: "exposeAll" });
|
const document = Document.hydrate<Document>(prismaEntityUpdated, { strategy: "excludeAll" });
|
||||||
|
|
||||||
//success
|
//success
|
||||||
this.httpSuccess(response, document);
|
this.httpSuccess(response, document);
|
||||||
@ -117,7 +115,7 @@ export default class DocumentsController extends ApiController {
|
|||||||
const documentEntity: Documents = await this.documentsService.delete(uid);
|
const documentEntity: Documents = await this.documentsService.delete(uid);
|
||||||
|
|
||||||
//Hydrate ressource with prisma entity
|
//Hydrate ressource with prisma entity
|
||||||
const document = ObjectHydrate.hydrate<Document>(new Document(), documentEntity, { strategy: "exposeAll" });
|
const document = Document.hydrate<Document>(documentEntity, { strategy: "excludeAll" });
|
||||||
|
|
||||||
//success
|
//success
|
||||||
this.httpSuccess(response, document);
|
this.httpSuccess(response, document);
|
||||||
@ -149,7 +147,7 @@ export default class DocumentsController extends ApiController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
//Hydrate ressource with prisma entity
|
//Hydrate ressource with prisma entity
|
||||||
const document = ObjectHydrate.hydrate<Document>(new Document(), documentEntity, { strategy: "exposeAll" });
|
const document = Document.hydrate<Document>(documentEntity, { strategy: "excludeAll" });
|
||||||
|
|
||||||
//success
|
//success
|
||||||
this.httpSuccess(response, document);
|
this.httpSuccess(response, document);
|
||||||
|
@ -85,7 +85,7 @@ export default class DocumentsController extends ApiController {
|
|||||||
ObjectHydrate.hydrate(documentEntity, req.body);
|
ObjectHydrate.hydrate(documentEntity, req.body);
|
||||||
|
|
||||||
//validate document
|
//validate document
|
||||||
await validateOrReject(documentEntity, { groups: ["create"] });
|
await validateOrReject(documentEntity, { groups: ["createDocument"] });
|
||||||
|
|
||||||
//call service to get prisma entity
|
//call service to get prisma entity
|
||||||
const prismaEntityUpdated: Documents = await this.documentsService.update(uid, documentEntity);
|
const prismaEntityUpdated: Documents = await this.documentsService.update(uid, documentEntity);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user