Skip to content

Commit

Permalink
feat(be): merge client problem repository into service
Browse files Browse the repository at this point in the history
  • Loading branch information
mnseok committed Nov 12, 2024
1 parent 9904530 commit eb8c180
Show file tree
Hide file tree
Showing 8 changed files with 589 additions and 191 deletions.
2 changes: 1 addition & 1 deletion apps/backend/apps/admin/src/problem/model/problem.input.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Field, InputType, Int } from '@nestjs/graphql'
import { Language, Level } from '@generated'
import { Language, Level } from '@prisma/client'
import { ValidatePromise } from 'class-validator'
import { GraphQLUpload } from 'graphql-upload'
import type { FileUploadDto } from '../dto/file-upload.dto'
Expand Down
37 changes: 20 additions & 17 deletions apps/backend/apps/admin/src/problem/problem.service.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { CACHE_MANAGER } from '@nestjs/cache-manager'
import { Cache } from '@nestjs/cache-manager'
import {
Inject,
Injectable,
InternalServerErrorException
} from '@nestjs/common'
import { Inject, Injectable } from '@nestjs/common'
import { ConfigService } from '@nestjs/config'
import { Language } from '@generated'
import type { ContestProblem, Problem, Tag, WorkbookProblem } from '@generated'
import { Level } from '@generated'
import type { ProblemWhereInput } from '@generated'
import { Prisma } from '@prisma/client'
import {
Language,
Level,
Prisma,
type ContestProblem,
type Problem,
type Tag,
type WorkbookProblem
} from '@prisma/client'
import { PrismaClientKnownRequestError } from '@prisma/client/runtime/library'
import { randomUUID } from 'crypto'
import { Workbook } from 'exceljs'
Expand All @@ -23,6 +23,7 @@ import {
UnprocessableFileDataException
} from '@libs/exception'
import { PrismaService } from '@libs/prisma'
import type { ProblemWhereInput } from '@admin/@generated'
import { StorageService } from '@admin/storage/storage.service'
import { ImportedProblemHeader } from './model/problem.constants'
import type {
Expand Down Expand Up @@ -56,10 +57,12 @@ export class ProblemService {
'A problem should support at least one language'
)
}
template.forEach((template) => {
if (!languages.includes(template.language)) {

// Check if the problem supports the language in the template
template.forEach((template: Template) => {
if (!languages.includes(template.language as Language)) {
throw new UnprocessableDataException(
`This problem does not support ${template.language}`
`This problem does not support ${template.language as Language}`
)
}
})
Expand Down Expand Up @@ -397,9 +400,9 @@ export class ProblemService {
}
const supportedLangs = languages ?? problem.languages
template?.forEach((template) => {
if (!supportedLangs.includes(template.language)) {
if (!supportedLangs.includes(template.language as Language)) {
throw new UnprocessableDataException(
`This problem does not support ${template.language}`
`This problem does not support ${template.language as Language}`
)
}
})
Expand Down Expand Up @@ -646,7 +649,7 @@ export class ProblemService {
* @throws DuplicateFoundException - 이미 존재하는 태그일 경우
*/
async createTag(tagName: string): Promise<Tag> {
// throw error if tag already exists
// 존재하는 태그일 경우 에러를 throw합니다
try {
return await this.prisma.tag.create({
data: {
Expand All @@ -660,7 +663,7 @@ export class ProblemService {
)
throw new DuplicateFoundException('tag')

throw new InternalServerErrorException(error)
throw error
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import type { CreateTemplateDto } from './create-code-draft.dto'
import type { JsonValue } from '@prisma/client/runtime/library'

export class CodeDraftResponseDto {
userId: string
problemId: string
template: CreateTemplateDto
createTime: string
updateTime: string
userId: number
problemId: number
template: JsonValue
createTime: Date
updateTime: Date
}
80 changes: 50 additions & 30 deletions apps/backend/apps/client/src/problem/dto/problem.response.dto.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,53 @@
import {
type Language,
Level,
type ProblemTestcase,
type Tag
} from '@prisma/client'
import { Exclude, Expose } from 'class-transformer'
import type { Language, Level, ProblemTestcase, Tag } from '@prisma/client'
import type { JsonValue } from '@prisma/client/runtime/library'

@Exclude()
export class ProblemResponseDto {
@Expose() id: number
@Expose() title: string
@Expose() description: string
@Expose() inputDescription: string
@Expose() outputDescription: string
@Expose() hint: string
@Expose() engTitle: string
@Expose() engDescription: string
@Expose() engInputDescription: string
@Expose() engOutputDescription: string
@Expose() engHint: string
@Expose() languages: Language[]
@Expose() timeLimit: number
@Expose() memoryLimit: number
@Expose() difficulty: Level
@Expose() source: string[]
@Expose() submissionCount: number
@Expose() acceptedCount: number
@Expose() acceptedRate: number
@Expose() tags: Partial<Tag>[]
@Expose() template: JSON[]
@Expose() problemTestcase: Pick<ProblemTestcase, 'id' | 'input' | 'output'>[]
id: number
title: string
description: string
inputDescription: string
outputDescription: string
hint: string
engTitle: string | null
engDescription: string | null
engInputDescription: string | null
engOutputDescription: string | null
engHint: string | null
languages: Language[]
timeLimit: number
memoryLimit: number
difficulty: Level
source: string
submissionCount: number
acceptedCount: number
acceptedRate: number
tags: Partial<Tag>[]
template: JsonValue[]
problemTestcase: Pick<ProblemTestcase, 'id' | 'input' | 'output'>[]
}

// @Exclude()
// export class ProblemResponseDto {
// @Expose() id: number
// @Expose() title: string
// @Expose() description: string
// @Expose() inputDescription: string
// @Expose() outputDescription: string
// @Expose() hint: string
// @Expose() engTitle: string
// @Expose() engDescription: string
// @Expose() engInputDescription: string
// @Expose() engOutputDescription: string
// @Expose() engHint: string
// @Expose() languages: Language[]
// @Expose() timeLimit: number
// @Expose() memoryLimit: number
// @Expose() difficulty: Level
// @Expose() source: string[]
// @Expose() submissionCount: number
// @Expose() acceptedCount: number
// @Expose() acceptedRate: number
// @Expose() tags: Partial<Tag>[]
// @Expose() template: JSON[]
// @Expose() problemTestcase: Pick<ProblemTestcase, 'id' | 'input' | 'output'>[]
// }
28 changes: 2 additions & 26 deletions apps/backend/apps/client/src/problem/dto/problems.response.dto.ts
Original file line number Diff line number Diff line change
@@ -1,42 +1,18 @@
import { Level, type Language, type Tag } from '@prisma/client'
import { Exclude, Expose, Type } from 'class-transformer'
import type { Language, Level, Tag } from '@prisma/client'

@Exclude()
export class ProblemsResponseDto {
@Expose()
@Type(() => Problem)
data: Problem[]

@Expose()
total: number
}

@Exclude()
class Problem {
@Expose()
id: number

@Expose()
title: string

@Expose()
engTitle: string

@Expose()
engTitle: string | null
difficulty: Level

@Expose()
submissionCount: number

@Expose()
acceptedRate: number

@Expose()
tags: Partial<Tag>[]

@Expose()
languages: Language[]

@Expose()
hasPassed: boolean | null
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
import { Problem } from '@prisma/client'
import { Exclude, Expose, Type } from 'class-transformer'
import { ProblemResponseDto } from './problem.response.dto'
import type { ProblemResponseDto } from './problem.response.dto'

@Exclude()
export class RelatedProblemResponseDto {
@Expose()
order: number

@Expose()
@Type(() => ProblemResponseDto)
problem: Problem
problem: ProblemResponseDto
}
Original file line number Diff line number Diff line change
@@ -1,51 +1,66 @@
import { Level } from '@prisma/client'
import { Exclude, Expose, Transform, Type } from 'class-transformer'
import { IsOptional } from 'class-validator'
import type { Level } from '@prisma/client'

@Exclude()
export class RelatedProblemsResponseDto {
@Expose()
@Type(() => Problem)
data: Problem[]

@Expose()
total: number
}

@Exclude()
// @Exclude()
// export class RelatedProblemsResponseDto {
// @Expose()
// @Type(() => Problem)
// data: Problem[]

// @Expose()
// total: number
// }

class Problem {
@Expose()
order: number

@Expose()
@Transform(({ obj }) => obj.problem.id, { toClassOnly: true })
id: number

@Expose()
@Transform(({ obj }) => obj.problem.title, { toClassOnly: true })
title: number

@Expose()
@Transform(({ obj }) => obj.problem.difficulty, { toClassOnly: true })
title: string
difficulty: Level

@Expose()
@Transform(({ obj }) => obj.problem.submissionCount, { toClassOnly: true })
submissionCount: number

@Expose()
@Transform(({ obj }) => obj.problem.acceptedRate, { toClassOnly: true })
acceptedRate: number

@Expose()
@IsOptional()
maxScore: number | null

@Expose()
@IsOptional()
score: string | null

@Expose()
@IsOptional()
submissionTime: Date | null
}

// @Exclude()
// class Problem {
// @Expose()
// order: number

// @Expose()
// @Transform(({ obj }) => obj.problem.id, { toClassOnly: true })
// id: number

// @Expose()
// @Transform(({ obj }) => obj.problem.title, { toClassOnly: true })
// title: number

// @Expose()
// @Transform(({ obj }) => obj.problem.difficulty, { toClassOnly: true })
// difficulty: Level

// @Expose()
// @Transform(({ obj }) => obj.problem.submissionCount, { toClassOnly: true })
// submissionCount: number

// @Expose()
// @Transform(({ obj }) => obj.problem.acceptedRate, { toClassOnly: true })
// acceptedRate: number

// @Expose()
// @IsOptional()
// maxScore: number | null

// @Expose()
// @IsOptional()
// score: string | null

// @Expose()
// @IsOptional()
// submissionTime: Date | null
// }
Loading

0 comments on commit eb8c180

Please sign in to comment.