Skip to content

Commit

Permalink
Merge branch 'master' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
be-student committed Jan 14, 2023
2 parents 8df8520 + 20aba8a commit c957b97
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 33 deletions.
7 changes: 2 additions & 5 deletions src/infra/entity/Answers.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export class Answers {
@PrimaryGeneratedColumn()
id: number;

@Column()
@Column({ type: 'longtext' })
value: string;

@Column()
Expand All @@ -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;
}
5 changes: 4 additions & 1 deletion src/infra/entity/Questions.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion src/modules/application/application-base.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ import { ApplicationBaseService } from './service/application-base.service';

@Module({
providers: [ApplicationRepository, AnswersRepository, ApplicationBaseService],
exports: [ApplicationBaseService, AnswersRepository],
exports: [ApplicationBaseService],
})
export class ApplicationBaseModule {}
8 changes: 7 additions & 1 deletion src/modules/application/repository/answer.repository.ts
Original file line number Diff line number Diff line change
@@ -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<Answers> {
constructor(private readonly dataSource: DataSource) {
super(Answers, dataSource.createEntityManager());
}

async deleteByIds(answerIds: number[]) {
await this.delete({
id: In(answerIds),
});
}
}
10 changes: 10 additions & 0 deletions src/modules/application/repository/application.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,16 @@ export class ApplicationRepository extends Repository<Applications> {
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<Applications> {
const queryBuilder = this.createQueryBuilder('application');
Expand Down
20 changes: 8 additions & 12 deletions src/modules/application/service/application-base.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -59,4 +52,7 @@ export class ApplicationBaseService {
generationId,
);
}
async deleteAnswersByQuestionId({ questionId }: { questionId: number }) {
return await this.answersRepository.delete({ questionId: questionId });
}
}
10 changes: 5 additions & 5 deletions src/modules/question/service/question.service.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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<Questions[]> {
Expand Down Expand Up @@ -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 };
}
Expand Down
14 changes: 14 additions & 0 deletions src/modules/user/repository/user.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,18 @@ export class UserRepository extends Repository<Users> {
})
.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();
}
}
12 changes: 4 additions & 8 deletions src/modules/user/service/user-base.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@ export class UserBaseService {
}

async findById(userId: number): Promise<Users> {
return await this.userRepository.findOne({
where: { id: userId },
});
return await this.userRepository.findOneById(userId);
}

async update(
Expand All @@ -40,11 +38,9 @@ export class UserBaseService {
}

async findUserAndApplicationsOrThrow(userId: number): Promise<Users> {
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('없는 사용자 입니다');
}
Expand Down

0 comments on commit c957b97

Please sign in to comment.