diff --git a/src/judge/contributer/contributer.controller.ts b/src/judge/contributer/contributer.controller.ts index 4c8fa77..b8a361e 100644 --- a/src/judge/contributer/contributer.controller.ts +++ b/src/judge/contributer/contributer.controller.ts @@ -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() @@ -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') diff --git a/src/judge/contributer/contributer.service.spec.ts b/src/judge/contributer/contributer.service.spec.ts index 2bc0f86..51bbc2a 100644 --- a/src/judge/contributer/contributer.service.spec.ts +++ b/src/judge/contributer/contributer.service.spec.ts @@ -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(); }); }); diff --git a/src/judge/contributer/contributer.service.ts b/src/judge/contributer/contributer.service.ts index 4d3e6cf..d399aec 100644 --- a/src/judge/contributer/contributer.service.ts +++ b/src/judge/contributer/contributer.service.ts @@ -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() @@ -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; @@ -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, }, }); @@ -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, @@ -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: { diff --git a/src/judge/contributer/dto/create-example.dto.ts b/src/judge/contributer/dto/create-example.dto.ts new file mode 100644 index 0000000..75d001f --- /dev/null +++ b/src/judge/contributer/dto/create-example.dto.ts @@ -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; +} diff --git a/src/judge/contributer/dto/index.ts b/src/judge/contributer/dto/index.ts index 7f3730d..8ba1e82 100644 --- a/src/judge/contributer/dto/index.ts +++ b/src/judge/contributer/dto/index.ts @@ -1,2 +1,3 @@ export * from './update-problem.dto'; export * from './update-example.dto'; +export * from './create-example.dto'; diff --git a/src/judge/contributer/dto/update-example.dto.ts b/src/judge/contributer/dto/update-example.dto.ts index 11bf305..26ab14b 100644 --- a/src/judge/contributer/dto/update-example.dto.ts +++ b/src/judge/contributer/dto/update-example.dto.ts @@ -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';