Skip to content

Commit

Permalink
Merge branch 'main' into t939-refactor-client-notice-user-module
Browse files Browse the repository at this point in the history
  • Loading branch information
Jaehyeon1020 authored Oct 3, 2024
2 parents c01bd91 + f9eb23c commit 76594b2
Show file tree
Hide file tree
Showing 53 changed files with 4,845 additions and 4,346 deletions.
127 changes: 34 additions & 93 deletions apps/backend/apps/client/src/problem/problem.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,15 @@ import {
Controller,
DefaultValuePipe,
Get,
InternalServerErrorException,
Logger,
NotFoundException,
Param,
Query,
Req
} from '@nestjs/common'
import { Prisma } from '@prisma/client'
import {
AuthNotNeededIfOpenSpace,
UserNullWhenAuthFailedIfOpenSpace,
type AuthenticatedRequest
AuthenticatedRequest
} from '@libs/auth'
import {
EntityNotExistException,
ForbiddenAccessException
} from '@libs/exception'
import {
CursorValidationPipe,
GroupIDPipe,
Expand All @@ -35,8 +27,6 @@ import {

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

constructor(
private readonly problemService: ProblemService,
private readonly workbookProblemService: WorkbookProblemService
Expand All @@ -56,33 +46,22 @@ export class ProblemController {
order: ProblemOrder,
@Query('search') search?: string
) {
try {
if (!workbookId) {
return await this.problemService.getProblems({
userId: req.user?.id ?? null,
cursor,
take,
groupId,
order,
search
})
}
return await this.workbookProblemService.getWorkbookProblems(
workbookId!,
if (!workbookId) {
return await this.problemService.getProblems({
userId: req.user?.id ?? null,
cursor,
take,
groupId
)
} catch (error) {
if (
error instanceof Prisma.PrismaClientKnownRequestError &&
error.name === 'NotFoundError'
) {
throw new NotFoundException(error.message)
}
this.logger.error(error)
throw new InternalServerErrorException()
groupId,
order,
search
})
}
return await this.workbookProblemService.getWorkbookProblems(
workbookId!,
cursor,
take,
groupId
)
}

@Get(':problemId')
Expand All @@ -92,32 +71,19 @@ export class ProblemController {
@Query('workbookId', IDValidationPipe) workbookId: number | null,
@Param('problemId', new RequiredIntPipe('problemId')) problemId: number
) {
try {
if (!workbookId) {
return await this.problemService.getProblem(problemId, groupId)
}
return await this.workbookProblemService.getWorkbookProblem(
workbookId!,
problemId,
groupId
)
} catch (error) {
if (
error instanceof Prisma.PrismaClientKnownRequestError &&
error.name === 'NotFoundError'
) {
throw new NotFoundException(error.message)
}
this.logger.error(error)
throw new InternalServerErrorException()
if (!workbookId) {
return await this.problemService.getProblem(problemId, groupId)
}
return await this.workbookProblemService.getWorkbookProblem(
workbookId!,
problemId,
groupId
)
}
}

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

constructor(private readonly contestProblemService: ContestProblemService) {}

@Get()
Expand All @@ -129,24 +95,13 @@ export class ContestProblemController {
@Query('take', new DefaultValuePipe(10), new RequiredIntPipe('take'))
take: number
) {
try {
return await this.contestProblemService.getContestProblems(
contestId,
req.user.id,
cursor,
take,
groupId
)
} catch (error) {
if (
error instanceof EntityNotExistException ||
error instanceof ForbiddenAccessException
) {
throw error.convert2HTTPException()
}
this.logger.error(error)
throw new InternalServerErrorException()
}
return await this.contestProblemService.getContestProblems(
contestId,
req.user.id,
cursor,
take,
groupId
)
}

@Get(':problemId')
Expand All @@ -156,25 +111,11 @@ export class ContestProblemController {
@Param('problemId', new RequiredIntPipe('problemId')) problemId: number,
@Query('groupId', GroupIDPipe) groupId: number
) {
try {
return await this.contestProblemService.getContestProblem(
contestId,
problemId,
req.user.id,
groupId
)
} 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.contestProblemService.getContestProblem(
contestId,
problemId,
req.user.id,
groupId
)
}
}
79 changes: 58 additions & 21 deletions apps/backend/apps/client/src/problem/problem.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,21 @@ import {
type Problem,
type Tag,
type CodeDraft,
type Prisma,
Prisma,
ResultStatus
} from '@prisma/client'
import { Span } from 'nestjs-otel'
import { MIN_DATE } from '@libs/constants'
import {
ConflictFoundException,
EntityNotExistException,
UnprocessableDataException
} from '@libs/exception'
import { PrismaService } from '@libs/prisma'
import type { CodeDraftUpdateInput } from '@admin/@generated'
import type {
CodeDraftCreateInput,
CodeDraftUpdateInput
} from '@admin/@generated'
import type { CreateTemplateDto } from './dto/create-code-draft.dto'
import type { ProblemOrder } from './enum/problem-order.enum'

Expand Down Expand Up @@ -168,14 +176,18 @@ export class ProblemRepository {
problemId: number,
groupId: number
): Promise<Partial<Problem>> {
return await this.prisma.problem.findUniqueOrThrow({
const problem = await this.prisma.problem.findUnique({
where: {
id: problemId,
groupId,
visibleLockTime: MIN_DATE
},
select: this.problemSelectOption
})
if (!problem) {
throw new EntityNotExistException('Problem')
}
return problem
}

async getProblemsTags(tagIds: number[]) {
Expand Down Expand Up @@ -232,7 +244,7 @@ export class ProblemRepository {
}

async getContestProblem(contestId: number, problemId: number) {
return await this.prisma.contestProblem.findUniqueOrThrow({
const contestProblem = await this.prisma.contestProblem.findUnique({
where: {
// eslint-disable-next-line @typescript-eslint/naming-convention
contestId_problemId: {
Expand All @@ -247,6 +259,10 @@ export class ProblemRepository {
}
}
})
if (!contestProblem) {
throw new EntityNotExistException('ContestProblem')
}
return contestProblem
}

async getWorkbookProblems(
Expand Down Expand Up @@ -292,7 +308,7 @@ export class ProblemRepository {
}

async getWorkbookProblem(workbookId: number, problemId: number) {
return await this.prisma.workbookProblem.findUniqueOrThrow({
const workbookProblem = await this.prisma.workbookProblem.findUnique({
where: {
// eslint-disable-next-line @typescript-eslint/naming-convention
workbookId_problemId: {
Expand All @@ -310,13 +326,17 @@ export class ProblemRepository {
}
}
})
if (!workbookProblem) {
throw new EntityNotExistException('WorkbookProblem')
}
return workbookProblem
}

async getCodeDraft(
userId: number,
problemId: number
): Promise<Partial<CodeDraft>> {
return await this.prisma.codeDraft.findUniqueOrThrow({
const codeDraft = await this.prisma.codeDraft.findUnique({
where: {
codeDraftId: {
userId,
Expand All @@ -325,30 +345,47 @@ export class ProblemRepository {
},
select: this.codeDraftSelectOption
})
if (!codeDraft) {
throw new EntityNotExistException('CodeDraft')
}
return codeDraft
}

async upsertCodeDraft(
userId: number,
problemId: number,
template: CreateTemplateDto
): Promise<Partial<CodeDraft>> {
return await this.prisma.codeDraft.upsert({
where: {
codeDraftId: {
try {
return await this.prisma.codeDraft.upsert({
where: {
codeDraftId: {
userId,
problemId
}
},
update: {
template: template.template as CodeDraftUpdateInput['template']
},
create: {
userId,
problemId
problemId,
template: template.template as CodeDraftCreateInput['template']
},
select: this.codeDraftSelectOption
})
} catch (error) {
if (error instanceof Prisma.PrismaClientKnownRequestError) {
if (error.code === 'P2002') {
throw new ConflictFoundException('CodeDraft already exists.')
} else if (error.code === 'P2003') {
throw new EntityNotExistException('User or Problem')
} else {
throw new UnprocessableDataException('Invalid data provided.')
}
},
update: {
template: template.template as CodeDraftUpdateInput['template']
},
create: {
userId,
problemId,
template: template.template as CodeDraftUpdateInput['template']
},
select: this.codeDraftSelectOption
})
}
throw error
}
}

@Span()
Expand Down
Loading

0 comments on commit 76594b2

Please sign in to comment.