lecoffre-back/src/app/api/super-admin/FilesController.ts
2023-05-10 23:26:02 +02:00

179 lines
4.8 KiB
TypeScript

import { Response, Request } from "express";
import { Controller, Delete, Get, Post, Put } from "@ControllerPattern/index";
import ApiController from "@Common/system/controller-pattern/ApiController";
import { Service } from "typedi";
import FilesService from "@Services/private-services/FilesService/FilesService";
import { Files } from "@prisma/client";
import { File, Document } from "le-coffre-resources/dist/SuperAdmin";
import { validateOrReject } from "class-validator";
import DocumentsService from "@Services/super-admin/DocumentsService/DocumentsService";
@Controller()
@Service()
export default class FilesController extends ApiController {
constructor(private filesService: FilesService, private documentService: DocumentsService) {
super();
}
/**
* @description Get all Files
* @returns File[] list of Files
*/
@Get("/api/v1/super-admin/files")
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 prismaEntity = await this.filesService.get(query);
//Hydrate ressource with prisma entity
const files = File.map<File>(File, prismaEntity, { strategy: "excludeAll" });
//success
this.httpSuccess(response, files);
} catch (error) {
this.httpBadRequest(response, error);
return;
}
}
/**
* @description Get a specific File by uid
*/
@Get("/api/v1/super-admin/files/upload/:uid")
protected async getFileData(req: Request, response: Response) {
try {
const uid = req.params["uid"];
if (!uid) {
throw new Error("No uid provided");
}
const file = await this.filesService.updload(uid);
this.httpSuccess(response, file);
} catch (error) {
this.httpBadRequest(response, error);
return;
}
}
/**
* @description Create a new File
* @returns File created
*/
@Post("/api/v1/super-admin/files")
protected async post(req: Request, response: Response) {
try {
//get file
if(!req.file) throw new Error('No file provided')
//init File resource with request body values
const fileEntity = File.hydrate<File>(JSON.parse(req.body["q"]));
//validate File
await validateOrReject(fileEntity, { groups: ["createFile"] });
//call service to get prisma entity
const prismaEntityCreated = await this.filesService.create(fileEntity, req.file);
const document: Document = await this.documentService.getByUid(fileEntity.document!.uid!)
document.document_status = "DEPOSITED";
await this.documentService.update(document.uid!, document);
//Hydrate ressource with prisma entity
const fileEntityCreated = File.hydrate<File>(prismaEntityCreated, {
strategy: "excludeAll",
});
//success
this.httpSuccess(response, fileEntityCreated);
} catch (error) {
this.httpBadRequest(response, error);
return;
}
}
/**
* @description Update a specific file
*/
@Put("/api/v1/super-admin/files/:uid")
protected async update(req: Request, response: Response) {
try {
const uid = req.params["uid"];
if (!uid) {
throw new Error("No uid provided");
}
//init File resource with request body values
const fileEntity = File.hydrate<File>(req.body);
//validate file
await validateOrReject(fileEntity, { groups: ["updateFile"] });
//call service to get prisma entity
const prismaEntityUpdated: Files = await this.filesService.update(uid, fileEntity);
//Hydrate ressource with prisma entity
const file = File.hydrate<File>(prismaEntityUpdated, { strategy: "excludeAll" });
//success
this.httpSuccess(response, file);
} catch (error) {
this.httpBadRequest(response, error);
return;
}
}
/**
* @description Delete a specific File
*/
@Delete("/api/v1/super-admin/files/:uid")
protected async delete(req: Request, response: Response) {
try {
const uid = req.params["uid"];
if (!uid) {
throw new Error("No uid provided");
}
//call service to get prisma entity
const fileEntity: Files = await this.filesService.delete(uid);
//Hydrate ressource with prisma entity
const file = File.hydrate<File>(fileEntity, { strategy: "excludeAll" });
//success
this.httpSuccess(response, file);
} catch (error) {
this.httpBadRequest(response, error);
return;
}
}
/**
* @description Get a specific File by uid
*/
@Get("/api/v1/super-admin/files/:uid")
protected async getOneByUid(req: Request, response: Response) {
try {
const uid = req.params["uid"];
if (!uid) {
throw new Error("No uid provided");
}
const fileEntity = await this.filesService.getByUid(uid);
//Hydrate ressource with prisma entity
const file = File.hydrate<File>(fileEntity, { strategy: "excludeAll" });
//success
this.httpSuccess(response, file);
} catch (error) {
this.httpBadRequest(response, error);
return;
}
}
}