Skip to content

Commit c779d44

Browse files
authored
Merge pull request #112 from ChooseTale/fix/create-game
Fix/create game
2 parents 23e889d + 2be9b9a commit c779d44

File tree

50 files changed

+695
-201
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+695
-201
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/*
2+
Warnings:
3+
4+
- You are about to drop the column `description` on the `ChoicePage` table. All the data in the column will be lost.
5+
6+
*/
7+
-- AlterTable
8+
ALTER TABLE "ChoicePage" DROP COLUMN "description";
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/*
2+
Warnings:
3+
4+
- Made the column `thumbnailId` on table `Game` required. This step will fail if there are existing NULL values in that column.
5+
6+
*/
7+
-- DropForeignKey
8+
ALTER TABLE "Game" DROP CONSTRAINT "Game_thumbnailId_fkey";
9+
10+
-- AlterTable
11+
ALTER TABLE "Game" ALTER COLUMN "thumbnailId" SET NOT NULL;
12+
13+
-- AddForeignKey
14+
ALTER TABLE "Game" ADD CONSTRAINT "Game_thumbnailId_fkey" FOREIGN KEY ("thumbnailId") REFERENCES "Image"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
-- DropForeignKey
2+
ALTER TABLE "Game" DROP CONSTRAINT "Game_thumbnailId_fkey";
3+
4+
-- AlterTable
5+
ALTER TABLE "Game" ALTER COLUMN "thumbnailId" DROP NOT NULL;
6+
7+
-- AddForeignKey
8+
ALTER TABLE "Game" ADD CONSTRAINT "Game_thumbnailId_fkey" FOREIGN KEY ("thumbnailId") REFERENCES "Image"("id") ON DELETE SET NULL ON UPDATE CASCADE;

