Skip to content

Commit 1542c58

Browse files
angristanMikadows
authored andcommitted
refactor(benchmarks): use GET for last submission route + move to BenchmarkController
1 parent e3ec454 commit 1542c58

File tree

6 files changed

+58
-25
lines changed

6 files changed

+58
-25
lines changed

src/benchmarks/benchmark.controller.ts

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,31 @@ import {
22
Body,
33
Controller,
44
Get,
5+
NotFoundException,
56
Param,
67
Post,
8+
Query,
79
Request,
810
UseGuards,
911
} from '@nestjs/common';
1012
import { ApiOkResponse, ApiOperation } from '@nestjs/swagger';
1113
import { BenchmarkService } from 'src/benchmarks/benchmark.service';
1214
import { CreateBenchmarkDto } from 'src/benchmarks/dto/create-benchmark.dto';
15+
import { FindSubmissionIDDTO } from 'src/submissions/dto/find-submission-id.dto';
16+
import { FindSubmissionLangDTO } from 'src/submissions/dto/find-submission-lang.dto';
17+
import { Submission } from 'src/submissions/submission.entity';
18+
import { SubmissionsService } from 'src/submissions/submissions.service';
1319
import { ValidatedJWTReq } from '../auth/dto/validated-jwt-req';
1420
import { JwtAuthGuard } from '../auth/jwt-auth.guard';
1521
import { Benchmark } from './benchmark.entity';
1622
import { BenchmarkIdDto } from './dto/benchmarkId.dto';
1723

1824
@Controller('benchmarks')
1925
export class BenchmarkController {
20-
constructor(private readonly benchmarkService: BenchmarkService) {}
26+
constructor(
27+
private readonly benchmarkService: BenchmarkService,
28+
private readonly submissionsService: SubmissionsService,
29+
) {}
2130

2231
@ApiOperation({ summary: 'Create a benchmark' })
2332
@ApiOkResponse({ type: Benchmark, description: 'Created benchmark' })
@@ -46,4 +55,28 @@ export class BenchmarkController {
4655
): Promise<Benchmark | undefined> {
4756
return this.benchmarkService.findOne(benchmarkIdDto.id);
4857
}
58+
59+
@ApiOperation({ summary: 'Get last submission for benchmark + language' })
60+
@UseGuards(JwtAuthGuard)
61+
@Get(':id/submissions/last')
62+
async findLastForUserByLanguage(
63+
@Request() req: ValidatedJWTReq,
64+
@Query() query: FindSubmissionLangDTO,
65+
@Param() params: FindSubmissionIDDTO,
66+
): Promise<Submission> {
67+
const submission: Submission | undefined =
68+
await this.submissionsService.findLastByLanguage(
69+
{
70+
benchmarkId: params.id,
71+
language: query.language,
72+
},
73+
req.user,
74+
);
75+
76+
if (!submission) {
77+
throw new NotFoundException();
78+
}
79+
80+
return submission;
81+
}
4982
}

src/benchmarks/benchmark.module.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
1-
import { Module, forwardRef } from '@nestjs/common';
2-
import { UsersModule } from 'src/users/users.module';
1+
import { forwardRef, Module } from '@nestjs/common';
32
import { TypeOrmModule } from '@nestjs/typeorm';
3+
import { SubmissionsModule } from 'src/submissions/submissions.module';
4+
import { UsersModule } from 'src/users/users.module';
5+
import { BenchmarkController } from './benchmark.controller';
46
import { Benchmark } from './benchmark.entity';
57
import { BenchmarkService } from './benchmark.service';
6-
import { BenchmarkController } from './benchmark.controller';
78

89
@Module({
910
imports: [
1011
TypeOrmModule.forFeature([Benchmark]),
1112
forwardRef(() => UsersModule),
13+
SubmissionsModule,
1214
],
1315
providers: [BenchmarkService],
1416
controllers: [BenchmarkController],
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { ApiProperty } from '@nestjs/swagger';
2+
import { IsNotEmpty, IsString } from 'class-validator';
3+
4+
export class FindSubmissionIDDTO {
5+
@IsNotEmpty()
6+
@IsString()
7+
@ApiProperty()
8+
id: string;
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { ApiProperty } from '@nestjs/swagger';
2+
import { IsNotEmpty, IsString } from 'class-validator';
3+
4+
export class FindSubmissionLangDTO {
5+
@IsNotEmpty()
6+
@IsString()
7+
@ApiProperty()
8+
language: string;
9+
}

src/submissions/submissions.controller.ts

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,12 @@ import {
66
NotFoundException,
77
Param,
88
Post,
9-
Put,
109
Request,
1110
UseGuards,
1211
} from '@nestjs/common';
1312
import { ValidatedJWTReq } from 'src/auth/dto/validated-jwt-req';
1413
import { JwtAuthGuard } from 'src/auth/jwt-auth.guard';
1514
import { CreateSubmissionDTO } from './dto/create-submission-dto';
16-
import { FindLastSubmissionByLanguageDTO } from './dto/find-last-submission-by-language.dto';
1715
import { FindSubmissionDTO } from './dto/find-submission.dto';
1816
import { Submission } from './submission.entity';
1917
import { SubmissionsService } from './submissions.service';
@@ -46,25 +44,6 @@ export class SubmissionsController {
4644
return submission;
4745
}
4846

49-
@UseGuards(JwtAuthGuard)
50-
@Put()
51-
async findLastForUserByLanguage(
52-
@Request() req: ValidatedJWTReq,
53-
@Body() findLastSubmissionByLanguageDTO: FindLastSubmissionByLanguageDTO,
54-
): Promise<Submission> {
55-
const submission: Submission | undefined =
56-
await this.submissionsService.findLastByLanguage(
57-
findLastSubmissionByLanguageDTO,
58-
req.user,
59-
);
60-
61-
if (!submission) {
62-
throw new NotFoundException();
63-
}
64-
65-
return submission;
66-
}
67-
6847
@UseGuards(JwtAuthGuard)
6948
@Get(':id')
7049
async findOne(

src/submissions/submissions.module.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,6 @@ import { SubmissionsService } from './submissions.service';
2525
],
2626
providers: [SubmissionsService],
2727
controllers: [SubmissionsController],
28+
exports: [SubmissionsService],
2829
})
2930
export class SubmissionsModule {}

0 commit comments

Comments
 (0)