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

fix(configuration): use DTO in controller #936

Merged
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
16 changes: 9 additions & 7 deletions src/configuration/controllers/configuration.controller.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { Tf2ClassName } from '@/shared/models/tf2-class-name';
import { Test, TestingModule } from '@nestjs/testing';
import { DefaultPlayerSkill } from '../dto/default-player-skill';
import { WhitelistId } from '../dto/whitelist-id';
import { ConfigurationService } from '../services/configuration.service';
import { ConfigurationController } from './configuration.controller';

Expand Down Expand Up @@ -33,7 +35,7 @@ describe('ConfigurationController', () => {
});

it('should return default player skill', async () => {
expect(await controller.getDefaultPlayerSkill()).toEqual(defaultPlayerSkill);
expect(await controller.getDefaultPlayerSkill()).toEqual(new DefaultPlayerSkill(defaultPlayerSkill));
});
});

Expand All @@ -44,9 +46,9 @@ describe('ConfigurationController', () => {
configurationService.setDefaultPlayerSkill.mockResolvedValue(defaultPlayerSkill);
});

it('should set default player skll', async () => {
const ret = await controller.setDefaultPlayerSkill({ [Tf2ClassName.medic]: 5 });
expect(ret).toEqual(defaultPlayerSkill);
it('should set default player skill', async () => {
const ret = await controller.setDefaultPlayerSkill(new DefaultPlayerSkill(new Map([[Tf2ClassName.medic, 5]])));
expect(ret).toEqual(new DefaultPlayerSkill(defaultPlayerSkill));
expect(configurationService.setDefaultPlayerSkill).toHaveBeenCalledWith(defaultPlayerSkill);
});
});
Expand All @@ -57,7 +59,7 @@ describe('ConfigurationController', () => {
});

it('should return the whitelist id', async () => {
expect(await controller.getWhitelistId()).toEqual('etf2l_6v6');
expect(await controller.getWhitelistId()).toEqual(new WhitelistId('etf2l_6v6'));
});
});

Expand All @@ -67,8 +69,8 @@ describe('ConfigurationController', () => {
});

it('should set the whitelist id', async () => {
const ret = await controller.setWhitelistId('etf2l_6v6');
expect(ret).toEqual('etf2l_6v6');
const ret = await controller.setWhitelistId(new WhitelistId('etf2l_6v6'));
expect(ret).toEqual(new WhitelistId('etf2l_6v6'));
expect(configurationService.setWhitelistId).toHaveBeenCalledWith('etf2l_6v6');
});
});
Expand Down
21 changes: 12 additions & 9 deletions src/configuration/controllers/configuration.controller.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Auth } from '@/auth/decorators/auth.decorator';
import { Tf2ClassName } from '@/shared/models/tf2-class-name';
import { Body, ClassSerializerInterceptor, Controller, Get, Put, UseInterceptors } from '@nestjs/common';
import { Body, ClassSerializerInterceptor, Controller, Get, Put, UseInterceptors, ValidationPipe } from '@nestjs/common';
import { DefaultPlayerSkill } from '../dto/default-player-skill';
import { WhitelistId } from '../dto/whitelist-id';
import { ConfigurationService } from '../services/configuration.service';

@Controller('configuration')
Expand All @@ -13,27 +14,29 @@ export class ConfigurationController {
@Get('default-player-skill')
@UseInterceptors(ClassSerializerInterceptor)
async getDefaultPlayerSkill() {
return this.configurationService.getDefaultPlayerSkill();
return new DefaultPlayerSkill(await this.configurationService.getDefaultPlayerSkill());
}

@Put('default-player-skill')
@Auth('admin', 'super-user')
@UseInterceptors(ClassSerializerInterceptor)
async setDefaultPlayerSkill(
@Body() value: { [className in Tf2ClassName]?: number },
@Body(new ValidationPipe({ transform: true })) { value }: DefaultPlayerSkill,
) {
const valueAsMap = new Map(Object.entries(value)) as Map<Tf2ClassName, number>;
return this.configurationService.setDefaultPlayerSkill(valueAsMap);
return new DefaultPlayerSkill(await this.configurationService.setDefaultPlayerSkill(value));
}

@Get('whitelist-id')
@UseInterceptors(ClassSerializerInterceptor)
async getWhitelistId() {
return this.configurationService.getWhitelistId();
return new WhitelistId(await this.configurationService.getWhitelistId());
}

@Put('whitelist-id')
@Auth('admin', 'super-user')
async setWhitelistId(@Body() whitelistId: string) {
return this.configurationService.setWhitelistId(whitelistId);
@UseInterceptors(ClassSerializerInterceptor)
async setWhitelistId(@Body(new ValidationPipe()) { value }: WhitelistId) {
return new WhitelistId(await this.configurationService.setWhitelistId(value));
}

}
20 changes: 20 additions & 0 deletions src/configuration/dto/default-player-skill.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { Tf2ClassName } from '@/shared/models/tf2-class-name';
import { Type } from 'class-transformer';
import { Equals, IsNumber } from 'class-validator';
import { ConfigurationEntryKey } from '../models/configuration-entry-key';

export class DefaultPlayerSkill {

constructor(value: Map<Tf2ClassName, number>) {
this.key = ConfigurationEntryKey.defaultPlayerSkill;
this.value = value;
}

@Equals(ConfigurationEntryKey.defaultPlayerSkill)
key: string;

@IsNumber({ }, { each: true })
@Type(() => Number)
value: Map<Tf2ClassName, number>;

}
17 changes: 17 additions & 0 deletions src/configuration/dto/whitelist-id.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Equals, IsString } from 'class-validator';
import { ConfigurationEntryKey } from '../models/configuration-entry-key';

export class WhitelistId {

constructor(value: string) {
this.key = ConfigurationEntryKey.whitelistId;
this.value = value;
}

@Equals(ConfigurationEntryKey.whitelistId)
key: string;

@IsString()
value: string;

}