@@ -17,6 +17,7 @@ import { DataSource, In, SelectQueryBuilder } from "typeorm";
1717import { PaginateMetaDto } from "@/question-list/dto/paginate-meta.dto" ;
1818import { PaginateDto } from "@/question-list/dto/paginate.dto" ;
1919import { QuestionListDto } from "@/question-list/dto/question-list.dto" ;
20+ import { Transactional } from "typeorm-transactional" ;
2021
2122@Injectable ( )
2223export class QuestionListService {
@@ -71,6 +72,7 @@ export class QuestionListService {
7172 }
7273
7374 // 질문 생성 메서드
75+ @Transactional ( )
7476 async createQuestionList ( createQuestionListDto : CreateQuestionListDto ) {
7577 const { title, contents, categoryNames, isPublic, userId } = createQuestionListDto ;
7678
@@ -81,37 +83,24 @@ export class QuestionListService {
8183 throw new Error ( "Some category names were not found." ) ;
8284 }
8385
84- const queryRunner = this . dataSource . createQueryRunner ( ) ;
85- await queryRunner . connect ( ) ;
86- await queryRunner . startTransaction ( ) ;
87-
88- try {
89- const questionList = new QuestionList ( ) ;
90- questionList . title = title ;
91- questionList . categories = categories ;
92- questionList . isPublic = isPublic ;
93- questionList . userId = userId ;
94-
95- const createdQuestionList = await queryRunner . manager . save ( questionList ) ;
86+ const questionList = new QuestionList ( ) ;
87+ questionList . title = title ;
88+ questionList . categories = categories ;
89+ questionList . isPublic = isPublic ;
90+ questionList . userId = userId ;
9691
97- const questions = contents . map ( ( content , index ) => {
98- const question = new Question ( ) ;
99- question . content = content ;
100- question . index = index ;
101- question . questionList = createdQuestionList ;
92+ const createdQuestionList = await this . questionListRepository . save ( questionList ) ;
10293
103- return question ;
104- } ) ;
105- const createdQuestions = await queryRunner . manager . save ( questions ) ;
94+ const questions = contents . map ( ( content , index ) => {
95+ const question = new Question ( ) ;
96+ question . content = content ;
97+ question . index = index ;
98+ question . questionList = createdQuestionList ;
10699
107- await queryRunner . commitTransaction ( ) ;
108- return { createdQuestionList, createdQuestions } ;
109- } catch ( error ) {
110- await queryRunner . rollbackTransaction ( ) ;
111- throw new Error ( error . message ) ;
112- } finally {
113- await queryRunner . release ( ) ;
114- }
100+ return question ;
101+ } ) ;
102+ const createdQuestions = await this . questionRepository . save ( questions ) ;
103+ return { createdQuestionList, createdQuestions } ;
115104 }
116105
117106 async getQuestionListContents ( questionListId : number , userId : number ) {
@@ -272,7 +261,8 @@ export class QuestionListService {
272261 }
273262
274263 async deleteQuestion ( deleteQuestionDto : DeleteQuestionDto ) {
275- const { id, questionListId, userId } = deleteQuestionDto ;
264+ const { id, userId } = deleteQuestionDto ;
265+ const questionListId = await this . questionRepository . getQuestionListIdByQuestionId ( id ) ;
276266
277267 const question = await this . questionRepository . findOne ( {
278268 where : { id } ,
0 commit comments