Skip to content

Commit

Permalink
refactor(be): improve client submission module error handling logic (#…
Browse files Browse the repository at this point in the history
…2118)

* refactor(be): improve client submission module error handling logic

* chore(be): revert deleted comments

* chore(be): change submission test code followed by refactor
  • Loading branch information
jimin9038 authored Oct 1, 2024
1 parent 1973ef9 commit 46e9322
Show file tree
Hide file tree
Showing 3 changed files with 205 additions and 261 deletions.
182 changes: 53 additions & 129 deletions apps/backend/apps/client/src/submission/submission.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,11 @@ import {
Param,
Body,
Req,
NotFoundException,
InternalServerErrorException,
Logger,
Query,
DefaultValuePipe,
Headers
} from '@nestjs/common'
import { Prisma } from '@prisma/client'
import { AuthNotNeededIfOpenSpace, AuthenticatedRequest } from '@libs/auth'
import {
ConflictFoundException,
EntityNotExistException,
ForbiddenAccessException
} from '@libs/exception'
import {
CursorValidationPipe,
GroupIDPipe,
Expand All @@ -30,8 +21,6 @@ import { SubmissionService } from './submission.service'

@Controller('submission')
export class SubmissionController {
private readonly logger = new Logger(SubmissionController.name)

constructor(private readonly submissionService: SubmissionService) {}

@Post()
Expand All @@ -44,47 +33,32 @@ export class SubmissionController {
@Query('contestId', IDValidationPipe) contestId: number | null,
@Query('workbookId', IDValidationPipe) workbookId: number | null
) {
try {
if (!contestId && !workbookId) {
return await this.submissionService.submitToProblem(
submissionDto,
userIp,
req.user.id,
problemId,
groupId
)
} else if (contestId) {
return await this.submissionService.submitToContest(
submissionDto,
userIp,
req.user.id,
problemId,
contestId,
groupId
)
} else if (workbookId) {
return await this.submissionService.submitToWorkbook(
submissionDto,
userIp,
req.user.id,
problemId,
workbookId,
groupId
)
}
} catch (error) {
if (error instanceof ConflictFoundException) {
throw error.convert2HTTPException()
}
if (
(error instanceof Prisma.PrismaClientKnownRequestError &&
error.name == 'NotFoundError') ||
error instanceof EntityNotExistException
) {
throw new NotFoundException(error.message)
}
this.logger.error(error)
throw new InternalServerErrorException()
if (!contestId && !workbookId) {
return await this.submissionService.submitToProblem(
submissionDto,
userIp,
req.user.id,
problemId,
groupId
)
} else if (contestId) {
return await this.submissionService.submitToContest(
submissionDto,
userIp,
req.user.id,
problemId,
contestId,
groupId
)
} else if (workbookId) {
return await this.submissionService.submitToWorkbook(
submissionDto,
userIp,
req.user.id,
problemId,
workbookId,
groupId
)
}
}

Expand All @@ -98,22 +72,11 @@ export class SubmissionController {
@Query('problemId', new RequiredIntPipe('problemId')) problemId: number,
@Body() submissionDto: CreateSubmissionDto
) {
try {
return await this.submissionService.submitTest(
req.user.id,
problemId,
submissionDto
)
} catch (error) {
if (
error instanceof EntityNotExistException ||
error instanceof ConflictFoundException
) {
throw error.convert2HTTPException()
}
this.logger.error(error)
throw new InternalServerErrorException()
}
return await this.submissionService.submitTest(
req.user.id,
problemId,
submissionDto
)
}

/**
Expand All @@ -139,24 +102,12 @@ export class SubmissionController {
@Query('problemId', new RequiredIntPipe('problemId')) problemId: number,
@Query('groupId', GroupIDPipe) groupId: number
) {
try {
return await this.submissionService.getSubmissions({
cursor,
take,
problemId,
groupId
})
} catch (error) {
if (
(error instanceof Prisma.PrismaClientKnownRequestError &&
error.name == 'NotFoundError') ||
error instanceof EntityNotExistException
) {
throw new NotFoundException(error.message)
}
this.logger.error(error)
throw new InternalServerErrorException()
}
return await this.submissionService.getSubmissions({
cursor,
take,
problemId,
groupId
})
}

@Get(':id')
Expand All @@ -167,35 +118,19 @@ export class SubmissionController {
@Query('contestId', IDValidationPipe) contestId: number | null,
@Param('id', new RequiredIntPipe('id')) id: number
) {
try {
return await this.submissionService.getSubmission(
id,
problemId,
req.user.id,
req.user.role,
groupId,
contestId
)
} catch (error) {
if (
(error instanceof Prisma.PrismaClientKnownRequestError &&
error.name == 'NotFoundError') ||
error instanceof EntityNotExistException
) {
throw new NotFoundException(error.message)
} else if (error instanceof ForbiddenAccessException) {
throw error.convert2HTTPException()
}
this.logger.error(error)
throw new InternalServerErrorException()
}
return await this.submissionService.getSubmission(
id,
problemId,
req.user.id,
req.user.role,
groupId,
contestId
)
}
}

@Controller('contest/:contestId/submission')
export class ContestSubmissionController {
private readonly logger = new Logger(ContestSubmissionController.name)

constructor(private readonly submissionService: SubmissionService) {}

@Get()
Expand All @@ -208,24 +143,13 @@ export class ContestSubmissionController {
@Query('problemId', new RequiredIntPipe('problemId')) problemId: number,
@Query('groupId', GroupIDPipe) groupId: number
) {
try {
return await this.submissionService.getContestSubmissions({
cursor,
take,
problemId,
contestId,
userId: req.user.id,
groupId
})
} catch (error) {
if (
error instanceof Prisma.PrismaClientKnownRequestError &&
error.name == 'NotFoundError'
) {
throw new NotFoundException(error.message)
}
this.logger.error(error)
throw new InternalServerErrorException()
}
return await this.submissionService.getContestSubmissions({
cursor,
take,
problemId,
contestId,
userId: req.user.id,
groupId
})
}
}
Loading

0 comments on commit 46e9322

Please sign in to comment.