prisma/schema.prisma

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,9 +151,9 @@ model ChoicePage {
151151
game Game @relation(fields: [gameId], references: [id])
152152
gameId Int @default(1)
153153
154-
title String
155-
description String
156-
order Int
154+
title String
155+
156+
order Int
157157
158158
UserChoice UserChoice[]
159159
createdAt DateTime @default(now())

src/common/infrastructure/repositories/page/port/page.repository.interface.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Page } from '@prisma/client';
2-
2+
import { Prisma } from '@prisma/client';
33
export interface PageRepositoryPort {
4+
getAll(query: Prisma.PageFindManyArgs): Promise<Page[]>;
45
getAllByGameId(gameId: number): Promise<Page[]>;
56
getStartPageByGameId(gameId: number): Promise<Page>;
67
getByIdOrThrow(id: number): Promise<Page>;

src/common/infrastructure/repositories/page/repository/page.repository.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
import { Injectable, NotFoundException } from '@nestjs/common';
22
import { PageRepositoryPort } from '../port/page.repository.interface';
33
import { PrismaService } from '@@prisma/prisma.service';
4-
import { Page } from '@prisma/client';
4+
import { Page, Prisma } from '@prisma/client';
55

66
@Injectable()
77
export class PageRepository implements PageRepositoryPort {
88
constructor(private readonly prismaService: PrismaService) {}
99

10+
async getAll(query: Prisma.PageFindManyArgs): Promise<Page[]> {
11+
return this.prismaService.page.findMany(query);
12+
}
13+
1014
async getAllByGameId(gameId: number): Promise<Page[]> {
1115
return this.prismaService.page.findMany({
1216
where: {

src/common/kafka/chat-gpt/port/input/chat-gpt.service.interface.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ export interface IChatGPTKafkaPort {
66
}[];
77
choices: {
88
title: string;
9-
description: string;
109
}[];
1110
}): Promise<void>;
1211
}

src/game-builder/choice/applications/controllers/choice.controller.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ export class ChoiceController {
4848
@Param('gameId', ParseIntPipe) gameId: number,
4949
@Body() body: CreateChoiceReqDto,
5050
): Promise<CreateChoiceResDto> {
51-
console.log(body);
5251
return await this.createChoiceUsecase.execute(gameId, body);
5352
}
5453

@@ -68,7 +67,6 @@ export class ChoiceController {
6867
@Param('choiceId', ParseIntPipe) choiceId: number,
6968
@Body() body: UpdateChoiceReqDto,
7069
): Promise<UpdateChoiceResDto> {
71-
console.log(body);
7270
return await this.updateChoiceUsecase.execute(gameId, choiceId, body);
7371
}
7472

src/game-builder/choice/applications/controllers/dto/update-choice.dto.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,6 @@ export class UpdateChoiceReqDto {
99
@IsNotEmpty()
1010
title: string;
1111

12-
@IsString()
13-
description: string;
14-
1512
@IsNumber()
1613
@IsNotEmpty()
1714
parentPageId: number;
@@ -24,7 +21,7 @@ export class UpdateChoiceReqDto {
2421
export class UpdateChoiceResDto {
2522
id: number;
2623
title: string;
27-
description: string;
24+
2825
parentPageId: number;
2926
childPageId: number | null;
3027
}

src/game-builder/choice/applications/usecases/update-choice.usecase.ts

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
import { PrismaService } from '@@prisma/prisma.service';
2-
import { Inject, Injectable, NotFoundException } from '@nestjs/common';
3-
import { CreateChoiceReqDto } from '../controllers/dto/create-choice.dto';
2+
import {
3+
ConflictException,
4+
Inject,
5+
Injectable,
6+
NotFoundException,
7+
} from '@nestjs/common';
8+
49
import { IChoiceService } from '../../domain/port/input/choice.service.interface';
510
import { IGameService } from '@@src/game-builder/game/domain/ports/input/game.service.interface';
611
import { UpdateChoiceReqDto } from '../controllers/dto/update-choice.dto';
712
import { IPageService } from '@@src/game-builder/page/domain/ports/input/page.service.interface';
13+
import { toGetAllResMapper } from '@@src/game-builder/game/application/usecases/mapper/to-get-all-res.mapper';
814

915
@Injectable()
1016
export class UpdateChoiceUseCase {
@@ -22,12 +28,42 @@ export class UpdateChoiceUseCase {
2228
) {
2329
const game = await this.gameService.getById(gameId);
2430
if (!game) throw new NotFoundException(`game is null`);
31+
const pages = await this.pageService.getAllByGameId(gameId);
32+
const choices = await this.choiceService.getAllByPageIds(
33+
pages.map((page) => page.id),
34+
);
35+
if (!game) {
36+
throw new NotFoundException('Game not found');
37+
}
38+
39+
const gameTrees = toGetAllResMapper(game, pages, choices);
2540

26-
const parentPage = await this.pageService.getOneById(
27-
createChoiceReqDto.parentPageId,
41+
const parentPage = gameTrees.pages.find(
42+
(page) => page.id === createChoiceReqDto.parentPageId,
2843
);
44+
2945
if (!parentPage) throw new NotFoundException(`parentPage is null`);
30-
parentPage.checkIsEnding();
46+
47+
const pageChoice = await this.choiceService.getAllByToPageId(parentPage.id);
48+
49+
if (pageChoice.length < 1 && !parentPage.isStarting) {
50+
throw new ConflictException(
51+
'부모 페이지가 없으면 선택지를 만들 수 없습니다.',
52+
);
53+
}
54+
55+
if (parentPage.isEnding) {
56+
throw new ConflictException('종료 페이지는 선택지를 만들 수 없습니다.');
57+
}
58+
59+
if (
60+
createChoiceReqDto.childPageId &&
61+
parentPage.fromPageIds.includes(createChoiceReqDto.childPageId)
62+
) {
63+
throw new ConflictException(
64+
'자식 페이지는 부모 페이지가 될 수 없습니다.',
65+
);
66+
}
3167

3268
if (createChoiceReqDto.childPageId) {
3369
const childPage = await this.pageService.getOneById(
@@ -44,7 +80,6 @@ export class UpdateChoiceUseCase {
4480
return {
4581
id: newChoice.id,
4682
title: newChoice.title,
47-
description: newChoice.description,
4883
parentPageId: newChoice.parentPageId,
4984
childPageId: newChoice.childPageId,
5085
};

0 commit comments

Comments
 (0)