From 4b09cbc8249edba25383aee47ae5f44cba0fd698 Mon Sep 17 00:00:00 2001 From: SungHyun Do <52828205+glaxyt@users.noreply.github.com> Date: Wed, 4 Dec 2024 15:13:32 +0900 Subject: [PATCH] =?UTF-8?q?[BE=20-#162=20]=20=ED=81=B4=EB=9E=98=EC=8A=A4?= =?UTF-8?q?=20=EC=82=AD=EC=A0=9C=20API:=20=EB=B2=8C=ED=81=AC=20=EC=82=AD?= =?UTF-8?q?=EC=A0=9C=EB=A1=9C=20=EB=B3=80=EA=B2=BD=20(#161)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat: 삭제 기능 벌크 삭제로 변경 * refactor: 선택지 엔티티 명시적 매핑 추가 --- .../quiz/quizzes/entities/choice.entity.ts | 10 +++--- .../src/module/quiz/quizzes/quiz.service.ts | 8 +---- .../quizzes/repositories/class.repository.ts | 36 ++++++------------- 3 files changed, 17 insertions(+), 37 deletions(-) diff --git a/packages/server/src/module/quiz/quizzes/entities/choice.entity.ts b/packages/server/src/module/quiz/quizzes/entities/choice.entity.ts index cc33311e..36637eb2 100644 --- a/packages/server/src/module/quiz/quizzes/entities/choice.entity.ts +++ b/packages/server/src/module/quiz/quizzes/entities/choice.entity.ts @@ -1,7 +1,7 @@ import { Column, Entity, JoinColumn, ManyToOne, PrimaryGeneratedColumn } from 'typeorm'; import { Quiz } from './quiz.entity'; -@Entity() +@Entity('choice') export class Choice { @PrimaryGeneratedColumn() id: number; @@ -18,10 +18,10 @@ export class Choice { @Column() position: number; - @Column({ name: 'created_at' }) + @Column({ name: 'created_at' }) createdAt: Date; - @ManyToOne(() => Quiz, quiz => quiz.choices) - @JoinColumn({ name: 'quiz_id' }) + @ManyToOne(() => Quiz, (quiz) => quiz.choices) + @JoinColumn({ name: 'quiz_id', referencedColumnName: 'id' }) quiz: Quiz; -} \ No newline at end of file +} diff --git a/packages/server/src/module/quiz/quizzes/quiz.service.ts b/packages/server/src/module/quiz/quizzes/quiz.service.ts index 9736b3df..21693aa7 100644 --- a/packages/server/src/module/quiz/quizzes/quiz.service.ts +++ b/packages/server/src/module/quiz/quizzes/quiz.service.ts @@ -103,12 +103,6 @@ export class QuizService { } async deleteClass(id: number): Promise { - const classEntity = await this.classRepository.findClassWithRelations(id); - - if (!classEntity) { - throw new NotFoundException(`Class with ID ${id} not found`); - } - - await this.classRepository.deleteWithRelations(classEntity); + await this.classRepository.deleteWithRelations(id); } } diff --git a/packages/server/src/module/quiz/quizzes/repositories/class.repository.ts b/packages/server/src/module/quiz/quizzes/repositories/class.repository.ts index d6e7d503..503c493a 100644 --- a/packages/server/src/module/quiz/quizzes/repositories/class.repository.ts +++ b/packages/server/src/module/quiz/quizzes/repositories/class.repository.ts @@ -1,6 +1,6 @@ import { Injectable, InternalServerErrorException, NotFoundException } from '@nestjs/common'; import { InjectRepository } from '@nestjs/typeorm'; -import { Repository } from 'typeorm'; +import { DataSource, Repository } from 'typeorm'; import { Class } from '../entities/class.entity'; import { Quiz } from '../entities/quiz.entity'; import { Choice } from '../entities/choice.entity'; @@ -10,6 +10,7 @@ export class ClassRepository { constructor( @InjectRepository(Class) private readonly repository: Repository, + private readonly dataSource: DataSource, ) {} async create(classData: Partial): Promise { @@ -129,40 +130,25 @@ export class ClassRepository { } } - async deleteWithRelations(classEntity: Class): Promise { - const queryRunner = this.repository.manager.connection.createQueryRunner(); + async deleteWithRelations(id: number): Promise { + const queryRunner = this.dataSource.createQueryRunner(); await queryRunner.connect(); await queryRunner.startTransaction(); try { - // 1. 먼저 모든 하위 Choice 삭제 - if (classEntity.quizzes) { - for (const quiz of classEntity.quizzes) { - if (quiz.choices && quiz.choices.length > 0) { - await queryRunner.manager.delete(Choice, { - quizId: quiz.id, - }); - } - } - } + await queryRunner.query( + `DELETE FROM choice WHERE quiz_id IN (SELECT id FROM quiz WHERE class_id = ?)`, + [id], + ); - // 2. Quiz 삭제 - if (classEntity.quizzes && classEntity.quizzes.length > 0) { - await queryRunner.manager.delete(Quiz, { - classId: classEntity.id, - }); - } + await queryRunner.query(`DELETE FROM quiz WHERE class_id = ?`, [id]); - // 3. 마지막으로 Class 삭제 - await queryRunner.manager.delete(Class, { - id: classEntity.id, - }); + await queryRunner.query(`DELETE FROM class WHERE id = ?`, [id]); await queryRunner.commitTransaction(); } catch (error) { - console.error('Failed to delete class with relations:', error); await queryRunner.rollbackTransaction(); - throw error; + throw new InternalServerErrorException('Failed to delete'); } finally { await queryRunner.release(); }