Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(be): apply global exception filter to admin contest and admin group modules #2099

Merged
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
210 changes: 41 additions & 169 deletions apps/backend/apps/admin/src/contest/contest.resolver.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,8 @@
import {
InternalServerErrorException,
Logger,
NotFoundException,
ParseBoolPipe
} from '@nestjs/common'
import { ParseBoolPipe } from '@nestjs/common'
import { Args, Context, Int, Mutation, Query, Resolver } from '@nestjs/graphql'
import { Contest, ContestProblem } from '@generated'
import { PrismaClientKnownRequestError } from '@prisma/client/runtime/library'
import { AuthenticatedRequest, UseRolesGuard } from '@libs/auth'
import { OPEN_SPACE_ID } from '@libs/constants'
import {
ConflictFoundException,
EntityNotExistException,
UnprocessableDataException
} from '@libs/exception'
import {
CursorValidationPipe,
GroupIDPipe,
Expand All @@ -34,7 +23,6 @@ import { UserContestScoreSummaryWithUserInfo } from './model/score-summary'

@Resolver(() => Contest)
export class ContestResolver {
private readonly logger = new Logger(ContestResolver.name)
constructor(private readonly contestService: ContestService) {}

@Query(() => [ContestWithParticipants])
Expand Down Expand Up @@ -62,16 +50,7 @@ export class ContestResolver {
@Args('contestId', { type: () => Int }, new RequiredIntPipe('contestId'))
contestId: number
) {
try {
return await this.contestService.getContest(contestId)
} catch (error) {
if (
error instanceof PrismaClientKnownRequestError &&
error.code == 'P2025'
) {
throw new NotFoundException(error.message)
}
}
return await this.contestService.getContest(contestId)
}

@Mutation(() => Contest)
Expand All @@ -85,57 +64,23 @@ export class ContestResolver {
groupId: number,
@Context('req') req: AuthenticatedRequest
) {
try {
return await this.contestService.createContest(
groupId,
req.user.id,
input
)
} catch (error) {
if (
error instanceof UnprocessableDataException ||
error instanceof EntityNotExistException
) {
throw error.convert2HTTPException()
}
this.logger.error(error)
throw new InternalServerErrorException()
}
return await this.contestService.createContest(groupId, req.user.id, input)
}

@Mutation(() => Contest)
async updateContest(
@Args('groupId', { type: () => Int }, GroupIDPipe) groupId: number,
@Args('input') input: UpdateContestInput
) {
try {
return await this.contestService.updateContest(groupId, input)
} catch (error) {
if (
error instanceof EntityNotExistException ||
error instanceof UnprocessableDataException
) {
throw error.convert2HTTPException()
}
this.logger.error(error)
throw new InternalServerErrorException()
}
return await this.contestService.updateContest(groupId, input)
}

@Mutation(() => Contest)
async deleteContest(
@Args('groupId', { type: () => Int }, GroupIDPipe) groupId: number,
@Args('contestId', { type: () => Int }) contestId: number
) {
try {
return await this.contestService.deleteContest(groupId, contestId)
} catch (error) {
if (error instanceof EntityNotExistException) {
throw error.convert2HTTPException()
}
this.logger.error(error)
throw new InternalServerErrorException()
}
return await this.contestService.deleteContest(groupId, contestId)
}

@Query(() => [PublicizingRequest])
Expand All @@ -149,21 +94,10 @@ export class ContestResolver {
@Args('groupId', { type: () => Int }, GroupIDPipe) groupId: number,
@Args('contestId', { type: () => Int }) contestId: number
) {
try {
return await this.contestService.createPublicizingRequest(
groupId,
contestId
)
} catch (error) {
if (
error instanceof EntityNotExistException ||
error instanceof ConflictFoundException
) {
throw error.convert2HTTPException()
}
this.logger.error(error)
throw new InternalServerErrorException()
}
return await this.contestService.createPublicizingRequest(
groupId,
contestId
)
}

@Mutation(() => PublicizingResponse)
Expand All @@ -172,18 +106,10 @@ export class ContestResolver {
@Args('contestId', { type: () => Int }) contestId: number,
@Args('isAccepted', ParseBoolPipe) isAccepted: boolean
) {
try {
return await this.contestService.handlePublicizingRequest(
contestId,
isAccepted
)
} catch (error) {
if (error instanceof EntityNotExistException) {
throw error.convert2HTTPException()
}
this.logger.error(error)
throw new InternalServerErrorException()
}
return await this.contestService.handlePublicizingRequest(
contestId,
isAccepted
)
}

@Mutation(() => [ContestProblem])
Expand All @@ -193,22 +119,11 @@ export class ContestResolver {
@Args('problemIdsWithScore', { type: () => [ProblemScoreInput] })
problemIdsWithScore: ProblemScoreInput[]
) {
try {
return await this.contestService.importProblemsToContest(
groupId,
contestId,
problemIdsWithScore
)
} catch (error) {
if (
error instanceof EntityNotExistException ||
error instanceof UnprocessableDataException
) {
throw error.convert2HTTPException()
}
this.logger.error(error)
throw new InternalServerErrorException()
}
return await this.contestService.importProblemsToContest(
groupId,
contestId,
problemIdsWithScore
)
}

@Mutation(() => [ContestProblem])
Expand All @@ -218,22 +133,11 @@ export class ContestResolver {
contestId: number,
@Args('problemIds', { type: () => [Int] }) problemIds: number[]
) {
try {
return await this.contestService.removeProblemsFromContest(
groupId,
contestId,
problemIds
)
} catch (error) {
if (
error instanceof EntityNotExistException ||
error instanceof UnprocessableDataException
) {
throw error.convert2HTTPException()
}
this.logger.error(error)
throw new InternalServerErrorException()
}
return await this.contestService.removeProblemsFromContest(
groupId,
contestId,
problemIds
)
}

/**
Expand All @@ -257,18 +161,13 @@ export class ContestResolver {
@Args('cursor', { nullable: true, type: () => Int }, CursorValidationPipe)
cursor: number | null
) {
try {
return await this.contestService.getContestSubmissionSummaryByUserId(
take,
contestId,
userId,
problemId,
cursor
)
} catch (error) {
this.logger.error(error)
throw new InternalServerErrorException()
}
return await this.contestService.getContestSubmissionSummaryByUserId(
take,
contestId,
userId,
problemId,
cursor
)
}

@Mutation(() => DuplicatedContestResponse)
Expand All @@ -278,22 +177,11 @@ export class ContestResolver {
contestId: number,
@Context('req') req: AuthenticatedRequest
) {
try {
return await this.contestService.duplicateContest(
groupId,
contestId,
req.user.id
)
} catch (error) {
if (
error instanceof UnprocessableDataException ||
error instanceof EntityNotExistException
) {
throw error.convert2HTTPException()
}
this.logger.error(error)
throw new InternalServerErrorException()
}
return await this.contestService.duplicateContest(
groupId,
contestId,
req.user.id
)
}

/**
Expand All @@ -308,33 +196,17 @@ export class ContestResolver {
@Args('contestId', { type: () => Int }) contestId: number,
@Args('cursor', { type: () => Int, nullable: true }) cursor: number | null
) {
try {
return await this.contestService.getContestScoreSummaries(
take,
contestId,
cursor
)
} catch (error) {
if (error instanceof EntityNotExistException) {
throw error.convert2HTTPException()
}
this.logger.error(error)
throw new InternalServerErrorException()
}
return await this.contestService.getContestScoreSummaries(
take,
contestId,
cursor
)
}

@Query(() => ContestsGroupedByStatus)
async getContestsByProblemId(
@Args('problemId', { type: () => Int }) problemId: number
) {
try {
return await this.contestService.getContestsByProblemId(problemId)
} catch (error) {
if (error instanceof EntityNotExistException) {
throw error.convert2HTTPException()
}
this.logger.error(error)
throw new InternalServerErrorException()
}
return await this.contestService.getContestsByProblemId(problemId)
}
}
Loading