Skip to content

Commit

Permalink
Fix: Fix create example to get dto and fix as send sqs when example m…
Browse files Browse the repository at this point in the history
…odified
  • Loading branch information
J-Hoplin committed Jan 15, 2024
1 parent 6dab696 commit 660cda8
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 20 deletions.
5 changes: 3 additions & 2 deletions src/judge/contributer/contributer.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { RoleGuard } from 'app/guard';
import { UpdateProblmeDto } from 'app/judge/contributer/dto/update-problem.dto';
import { ContributerDocs } from './contributer.docs';
import { ContributerService } from './contributer.service';
import { UpdateExampleDto } from './dto';
import { CreateExampleDto, UpdateExampleDto } from './dto';
import { ContributerProblemGuard } from './decorator/contributer-problem.guard';

@Controller()
Expand Down Expand Up @@ -81,8 +81,9 @@ export class ContributerController {
createExample(
@GetUser('id') uid: string,
@Param('pid', ParseIntPipe) pid: number,
@Body() dto: CreateExampleDto,
) {
return this.contributerService.createExmaple(uid, pid);
return this.contributerService.createExmaple(uid, pid, dto);
}

@Patch('problems/:pid/examples/:eid')
Expand Down
6 changes: 5 additions & 1 deletion src/judge/contributer/contributer.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,11 @@ describe('ContributerService', () => {
});
it('should create new example of problem1', async () => {
// Create example of problem1
example1 = await service.createExmaple(user1.id, problem1.id);
example1 = await service.createExmaple(user1.id, problem1.id, {
input: '2 3 4',
output: '5 6 7',
isPublic: true,
});
expect(example1).toBeTruthy();
});
});
Expand Down
32 changes: 16 additions & 16 deletions src/judge/contributer/contributer.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { ForbiddenException, Injectable } from '@nestjs/common';
import { PaginateObject } from 'app/decorator';
import { UpdateProblmeDto } from 'app/judge/contributer/dto/update-problem.dto';
import { PrismaService } from 'app/prisma/prisma.service';
import { UpdateExampleDto } from './dto';
import { CreateExampleDto, UpdateExampleDto } from './dto';
import { AwsSqsService } from 'aws-sqs/aws-sqs';

@Injectable()
Expand Down Expand Up @@ -49,13 +49,6 @@ export class ContributerService {
}

async updateProblem(uid: string, pid: number, dto: UpdateProblmeDto) {
const findProblem = await this.prisma.problem.findUnique({
where: {
id: pid,
contributerId: uid,
},
});

// If time limit is lower than 0
if (dto?.timeLimit && dto.timeLimit < 0) {
dto.timeLimit = 5;
Expand Down Expand Up @@ -98,9 +91,10 @@ export class ContributerService {
return updatedProblem;
}

async createExmaple(uid: string, pid: number) {
async createExmaple(uid: string, pid: number, dto: CreateExampleDto) {
return this.prisma.problemExample.create({
data: {
...dto,
problemId: pid,
},
});
Expand All @@ -112,7 +106,7 @@ export class ContributerService {
eid: number,
dto: UpdateExampleDto,
) {
const findExample = await this.prisma.problemExample.findUnique({
const previousExample = await this.prisma.problemExample.findUnique({
where: {
id: eid,
problemId: pid,
Expand All @@ -122,15 +116,21 @@ export class ContributerService {
},
});

if (!findExample) {
if (!previousExample) {
throw new ForbiddenException('FORBIDDEN_REQUEST');
}

// Send task to client
await this.sqs.sendTask({
id: pid,
message: 'RE_CORRECTION',
});
// Only trigger re-correction if it input or out-put modified
if (
dto.input !== previousExample.input ||
dto.output !== previousExample.output
) {
// Send task to client
await this.sqs.sendTask({
id: pid,
message: 'RE_CORRECTION',
});
}

return this.prisma.problemExample.update({
where: {
Expand Down
20 changes: 20 additions & 0 deletions src/judge/contributer/dto/create-example.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { OmitType } from '@nestjs/swagger';
import { IsString, IsOptional, IsBoolean } from 'class-validator';
import { ProblemExampleDomain } from 'domains';

export class CreateExampleDto extends OmitType(ProblemExampleDomain, [
'id',
'problemId',
]) {
@IsString()
@IsOptional()
input: string;

@IsString()
@IsOptional()
output: string;

@IsBoolean()
@IsOptional()
isPublic: boolean;
}
1 change: 1 addition & 0 deletions src/judge/contributer/dto/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './update-problem.dto';
export * from './update-example.dto';
export * from './create-example.dto';
2 changes: 1 addition & 1 deletion src/judge/contributer/dto/update-example.dto.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ApiProperty, OmitType } from '@nestjs/swagger';
import { OmitType } from '@nestjs/swagger';
import { IsBoolean, IsOptional, IsString } from 'class-validator';
import { ProblemExampleDomain } from 'domains';

Expand Down

0 comments on commit 660cda8

Please sign in to comment.