瀏覽代碼

include history

Dr-Swopt 4 天之前
父節點
當前提交
95bec7a417
共有 3 個文件被更改,包括 39 次插入2 次删除
  1. 11 1
      src/palm-oil/palm-oil.controller.ts
  2. 27 0
      src/palm-oil/palm-oil.service.ts
  3. 1 1
      src/surveillance/surveillance.gateway.ts

+ 11 - 1
src/palm-oil/palm-oil.controller.ts

@@ -9,7 +9,7 @@
  * the Angular history tab and do not conflict with the Hoarding architecture.
  */
 
-import { Controller, Get, Res, Param } from '@nestjs/common';
+import { Controller, Get, Delete, Res, Param } from '@nestjs/common';
 import { PalmOilService } from './palm-oil.service';
 import { Response } from 'express';
 import * as fs from 'fs';
@@ -23,6 +23,16 @@ export class PalmOilController {
     return this.palmOilService.getHistory();
   }
 
+  @Delete('history/:archiveId')
+  async deleteRecord(@Param('archiveId') archiveId: string) {
+    return this.palmOilService.deleteRecord(archiveId);
+  }
+
+  @Delete('history')
+  async clearAllHistory() {
+    return this.palmOilService.clearAllHistory();
+  }
+
   @Get('archive/:id')
   async getArchivedImage(@Param('id') id: string, @Res() res: Response) {
     const record = await this.palmOilService.getRecordByArchiveId(id);

+ 27 - 0
src/palm-oil/palm-oil.service.ts

@@ -114,4 +114,31 @@ export class PalmOilService {
   async getRecordByArchiveId(archiveId: string): Promise<History | null> {
     return this.historyRepository.findOne({ where: { archive_id: archiveId } });
   }
+
+  async deleteRecord(archiveId: string): Promise<{ deleted: boolean }> {
+    const record = await this.historyRepository.findOne({ where: { archive_id: archiveId } });
+    if (!record) return { deleted: false };
+
+    // Remove archived image from disk
+    if (record.image_path && fs.existsSync(record.image_path)) {
+      fs.unlinkSync(record.image_path);
+    }
+
+    await this.historyRepository.delete({ archive_id: archiveId });
+    return { deleted: true };
+  }
+
+  async clearAllHistory(): Promise<{ deleted: number }> {
+    const records = await this.historyRepository.find();
+
+    // Remove every archived image from disk
+    for (const record of records) {
+      if (record.image_path && fs.existsSync(record.image_path)) {
+        fs.unlinkSync(record.image_path);
+      }
+    }
+
+    await this.historyRepository.clear();
+    return { deleted: records.length };
+  }
 }

+ 1 - 1
src/surveillance/surveillance.gateway.ts

@@ -27,7 +27,7 @@ export class SurveillanceGateway
   implements OnGatewayInit, OnGatewayConnection, OnGatewayDisconnect, OnModuleInit
 {
   @WebSocketServer()
-  private server: Server;
+  private server!: Server;
 
   private readonly logger = new Logger(SurveillanceGateway.name);