diff --git a/apps/backend/apps/client/src/assignment/assignment.controller.ts b/apps/backend/apps/client/src/assignment/assignment.controller.ts index 90a9463b1..47be75910 100644 --- a/apps/backend/apps/client/src/assignment/assignment.controller.ts +++ b/apps/backend/apps/client/src/assignment/assignment.controller.ts @@ -1,27 +1,18 @@ import { Controller, - InternalServerErrorException, - NotFoundException, Param, Post, Req, Get, Query, - Logger, DefaultValuePipe, Delete } from '@nestjs/common' -import { Prisma } from '@prisma/client' import { AuthNotNeededIfOpenSpace, AuthenticatedRequest, UserNullWhenAuthFailedIfOpenSpace } from '@libs/auth' -import { - ConflictFoundException, - EntityNotExistException, - ForbiddenAccessException -} from '@libs/exception' import { CursorValidationPipe, GroupIDPipe, @@ -32,8 +23,6 @@ import { AssignmentService } from './assignment.service' @Controller('assignment') export class AssignmentController { - private readonly logger = new Logger(AssignmentController.name) - constructor(private readonly assignmentService: AssignmentService) {} @Get('ongoing-upcoming') @@ -41,12 +30,7 @@ export class AssignmentController { async getOngoingUpcomingAssignments( @Query('groupId', GroupIDPipe) groupId: number ) { - try { - return await this.assignmentService.getAssignmentsByGroupId(groupId) - } catch (error) { - this.logger.error(error) - throw new InternalServerErrorException() - } + return await this.assignmentService.getAssignmentsByGroupId(groupId) } @Get('ongoing-upcoming-with-registered') @@ -54,15 +38,10 @@ export class AssignmentController { @Req() req: AuthenticatedRequest, @Query('groupId', GroupIDPipe) groupId: number ) { - try { - return await this.assignmentService.getAssignmentsByGroupId( - groupId, - req.user.id - ) - } catch (error) { - this.logger.error(error) - throw new InternalServerErrorException() - } + return await this.assignmentService.getAssignmentsByGroupId( + groupId, + req.user.id + ) } @Get('finished') @@ -75,18 +54,13 @@ export class AssignmentController { take: number, @Query('search') search?: string ) { - try { - return await this.assignmentService.getFinishedAssignmentsByGroupId( - req.user?.id, - cursor, - take, - groupId, - search - ) - } catch (error) { - this.logger.error(error) - throw new InternalServerErrorException() - } + return await this.assignmentService.getFinishedAssignmentsByGroupId( + req.user?.id, + cursor, + take, + groupId, + search + ) } @Get('registered-finished') @@ -98,18 +72,13 @@ export class AssignmentController { take: number, @Query('search') search?: string ) { - try { - return await this.assignmentService.getRegisteredFinishedAssignments( - cursor, - take, - groupId, - req.user.id, - search - ) - } catch (error) { - this.logger.error(error) - throw new InternalServerErrorException() - } + return await this.assignmentService.getRegisteredFinishedAssignments( + cursor, + take, + groupId, + req.user.id, + search + ) } @Get('registered-ongoing-upcoming') @@ -118,16 +87,11 @@ export class AssignmentController { @Query('groupId', GroupIDPipe) groupId: number, @Query('search') search?: string ) { - try { - return await this.assignmentService.getRegisteredOngoingUpcomingAssignments( - groupId, - req.user.id, - search - ) - } catch (error) { - this.logger.error(error) - throw new InternalServerErrorException() - } + return await this.assignmentService.getRegisteredOngoingUpcomingAssignments( + groupId, + req.user.id, + search + ) } @Get(':id') @@ -137,19 +101,7 @@ export class AssignmentController { @Query('groupId', GroupIDPipe) groupId: number, @Param('id', new RequiredIntPipe('id')) id: number ) { - try { - return await this.assignmentService.getAssignment( - id, - groupId, - req.user?.id - ) - } catch (error) { - if (error instanceof EntityNotExistException) { - throw error.convert2HTTPException() - } - this.logger.error(error) - throw new InternalServerErrorException() - } + return await this.assignmentService.getAssignment(id, groupId, req.user?.id) } @Post(':id/participation') @@ -159,25 +111,12 @@ export class AssignmentController { @Param('id', IDValidationPipe) assignmentId: number, @Query('invitationCode') invitationCode?: string ) { - try { - return await this.assignmentService.createAssignmentRecord( - assignmentId, - req.user.id, - invitationCode, - groupId - ) - } catch (error) { - if ( - error instanceof Prisma.PrismaClientKnownRequestError && - error.name === 'NotFoundError' - ) { - throw new NotFoundException(error.message) - } else if (error instanceof ConflictFoundException) { - throw error.convert2HTTPException() - } - this.logger.error(error) - throw new InternalServerErrorException(error.message) - } + return await this.assignmentService.createAssignmentRecord( + assignmentId, + req.user.id, + invitationCode, + groupId + ) } // unregister only for upcoming Assignment @@ -187,21 +126,10 @@ export class AssignmentController { @Query('groupId', GroupIDPipe) groupId: number, @Param('id', IDValidationPipe) assignmentId: number ) { - try { - return await this.assignmentService.deleteAssignmentRecord( - assignmentId, - req.user.id, - groupId - ) - } catch (error) { - if ( - error instanceof ForbiddenAccessException || - error instanceof EntityNotExistException - ) { - throw error.convert2HTTPException() - } - this.logger.error(error) - throw new InternalServerErrorException(error.message) - } + return await this.assignmentService.deleteAssignmentRecord( + assignmentId, + req.user.id, + groupId + ) } } diff --git a/apps/backend/apps/client/src/assignment/assignment.service.ts b/apps/backend/apps/client/src/assignment/assignment.service.ts index 1d3ba116f..52aac2b29 100644 --- a/apps/backend/apps/client/src/assignment/assignment.service.ts +++ b/apps/backend/apps/client/src/assignment/assignment.service.ts @@ -467,31 +467,23 @@ export class AssignmentService { userId: number, groupId = OPEN_SPACE_ID ) { - let assignment - try { - assignment = await this.prisma.assignment.findUniqueOrThrow({ + const [assignment, assignmentRecord] = await Promise.all([ + this.prisma.contest.findUnique({ where: { id: assignmentId, groupId } - }) - } catch (err) { - if ( - err instanceof Prisma.PrismaClientKnownRequestError && - err.code === 'P2025' - ) { - throw new EntityNotExistException('Assignment') - } - } - try { - await this.prisma.assignmentRecord.findFirstOrThrow({ + }), + this.prisma.assignmentRecord.findFirst({ where: { userId, assignmentId } }) - } catch (err) { - if ( - err instanceof Prisma.PrismaClientKnownRequestError && - err.code === 'P2025' - ) { - throw new EntityNotExistException('AssignmentRecord') - } + ]) + + if (!assignment) { + throw new EntityNotExistException('Contest') + } + + if (!assignmentRecord) { + throw new EntityNotExistException('ContestRecord') } + const now = new Date() if (now >= assignment.startTime) { throw new ForbiddenAccessException( @@ -499,18 +491,9 @@ export class AssignmentService { ) } - try { - return await this.prisma.assignmentRecord.delete({ - // eslint-disable-next-line @typescript-eslint/naming-convention - where: { assignmentId_userId: { assignmentId, userId } } - }) - } catch (err) { - if ( - err instanceof Prisma.PrismaClientKnownRequestError && - err.code === 'P2025' - ) { - throw new EntityNotExistException('AssignmentRecord') - } - } + return await this.prisma.assignmentRecord.delete({ + // eslint-disable-next-line @typescript-eslint/naming-convention + where: { assignmentId_userId: { assignmentId, userId } } + }) } }