Skip to content

Conversation

@twalla26
Copy link
Collaborator

@twalla26 twalla26 commented Nov 28, 2024

Note

리뷰 해주시면 제가 merge 하겠습니다!

  • 작성자: 송수민
  • 작성 날짜: 2024.11.28

✅ 체크리스트

  • 코드가 정상적으로 작동하는지 확인했습니다.
  • 주요 변경사항에 대한 설명을 작성했습니다.
  • 코드 스타일 가이드에 따라 코드를 작성했습니다.

🧩 작업 내용

  • 질문 삭제 서비스 로직에 transactional() 추가

📝 작업 상세 내역

질문 삭제 서비스 로직에 transactional() 추가

  • 특정 질문이 하나 삭제되면 남은 질문들의 순서도 업데이트 해줘야합니다.
  • 현재 질문 삭제 로직에서는 질문이 하나 삭제되면 그 다음 질문들의 index를 하나씩 줄여주고 있습니다.
  • 수정이 필요한 질문 갯수만큼 디비에 여러번 접근하고 있어서 transaction으로 묶어줘야했으나, 잊어버렸습니다...
  • 그래서 아래와 같이 제일 위에 추가해주었습니다.
@Transactional()
async deleteQuestion(deleteQuestionDto: DeleteQuestionDto) {
    const { id, questionListId, userId } = deleteQuestionDto;

    const question = await this.questionListRepository.getQuestionById(id);
    if (!question) throw new Error("Question not found.");

    const user = await this.userRepository.getUserByUserId(userId);
    if (!user) throw new Error("User not found.");

    const questionList = await this.questionListRepository.getQuestionListById(questionListId);
    if (!questionList) throw new Error("Question list not found.");
    if (questionList.userId !== userId)
        throw new Error(
            "You do not have permission to delete the question in this question list."
        );

    const questionIndex = question.index;

    const questionsToUpdate = await this.questionListRepository.getQuestionsAfterIndex(
        questionListId,
        questionIndex
    );

    for (const q of questionsToUpdate) {
        q.index -= 1;
        await this.questionListRepository.saveQuestion(q);
    }

    return await this.questionListRepository.deleteQuestion(question);
}

@twalla26 twalla26 added the 🐛 Bug Fix 해충 제거 label Nov 28, 2024
@twalla26 twalla26 requested a review from blu3fishez November 28, 2024 10:42
@twalla26 twalla26 self-assigned this Nov 28, 2024
Copy link
Collaborator

@blu3fishez blu3fishez left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

엘지티엠.

@twalla26 twalla26 merged commit 48e6820 into boostcampwm-2024:dev Nov 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

🐛 Bug Fix 해충 제거

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants