diff --git a/src/infra/entity/Answers.entity.ts b/src/infra/entity/Answers.entity.ts index 3026e3b..c0d85ec 100644 --- a/src/infra/entity/Answers.entity.ts +++ b/src/infra/entity/Answers.entity.ts @@ -7,7 +7,7 @@ export class Answers { @PrimaryGeneratedColumn() id: number; - @Column() + @Column({ type: 'longtext' }) value: string; @Column() @@ -16,9 +16,6 @@ export class Answers { @ManyToOne(() => Applications, (application) => application.answers) application: Applications; - @ManyToOne(() => Questions, (question) => question.answers, { - onDelete: 'CASCADE', - onUpdate: 'CASCADE', - }) + @ManyToOne(() => Questions, (question) => question.answers) question: Questions; } diff --git a/src/infra/entity/Questions.entity.ts b/src/infra/entity/Questions.entity.ts index f01d59c..d186a4f 100644 --- a/src/infra/entity/Questions.entity.ts +++ b/src/infra/entity/Questions.entity.ts @@ -56,7 +56,10 @@ export class Questions { selectOptions: SelectOptions[]; @ApiHideProperty() - @OneToMany(() => Answers, (answer) => answer.question) + @OneToMany(() => Answers, (answer) => answer.question, { + onDelete: 'CASCADE', + onUpdate: 'CASCADE', + }) answers: Answers[]; @ApiHideProperty() diff --git a/src/modules/application/application-base.module.ts b/src/modules/application/application-base.module.ts index 2cb7c9e..ee1f349 100644 --- a/src/modules/application/application-base.module.ts +++ b/src/modules/application/application-base.module.ts @@ -5,6 +5,6 @@ import { ApplicationBaseService } from './service/application-base.service'; @Module({ providers: [ApplicationRepository, AnswersRepository, ApplicationBaseService], - exports: [ApplicationBaseService, AnswersRepository], + exports: [ApplicationBaseService], }) export class ApplicationBaseModule {} diff --git a/src/modules/application/repository/answer.repository.ts b/src/modules/application/repository/answer.repository.ts index b8af086..d31706d 100644 --- a/src/modules/application/repository/answer.repository.ts +++ b/src/modules/application/repository/answer.repository.ts @@ -1,10 +1,16 @@ import { Injectable } from '@nestjs/common'; import { Answers } from 'src/infra/entity/Answers.entity'; -import { DataSource, Repository } from 'typeorm'; +import { DataSource, In, Repository } from 'typeorm'; @Injectable() export class AnswersRepository extends Repository { constructor(private readonly dataSource: DataSource) { super(Answers, dataSource.createEntityManager()); } + + async deleteByIds(answerIds: number[]) { + await this.delete({ + id: In(answerIds), + }); + } } diff --git a/src/modules/application/repository/application.repository.ts b/src/modules/application/repository/application.repository.ts index c0654df..438f0c4 100644 --- a/src/modules/application/repository/application.repository.ts +++ b/src/modules/application/repository/application.repository.ts @@ -7,6 +7,16 @@ export class ApplicationRepository extends Repository { constructor(private readonly dataSource: DataSource) { super(Applications, dataSource.createEntityManager()); } + async findWithIdAndJoinAnswer(applicationId: number) { + return await this.findOne({ + where: { + id: applicationId, + }, + relations: { + answers: true, + }, + }); + } async findById(applicationId: number): Promise { const queryBuilder = this.createQueryBuilder('application'); diff --git a/src/modules/application/service/application-base.service.ts b/src/modules/application/service/application-base.service.ts index 2d279bd..59445de 100644 --- a/src/modules/application/service/application-base.service.ts +++ b/src/modules/application/service/application-base.service.ts @@ -28,19 +28,12 @@ export class ApplicationBaseService { } async deleteById(applicationId: number): Promise<{ affected?: number }> { - const application = await this.applicationRepository.findOne({ - where: { - id: applicationId, - }, - relations: { - answers: true, - }, - }); - await Promise.all( - application.answers.map(async (answer) => { - await this.answersRepository.delete(answer.id); - }), + const application = + await this.applicationRepository.findWithIdAndJoinAnswer(applicationId); + await this.answersRepository.deleteByIds( + application.answers.map((answer) => answer.id), ); + return await this.applicationRepository.delete(applicationId); } @@ -59,4 +52,7 @@ export class ApplicationBaseService { generationId, ); } + async deleteAnswersByQuestionId({ questionId }: { questionId: number }) { + return await this.answersRepository.delete({ questionId: questionId }); + } } diff --git a/src/modules/question/service/question.service.ts b/src/modules/question/service/question.service.ts index 1dab6b5..6b3b68d 100644 --- a/src/modules/question/service/question.service.ts +++ b/src/modules/question/service/question.service.ts @@ -1,9 +1,7 @@ -import { AnswersRepository } from '@modules/application/repository/answer.repository'; +import { ApplicationBaseService } from '@modules/application/service/application-base.service'; import { CommonService } from '@modules/common/common.service'; import { Injectable, NotFoundException } from '@nestjs/common'; -import { Answers } from 'src/infra/entity/Answers.entity'; import { Questions } from 'src/infra/entity/Questions.entity'; -import { SelectOptions } from 'src/infra/entity/SelectOptions.entity'; import { questionSaveStrategyFactory } from '../domain/question.factory'; import { CreateQuestionRequestDto, @@ -21,7 +19,7 @@ export class QuestionService { private readonly partQuestionRepository: PartQuestionRepository, private readonly selectOptionsRepository: SelectOptionsRepository, private readonly commonService: CommonService, - private readonly answersRepository: AnswersRepository, + private readonly applicationBaseService: ApplicationBaseService, ) {} async getQuestions(partIds: number[]): Promise { @@ -68,7 +66,9 @@ export class QuestionService { } async deleteQuestion(id: number) { - await this.answersRepository.delete({ questionId: id }); + await this.applicationBaseService.deleteAnswersByQuestionId({ + questionId: id, + }); await this.questionRepository.delete(id); return { success: true }; } diff --git a/src/modules/user/repository/user.repository.ts b/src/modules/user/repository/user.repository.ts index 03b5972..29dbb23 100644 --- a/src/modules/user/repository/user.repository.ts +++ b/src/modules/user/repository/user.repository.ts @@ -24,4 +24,18 @@ export class UserRepository extends Repository { }) .getOne(); } + + async findOneById(userId: number) { + return await this.findOne({ + where: { id: userId }, + }); + } + + async findOneByIdAndSelectApplications(userId: number) { + return await this.createQueryBuilder('user') + .where('user.id = :userId', { userId }) + .leftJoinAndSelect('user.applications', 'applications') + .leftJoinAndSelect('applications.part', 'part') + .getOne(); + } } diff --git a/src/modules/user/service/user-base.service.ts b/src/modules/user/service/user-base.service.ts index 0673076..94a5190 100644 --- a/src/modules/user/service/user-base.service.ts +++ b/src/modules/user/service/user-base.service.ts @@ -23,9 +23,7 @@ export class UserBaseService { } async findById(userId: number): Promise { - return await this.userRepository.findOne({ - where: { id: userId }, - }); + return await this.userRepository.findOneById(userId); } async update( @@ -40,11 +38,9 @@ export class UserBaseService { } async findUserAndApplicationsOrThrow(userId: number): Promise { - const user = await this.userRepository - .createQueryBuilder('user') - .where('user.id = :userId', { userId }) - .leftJoinAndSelect('user.applications', 'applications') - .getOne(); + const user = await this.userRepository.findOneByIdAndSelectApplications( + userId, + ); if (!user) { throw new NotFoundException('없는 사용자 입니다'); }