Skip to content

Commit

Permalink
[BE -#162 ] 클래스 삭제 API: 벌크 삭제로 변경 (#161)
Browse files Browse the repository at this point in the history
* feat: 삭제 기능 벌크 삭제로 변경

* refactor: 선택지 엔티티 명시적 매핑 추가
  • Loading branch information
glaxyt authored Dec 4, 2024
1 parent 99b9a86 commit 4b09cbc
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 37 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
}
}
8 changes: 1 addition & 7 deletions packages/server/src/module/quiz/quizzes/quiz.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,6 @@ export class QuizService {
}

async deleteClass(id: number): Promise<void> {
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);
}
}
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -10,6 +10,7 @@ export class ClassRepository {
constructor(
@InjectRepository(Class)
private readonly repository: Repository<Class>,
private readonly dataSource: DataSource,
) {}

async create(classData: Partial<Class>): Promise<Class> {
Expand Down Expand Up @@ -129,40 +130,25 @@ export class ClassRepository {
}
}

async deleteWithRelations(classEntity: Class): Promise<void> {
const queryRunner = this.repository.manager.connection.createQueryRunner();
async deleteWithRelations(id: number): Promise<void> {
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();
}
Expand Down

0 comments on commit 4b09cbc

Please sign in to comment.