Переглянути джерело

refine partition controller

Signed-off-by: Gitea <zizhao.chen@zilliz.com>
Gitea 3 роки тому
батько
коміт
200fddd262

+ 30 - 0
express/src/partitions/dto.ts

@@ -0,0 +1,30 @@
+import { IsString, IsEnum, IsArray, ArrayNotEmpty } from "class-validator";
+
+export enum ManageType {
+  DELETE = "delete",
+  CREATE = "create",
+}
+export class GetPartitionsInfoDto {
+  @IsString()
+  readonly collection_name: string;
+}
+
+export class ManagePartitionDto {
+  @IsString()
+  readonly collection_name: string;
+
+  @IsString()
+  readonly partition_name: string;
+
+  @IsEnum(ManageType, { message: "Type allow delete and create" })
+  readonly type: ManageType;
+}
+
+export class LoadPartitionsDto {
+  @IsString()
+  readonly collection_name: string;
+
+  @IsArray()
+  @ArrayNotEmpty()
+  readonly partition_names: string[];
+}

+ 3 - 48
express/src/partitions/index.ts

@@ -1,51 +1,6 @@
-import express from "express";
-import { PartitionsService } from "./partitions.service";
-import { milvusService } from "../milvus";
+import { PartitionController } from "./partitions.controller";
 
-const router = express.Router();
-
-const partitionsService = new PartitionsService(milvusService);
-
-router.get("/", async (req, res, next) => {
-  const collectionName = "" + req.query?.collection_name;
-  try {
-    const result = await partitionsService.getPatitionsInfo({
-      collection_name: collectionName,
-    });
-    res.send(result);
-  } catch (error) {
-    next(error);
-  }
-});
-router.post("/", async (req, res, next) => {
-  const { type, ...params } = req.body;
-  try {
-    const result =
-      type.toLocaleLowerCase() === "create"
-        ? await partitionsService.createParition(params)
-        : await partitionsService.deleteParition(params);
-    res.send(result);
-  } catch (error) {
-    next(error);
-  }
-});
-router.put("/load", async (req, res, next) => {
-  const loadData = req.body;
-  try {
-    const result = await partitionsService.loadPartitions(loadData);
-    res.send(result);
-  } catch (error) {
-    next(error);
-  }
-});
-router.put("/release", async (req, res, next) => {
-  const loadData = req.body;
-  try {
-    const result = await partitionsService.releasePartitions(loadData);
-    res.send(result);
-  } catch (error) {
-    next(error);
-  }
-});
+const partitionManager = new PartitionController();
+const router = partitionManager.generateRoutes();
 
 export { router };

+ 93 - 0
express/src/partitions/partitions.controller.ts

@@ -0,0 +1,93 @@
+import { NextFunction, Request, Response, Router } from "express";
+import { dtoValidationMiddleware } from "../middlewares/validation";
+import { PartitionsService } from "./partitions.service";
+import { milvusService } from "../milvus";
+
+import {
+  GetPartitionsInfoDto,
+  ManagePartitionDto,
+  LoadPartitionsDto,
+} from "./dto";
+
+export class PartitionController {
+  private router: Router;
+  private partitionsService: PartitionsService;
+
+  constructor() {
+    this.partitionsService = new PartitionsService(milvusService);
+    this.router = Router();
+  }
+
+  generateRoutes() {
+    this.router.get(
+      "/",
+      dtoValidationMiddleware(GetPartitionsInfoDto),
+      this.getPatitionsInfo.bind(this)
+    );
+
+    this.router.post(
+      "/",
+      dtoValidationMiddleware(ManagePartitionDto),
+      this.managePartition.bind(this)
+    );
+
+    this.router.post(
+      "/load",
+      dtoValidationMiddleware(LoadPartitionsDto),
+      this.loadPartition.bind(this)
+    );
+
+    this.router.post(
+      "/release",
+      dtoValidationMiddleware(LoadPartitionsDto),
+      this.releasePartition.bind(this)
+    );
+
+    return this.router;
+  }
+
+  async getPatitionsInfo(req: Request, res: Response, next: NextFunction) {
+    const collectionName = "" + req.query?.collection_name;
+    try {
+      const result = await this.partitionsService.getPatitionsInfo({
+        collection_name: collectionName,
+      });
+      res.send(result);
+    } catch (error) {
+      next(error);
+    }
+  }
+
+  async managePartition(req: Request, res: Response, next: NextFunction) {
+    const { type, ...params } = req.body;
+    try {
+      const result =
+        type.toLocaleLowerCase() === "create"
+          ? await this.partitionsService.createParition(params)
+          : await this.partitionsService.deleteParition(params);
+      res.send(result);
+    } catch (error) {
+      next(error);
+    }
+  }
+
+  async loadPartition(req: Request, res: Response, next: NextFunction) {
+    const data = req.body;
+    try {
+      const result = await this.partitionsService.loadPartitions(data);
+      res.send(result);
+    } catch (error) {
+      next(error);
+    }
+  }
+
+  async releasePartition(req: Request, res: Response, next: NextFunction) {
+    const data = req.body;
+    try {
+      const result = await this.partitionsService.releasePartitions(data);
+      res.send(result);
+    } catch (error) {
+      next(error);
+    }
+  }
+}