Skip to content

Commit b863773

Browse files
committed
feat: get last submission for user (PUT)
1 parent 6c44867 commit b863773

File tree

3 files changed

+76
-14
lines changed

3 files changed

+76
-14
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { ApiProperty } from '@nestjs/swagger';
2+
import { IsNotEmpty, IsString } from 'class-validator';
3+
4+
export class FindLastSubmissionByLanguageDTO {
5+
@IsNotEmpty()
6+
@IsString()
7+
@ApiProperty()
8+
benchmarkId: string;
9+
10+
@IsNotEmpty()
11+
@IsString()
12+
@ApiProperty()
13+
language: string;
14+
}

src/submissions/submissions.controller.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
NotFoundException,
77
Param,
88
Post,
9+
Put,
910
Request,
1011
UseGuards,
1112
} from '@nestjs/common';
@@ -15,6 +16,7 @@ import { CreateSubmissionDTO } from './dto/create-submission-dto';
1516
import { FindSubmissionDTO } from './dto/find-submission.dto';
1617
import { Submission } from './submission.entity';
1718
import { SubmissionsService } from './submissions.service';
19+
import { FindLastSubmissionByLanguageDTO } from './dto/find-last-submission-by-language.dto';
1820

1921
@Controller('submissions')
2022
export class SubmissionsController {
@@ -44,6 +46,27 @@ export class SubmissionsController {
4446
return submission;
4547
}
4648

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

src/submissions/submissions.service.ts

Lines changed: 39 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@ import {
88
} from '@nestjs/common';
99
import { InjectRepository } from '@nestjs/typeorm';
1010
import { Cache } from 'cache-manager';
11-
import { isUUID } from 'class-validator';
1211
import { TypedJSON } from 'typedjson';
1312
import { Repository } from 'typeorm';
1413
import { BenchmarkService } from '../benchmarks/benchmark.service';
14+
import { User } from '../users/user.entity';
15+
import { FindLastSubmissionByLanguageDTO } from './dto/find-last-submission-by-language.dto';
1516
import { FindSubmissionDTO } from './dto/find-submission.dto';
1617
import { InsertSubmissionDTO } from './dto/insert-submission-dto';
1718
import { JobStatusDTO } from './dto/job-status.dto';
@@ -31,21 +32,13 @@ export class SubmissionsService {
3132
const submission = new Submission(insertSubmissionDTO);
3233
submission.status = 'waiting';
3334

34-
if (!isUUID(insertSubmissionDTO.benchmarkId)) {
35-
throw new BadRequestException(
36-
`Invalid benchmark id: ${insertSubmissionDTO.benchmarkId}`,
37-
);
38-
}
39-
const benchmark = await this.benchmarkService.findOne({
40-
id: insertSubmissionDTO.benchmarkId,
41-
});
35+
const benchmark = await this.benchmarkService.findOne(
36+
insertSubmissionDTO.benchmarkId,
37+
);
4238

43-
if (!benchmark) {
44-
throw new BadRequestException(
45-
`Could not find benchmark: ${insertSubmissionDTO.benchmarkId}`,
46-
);
39+
if (benchmark) {
40+
submission.benchmark = benchmark;
4741
}
48-
submission.benchmark = benchmark;
4942

5043
return submission.save();
5144
}
@@ -124,4 +117,36 @@ export class SubmissionsService {
124117
}
125118
}
126119
}
120+
121+
async findLastByLanguage(
122+
filter: FindLastSubmissionByLanguageDTO,
123+
requestUser: User,
124+
): Promise<Submission | undefined> {
125+
const bench = await this.benchmarkService.findOne(filter.benchmarkId);
126+
const matchedLanguage = await this.languageMatcher(filter.language);
127+
128+
if (!matchedLanguage) {
129+
throw new BadRequestException(`Invalid language: ${filter.language}`);
130+
}
131+
132+
return this.submissionsRepository.findOne({
133+
where: [
134+
{
135+
user: requestUser,
136+
benchmark: bench,
137+
language: matchedLanguage,
138+
},
139+
],
140+
order: { createdAt: 'DESC' },
141+
});
142+
}
143+
144+
async languageMatcher(language: string): Promise<string | undefined> {
145+
switch (language) {
146+
case 'python':
147+
return 'cpython3';
148+
default:
149+
return undefined;
150+
}
151+
}
127152
}

0 commit comments

Comments
 (0)