From 32bc7b602eec4209218fc54367f94d36a2e1fee2 Mon Sep 17 00:00:00 2001 From: ryjiang Date: Mon, 20 Jan 2025 19:26:50 +0800 Subject: [PATCH] chore: add missing dto for database requests Signed-off-by: ryjiang --- client/src/http/Database.service.ts | 5 ++- server/src/database/databases.controller.ts | 49 +++++++++++++-------- server/src/database/dto.ts | 11 +++-- 3 files changed, 42 insertions(+), 23 deletions(-) diff --git a/client/src/http/Database.service.ts b/client/src/http/Database.service.ts index 8655288c..c50afbb2 100644 --- a/client/src/http/Database.service.ts +++ b/client/src/http/Database.service.ts @@ -39,9 +39,10 @@ export class DatabaseService extends BaseModel { } static setProperty(data: AlterDatabaseRequest) { + const { db_name, ...properties } = data; return super.update({ - path: `/databases/${data.db_name}/properties`, - data: data.properties, + path: `/databases/${db_name}/properties`, + data: properties, }); } } diff --git a/server/src/database/databases.controller.ts b/server/src/database/databases.controller.ts index 27870f19..1cf741f4 100644 --- a/server/src/database/databases.controller.ts +++ b/server/src/database/databases.controller.ts @@ -1,7 +1,7 @@ import { NextFunction, Request, Response, Router } from 'express'; import { dtoValidationMiddleware } from '../middleware/validation'; import { DatabasesService } from './databases.service'; -import { CreateDatabaseDto } from './dto'; +import { DatabaseNameDto, DatabasePropertiesDto } from './dto'; import { DatabaseObject } from '../types'; export class DatabasesController { @@ -22,23 +22,26 @@ export class DatabasesController { this.router.get('/', this.listDatabases.bind(this)); this.router.post( '/', - dtoValidationMiddleware(CreateDatabaseDto), + dtoValidationMiddleware(DatabaseNameDto), this.createDatabase.bind(this) ); - this.router.get('/:name', this.describeDatabase.bind(this)); - this.router.delete('/:name', this.dropDatabase.bind(this)); - this.router.put('/:name/properties', this.alterDatabase.bind(this)); + this.router.get('/:db_name', this.describeDatabase.bind(this)); + this.router.delete('/:db_name', this.dropDatabase.bind(this)); + this.router.put('/:db_name/properties', this.alterDatabase.bind(this)); return this.router; } - async createDatabase(req: Request, res: Response, next: NextFunction) { - const createDatabaseData = req.body; + async createDatabase( + req: Request<{}, {}, DatabaseNameDto>, + res: Response, + next: NextFunction + ) { try { const result = await this.databasesService.createDatabase( req.clientId, - createDatabaseData + req.body ); res.send(result); } catch (error) { @@ -64,11 +67,14 @@ export class DatabasesController { } } - async dropDatabase(req: Request, res: Response, next: NextFunction) { - const db_name = req.params?.name; + async dropDatabase( + req: Request, + res: Response, + next: NextFunction + ) { try { const result = await this.databasesService.dropDatabase(req.clientId, { - db_name, + db_name: req.params.db_name, }); res.send(result); } catch (error) { @@ -76,13 +82,16 @@ export class DatabasesController { } } - async describeDatabase(req: Request, res: Response, next: NextFunction) { - const db_name = req.params?.name; + async describeDatabase( + req: Request, + res: Response, + next: NextFunction + ) { try { const result = await this.databasesService.describeDatabase( req.clientId, { - db_name, + db_name: req.params.db_name, } ); res.send(result); @@ -91,12 +100,16 @@ export class DatabasesController { } } - async alterDatabase(req: Request, res: Response, next: NextFunction) { - const db_name = req.params?.name; - const properties = req.body; + async alterDatabase( + req: Request, + res: Response, + next: NextFunction + ) { + const { properties } = req.body; + try { const result = await this.databasesService.alterDatabase(req.clientId, { - db_name, + db_name: req.params.db_name, properties, }); res.send(result); diff --git a/server/src/database/dto.ts b/server/src/database/dto.ts index ffd49c26..3e134b94 100644 --- a/server/src/database/dto.ts +++ b/server/src/database/dto.ts @@ -1,6 +1,11 @@ -import { IsString } from 'class-validator'; +import { IsNotEmpty } from 'class-validator'; -export class CreateDatabaseDto { - @IsString() +export class DatabaseNameDto { + @IsNotEmpty({ message: 'db_name is empty' }) readonly db_name: string; } + +export class DatabasePropertiesDto { + @IsNotEmpty({ message: 'properties is empty' }) + readonly properties: Record; +} \ No newline at end of file