From a0d70fbb38889409561f23c76780db3acb202de6 Mon Sep 17 00:00:00 2001 From: Methapon2001 <61303214+Methapon2001@users.noreply.github.com> Date: Mon, 20 Nov 2023 12:03:28 +0700 Subject: [PATCH] feat: delete file --- .../server/src/controllers/fileController.ts | 41 +++++++++++++++++++ Prototype/server/src/routes.ts | 2 +- Prototype/server/src/swagger.json | 2 +- 3 files changed, 43 insertions(+), 2 deletions(-) diff --git a/Prototype/server/src/controllers/fileController.ts b/Prototype/server/src/controllers/fileController.ts index 7999145..fb47fa4 100644 --- a/Prototype/server/src/controllers/fileController.ts +++ b/Prototype/server/src/controllers/fileController.ts @@ -1,5 +1,6 @@ import { Controller, + Delete, FormField, Get, Path, @@ -165,4 +166,44 @@ export class FileController extends Controller { return records; } + + @Delete("/{cabinetName}/drawer/{drawerName}/folder/{folderName}/file/{fileName}") + @Tags("File") + @SuccessResponse(HttpStatusCode.OK) + public async deleteFile( + @Path() cabinetName: string, + @Path() drawerName: string, + @Path() folderName: string, + @Path() fileName: string, + ) { + const search = await esClient.search< + EhrFile & { + attachment: Record; + } + >({ + index: "ehr-api-client", + query: { + match: { + pathname: `${cabinetName}/${drawerName}/${folderName}/${fileName}`, + }, + }, + }); + + if (search && search.hits.hits.length === 0) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "Not found"); + } + + const esResult = await esClient + .delete({ + index: "ehr-api-client", + id: search.hits.hits[0]._id, + }) + .catch((e) => console.error(e)); + + if (!esResult) throw new Error("An error occured, cannot perform this action."); + + await minioClient.removeObject("ehr", `${cabinetName}/${drawerName}/${folderName}/${fileName}`); + + return this.setStatus(HttpStatusCode.NO_CONTENT); + } } diff --git a/Prototype/server/src/routes.ts b/Prototype/server/src/routes.ts index 14d6579..3c9b377 100644 --- a/Prototype/server/src/routes.ts +++ b/Prototype/server/src/routes.ts @@ -317,7 +317,7 @@ export function RegisterRoutes(app: Router) { } }); // WARNING: This file was auto-generated with tsoa. Please do not modify it. Re-run tsoa to re-generate this file: https://github.com/lukeautry/tsoa - app.get('/cabinet/:cabinetName/drawer/:drawerName/folder/:folderName/file/:fileName', + app.delete('/cabinet/:cabinetName/drawer/:drawerName/folder/:folderName/file/:fileName', ...(fetchMiddlewares(FileController)), ...(fetchMiddlewares(FileController.prototype.deleteFile)), diff --git a/Prototype/server/src/swagger.json b/Prototype/server/src/swagger.json index fa19899..0878d45 100644 --- a/Prototype/server/src/swagger.json +++ b/Prototype/server/src/swagger.json @@ -569,7 +569,7 @@ } }, "/cabinet/{cabinetName}/drawer/{drawerName}/folder/{folderName}/file/{fileName}": { - "get": { + "delete": { "operationId": "DeleteFile", "responses": { "200": {