Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add missing dto for database requests #744

Merged
merged 1 commit into from
Jan 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions client/src/http/Database.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
}
}
49 changes: 31 additions & 18 deletions server/src/database/databases.controller.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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) {
Expand All @@ -64,25 +67,31 @@ export class DatabasesController {
}
}

async dropDatabase(req: Request, res: Response, next: NextFunction) {
const db_name = req.params?.name;
async dropDatabase(
req: Request<DatabaseNameDto>,
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) {
next(error);
}
}

async describeDatabase(req: Request, res: Response, next: NextFunction) {
const db_name = req.params?.name;
async describeDatabase(
req: Request<DatabaseNameDto>,
res: Response,
next: NextFunction
) {
try {
const result = await this.databasesService.describeDatabase(
req.clientId,
{
db_name,
db_name: req.params.db_name,
}
);
res.send(result);
Expand All @@ -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<DatabaseNameDto, {}, DatabasePropertiesDto>,
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);
Expand Down
11 changes: 8 additions & 3 deletions server/src/database/dto.ts
Original file line number Diff line number Diff line change
@@ -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<string, 'string | number | boolean'>;
}
Loading