Skip to content

Commit

Permalink
feat: fix create & update logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Lee-won-hyeok committed Feb 19, 2024
1 parent eb22bb7 commit 2c4a950
Show file tree
Hide file tree
Showing 24 changed files with 72 additions and 51 deletions.
11 changes: 5 additions & 6 deletions backend/apps/admin/src/announcement/announcement.resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,12 @@ export class AnnouncementResolver {

@Query(() => [Announcement], { name: 'getAnnouncementsByProblemId' })
async getAnnouncementsByProblemId(
@Args('problemId', { type: () => Int }) problemId: number
@Args('contestId', { type: () => Int }) contestId: number,
@Args('problemId', { type: () => Int, nullable: true }) problemId?: number
) {
try {
return await this.announcementService.getAnnouncementsByProblemId(
contestId,
problemId
)
} catch (error) {
Expand All @@ -61,13 +63,10 @@ export class AnnouncementResolver {
@Mutation(() => Announcement)
async updateAnnouncement(
@Args('id', { type: () => Int }) id: number,
@Args('announcementInput') announcementInput: AnnouncementInput
@Args('content', { type: () => String }) content: string
) {
try {
return await this.announcementService.updateAnnouncement(
id,
announcementInput
)
return await this.announcementService.updateAnnouncement(id, content)
} catch (error) {
if (error instanceof EntityNotExistException) {
throw error.convert2HTTPException()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ import { PrismaService } from '@libs/prisma'
import { AnnouncementService } from './announcement.service'

const problemId = faker.number.int()
const contestId = faker.number.int()
const id = faker.number.int()
const announcementInput = {
problemId,
contestId,
content: faker.string.sample()
}

Expand Down
30 changes: 21 additions & 9 deletions backend/apps/admin/src/announcement/announcement.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,40 +16,52 @@ export class AnnouncementService {
async createAnnouncement(announcementInput: AnnouncementInput) {
const announcement = await this.prisma.announcement.findFirst({
where: {
problemId: announcementInput.problemId,
...(announcementInput.problemId && {
problemId: announcementInput.problemId
}),
contestId: announcementInput.contestId,
content: announcementInput.content
}
})
if (announcement) throw new DuplicateFoundException('announcement')

await this.prisma.problem
await this.prisma.contestProblem
.findFirstOrThrow({
where: {
id: announcementInput.problemId
contestId: announcementInput.contestId,
...(announcementInput.problemId && {
problemId: announcementInput.problemId
})
}
})
.catch((error) => {
if (error.name == 'NotFoundError') {
throw new EntityNotExistException('problem')
throw new EntityNotExistException('contestProblem')
}
})

return await this.prisma.announcement.create({
data: {
problemId: announcementInput.problemId,
...(announcementInput.problemId && {
problemId: announcementInput.problemId
}),
contestId: announcementInput.contestId,
content: announcementInput.content
}
})
}

async getAnnouncementsByProblemId(problemId: number) {
//getAnnouncements
async getAnnouncementsByProblemId(contestId: number, problemId?: number) {
return await this.prisma.announcement.findMany({
where: {
problemId
...(problemId && { problemId }),
contestId
}
})
}

//getAnnouncementById
async getAnnouncement(id: number) {
const announcement = await this.prisma.announcement
.findFirstOrThrow({
Expand All @@ -65,11 +77,11 @@ export class AnnouncementService {
return announcement
}

async updateAnnouncement(id: number, announcementInput: AnnouncementInput) {
async updateAnnouncement(id: number, content: string) {
try {
return await this.prisma.announcement.update({
where: { id },
data: announcementInput
data: { content }
})
} catch (error) {
if (error instanceof PrismaClientKnownRequestError) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ import { InputType, Int, Field } from '@nestjs/graphql'

@InputType()
export class AnnouncementInput {
@Field({ nullable: true })
problemId?: number

@Field(() => Int)
problemId: number
contestId: number

@Field(() => String)
content: string
Expand Down
4 changes: 2 additions & 2 deletions backend/apps/admin/src/contest/contest.resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import {
ParseBoolPipe
} from '@nestjs/common'
import { Args, Context, Int, Mutation, Query, Resolver } from '@nestjs/graphql'
import { ContestProblem } from '@generated'
import { Contest } from '@generated'
import { PrismaClientKnownRequestError } from '@prisma/client/runtime/library'
import { AuthenticatedRequest, UseRolesGuard } from '@libs/auth'
import { OPEN_SPACE_ID } from '@libs/constants'
Expand All @@ -16,6 +14,8 @@ import {
UnprocessableDataException
} from '@libs/exception'
import { CursorValidationPipe, GroupIDPipe, RequiredIntPipe } from '@libs/pipe'
import { ContestProblem } from '@admin/@generated'
import { Contest } from '@admin/@generated'
import { ContestService } from './contest.service'
import { ContestWithParticipants } from './model/contest-with-participants.model'
import { CreateContestInput } from './model/contest.input'
Expand Down
6 changes: 3 additions & 3 deletions backend/apps/admin/src/contest/contest.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { CACHE_MANAGER } from '@nestjs/cache-manager'
import { Test, type TestingModule } from '@nestjs/testing'
import { ContestProblem, Group } from '@generated'
import { Problem } from '@generated'
import { Contest } from '@generated'
import { faker } from '@faker-js/faker'
import { Prisma } from '@prisma/client'
import type { Cache } from 'cache-manager'
import { expect } from 'chai'
import { stub } from 'sinon'
import { EntityNotExistException } from '@libs/exception'
import { PrismaService } from '@libs/prisma'
import { ContestProblem, Group } from '@admin/@generated'
import { Problem } from '@admin/@generated'
import { Contest } from '@admin/@generated'
import { ContestService } from './contest.service'
import type { ContestWithParticipants } from './model/contest-with-participants.model'
import type {
Expand Down
2 changes: 1 addition & 1 deletion backend/apps/admin/src/contest/contest.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {
Injectable,
UnprocessableEntityException
} from '@nestjs/common'
import type { Contest } from '@generated'
import type { ContestProblem } from '@prisma/client'
import { Cache } from 'cache-manager'
import {
Expand All @@ -18,6 +17,7 @@ import {
UnprocessableDataException
} from '@libs/exception'
import { PrismaService } from '@libs/prisma'
import type { Contest } from '@admin/@generated'
import type { CreateContestInput } from './model/contest.input'
import type { UpdateContestInput } from './model/contest.input'
import type { PublicizingRequest } from './model/publicizing-request.model'
Expand Down
2 changes: 1 addition & 1 deletion backend/apps/admin/src/group/group.resolver.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { InternalServerErrorException, Logger } from '@nestjs/common'
import { Args, Int, Query, Mutation, Resolver, Context } from '@nestjs/graphql'
import { Group } from '@generated'
import { Role } from '@prisma/client'
import { AuthenticatedRequest, UseRolesGuard } from '@libs/auth'
import {
Expand All @@ -9,6 +8,7 @@ import {
ForbiddenAccessException
} from '@libs/exception'
import { CursorValidationPipe, GroupIDPipe } from '@libs/pipe'
import { Group } from '@admin/@generated'
import { GroupService } from './group.service'
import { CreateGroupInput, UpdateGroupInput } from './model/group.input'
import { DeletedUserGroup, FindGroup } from './model/group.output'
Expand Down
4 changes: 2 additions & 2 deletions backend/apps/admin/src/group/group.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { CACHE_MANAGER } from '@nestjs/cache-manager'
import { Test, type TestingModule } from '@nestjs/testing'
import type { Group } from '@generated'
import type { User } from '@generated'
import { faker } from '@faker-js/faker'
import { Role } from '@prisma/client'
import type { Cache } from 'cache-manager'
Expand All @@ -15,6 +13,8 @@ import {
ForbiddenAccessException
} from '@libs/exception'
import { PrismaService } from '@libs/prisma'
import type { Group } from '@admin/@generated'
import type { User } from '@admin/@generated'
import { GroupService } from './group.service'

const userId = faker.number.int()
Expand Down
2 changes: 1 addition & 1 deletion backend/apps/admin/src/group/model/group.input.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Field } from '@nestjs/graphql'
import { InputType } from '@nestjs/graphql'
import { GroupCreateInput, GroupUpdateInput } from '@generated'
import { GroupCreateInput, GroupUpdateInput } from '@admin/@generated'

@InputType()
class Config {
Expand Down
2 changes: 1 addition & 1 deletion backend/apps/admin/src/group/model/group.output.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Field, Int } from '@nestjs/graphql'
import { ObjectType } from '@nestjs/graphql'
import { Group } from '@generated'
import { Group } from '@admin/@generated'

@ObjectType()
export class FindGroup extends Group {
Expand Down
2 changes: 1 addition & 1 deletion backend/apps/admin/src/notice/model/notice.input.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { InputType, PartialType, PickType } from '@nestjs/graphql'
import { NoticeCreateInput } from '@generated'
import { NoticeCreateInput } from '@admin/@generated'

@InputType()
export class CreateNoticeInput extends PickType(NoticeCreateInput, [
Expand Down
2 changes: 1 addition & 1 deletion backend/apps/admin/src/notice/notice.resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ import {
ResolveField,
Parent
} from '@nestjs/graphql'
import { Group, Notice, User } from '@generated'
import { AuthenticatedRequest } from '@libs/auth'
import { EntityNotExistException } from '@libs/exception'
import { CursorValidationPipe, GroupIDPipe, IDValidationPipe } from '@libs/pipe'
import { Group, Notice, User } from '@admin/@generated'
import { GroupService } from '@admin/group/group.service'
import { UserService } from '@admin/user/user.service'
import { CreateNoticeInput, UpdateNoticeInput } from './model/notice.input'
Expand Down
2 changes: 1 addition & 1 deletion backend/apps/admin/src/notice/notice.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { Test, type TestingModule } from '@nestjs/testing'
import type { Notice } from '@generated'
import { faker } from '@faker-js/faker'
import { PrismaClientKnownRequestError } from '@prisma/client/runtime/library'
import { expect } from 'chai'
import { stub } from 'sinon'
import { EntityNotExistException } from '@libs/exception'
import { PrismaService } from '@libs/prisma'
import type { Notice } from '@admin/@generated'
import type { CreateNoticeInput, UpdateNoticeInput } from './model/notice.input'
import { NoticeService } from './notice.service'

Expand Down
4 changes: 2 additions & 2 deletions backend/apps/admin/src/problem/mock/mock.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Language, Level } from '@generated'
import type { Problem } from '@generated'
import { faker } from '@faker-js/faker'
import { createReadStream } from 'fs'
import { Language, Level } from '@admin/@generated'
import type { Problem } from '@admin/@generated'
import type { FileUploadDto } from '../dto/file-upload.dto'
import type { UploadFileInput } from '../model/problem.input'
import type { Template } from '../model/template.input'
Expand Down
2 changes: 1 addition & 1 deletion backend/apps/admin/src/problem/model/problem.input.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Field, InputType, Int } from '@nestjs/graphql'
import { Language, Level } from '@generated'
import { ValidatePromise } from 'class-validator'
import { GraphQLUpload } from 'graphql-upload'
import { Language, Level } from '@admin/@generated'
import type { FileUploadDto } from '../dto/file-upload.dto'
import { Template } from './template.input'
import { Testcase } from './testcase.input'
Expand Down
2 changes: 1 addition & 1 deletion backend/apps/admin/src/problem/model/template.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 } from '@generated'
import { Language } from '@admin/@generated'

@InputType()
class Snippet {
Expand Down
7 changes: 6 additions & 1 deletion backend/apps/admin/src/problem/problem.resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
ValidationPipe
} from '@nestjs/common'
import { Args, Context, Query, Int, Mutation, Resolver } from '@nestjs/graphql'
import { ContestProblem, Problem, Tag, WorkbookProblem } from '@generated'
import { Prisma } from '@prisma/client'
import { AuthenticatedRequest } from '@libs/auth'
import { OPEN_SPACE_ID } from '@libs/constants'
Expand All @@ -18,6 +17,12 @@ import {
UnprocessableDataException
} from '@libs/exception'
import { CursorValidationPipe, GroupIDPipe, RequiredIntPipe } from '@libs/pipe'
import {
ContestProblem,
Problem,
Tag,
WorkbookProblem
} from '@admin/@generated'
import {
CreateProblemInput,
UploadFileInput,
Expand Down
14 changes: 7 additions & 7 deletions backend/apps/admin/src/problem/problem.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import { ConfigService } from '@nestjs/config'
import { Test, type TestingModule } from '@nestjs/testing'
import type {
Workbook,
WorkbookProblem,
Contest,
ContestProblem
} from '@generated'
import { Level } from '@generated'
import { expect } from 'chai'
import { spy, stub } from 'sinon'
import {
EntityNotExistException,
UnprocessableDataException
} from '@libs/exception'
import { PrismaService } from '@libs/prisma'
import type {
Workbook,
WorkbookProblem,
Contest,
ContestProblem
} from '@admin/@generated'
import { Level } from '@admin/@generated'
import { S3Provider } from '@admin/storage/s3.provider'
import { StorageService } from '@admin/storage/storage.service'
import {
Expand Down
8 changes: 4 additions & 4 deletions backend/apps/admin/src/problem/problem.service.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { Injectable } from '@nestjs/common'
import { Language } from '@generated'
import type { ContestProblem, Tag, WorkbookProblem } from '@generated'
import { Level } from '@generated'
import type { ProblemWhereInput } from '@generated'
import { Workbook } from 'exceljs'
import {
DuplicateFoundException,
UnprocessableDataException,
UnprocessableFileDataException
} from '@libs/exception'
import { PrismaService } from '@libs/prisma'
import { Language } from '@admin/@generated'
import type { ContestProblem, Tag, WorkbookProblem } from '@admin/@generated'
import { Level } from '@admin/@generated'
import type { ProblemWhereInput } from '@admin/@generated'
import { StorageService } from '@admin/storage/storage.service'
import { ImportedProblemHeader } from './model/problem.constants'
import type {
Expand Down
4 changes: 2 additions & 2 deletions backend/apps/admin/src/user/user.resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import {
NotFoundException
} from '@nestjs/common'
import { Resolver, Query, Mutation, Args, Int } from '@nestjs/graphql'
import { UserGroup } from '@generated'
import { User } from '@generated'
import { OPEN_SPACE_ID } from '@libs/constants'
import { CursorValidationPipe, GroupIDPipe, RequiredIntPipe } from '@libs/pipe'
import { UserGroup } from '@admin/@generated'
import { User } from '@admin/@generated'
import { GroupMember } from './model/groupMember.model'
import { UserService } from './user.service'

Expand Down
2 changes: 1 addition & 1 deletion backend/apps/admin/src/user/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ import {
Injectable,
NotFoundException
} from '@nestjs/common'
import type { UserGroup } from '@generated'
import { Role } from '@prisma/client'
import { Cache } from 'cache-manager'
import { joinGroupCacheKey } from '@libs/cache'
import { JOIN_GROUP_REQUEST_EXPIRE_TIME } from '@libs/constants'
import { EntityNotExistException } from '@libs/exception'
import { PrismaService } from '@libs/prisma'
import type { GroupJoinRequest } from '@libs/types'
import type { UserGroup } from '@admin/@generated'

@Injectable()
export class UserService {
Expand Down
2 changes: 1 addition & 1 deletion backend/apps/client/src/problem/problem.repository.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/* eslint-disable @typescript-eslint/naming-convention */
import { Injectable } from '@nestjs/common'
import type { CodeDraftUpdateInput } from '@generated'
import type { Problem, Tag, CodeDraft, Prisma } from '@prisma/client'
import { PrismaService } from '@libs/prisma'
import type { CodeDraftUpdateInput } from '@admin/@generated'
import type { CreateTemplateDto } from './dto/create-code-draft.dto'
import type { ProblemOrder } from './schema/problem-order.schema'

Expand Down
Loading

0 comments on commit 2c4a950

Please sign in to comment.