Skip to content

Commit

Permalink
fix() : 댓글 삭제하면 댓글 수 감소
Browse files Browse the repository at this point in the history
  • Loading branch information
dongkyun0713 committed Feb 27, 2024
1 parent 3b39057 commit bb4f1a2
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public MatchingPostCreateResponseDto createMatchingPost(Principal principal,
// 게시물 조회
@Transactional(readOnly = true)
public MatchingPostResponseDto findByMatchingPostId(Long matchingPostId, HttpServletRequest request,
HttpServletResponse response) {
HttpServletResponse response) {
MatchingPost matchingPost = findMatchingPostById(matchingPostId);
Integer reviewCount = matchingPostReviewRepository.countByMatchingPost(matchingPost);
countViews(matchingPost, request, response);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public ResponseEntity<String> acceptAnswer(@PathVariable("answerId") Long answer
String currentEmail = SecurityContextHolder.getContext().getAuthentication().getName();
User currentUser = userRepository.findByEmail(currentEmail)
.orElseThrow(() -> new CustomException(ErrorCode.USER_NOT_FOUND));
answerService.acceptAnswer(questionId, answerId, currentUser.getId());
answerService.acceptAnswer(questionId, answerId, currentUser);
return ResponseEntity.ok("답변 채택 성공");
}

Expand All @@ -76,7 +76,7 @@ public ResponseEntity<AnswerDTO.Response> updateAnswer(@PathVariable("answerId")
String currentEmail = SecurityContextHolder.getContext().getAuthentication().getName();
User currentUser = userRepository.findByEmail(currentEmail)
.orElseThrow(() -> new CustomException(ErrorCode.USER_NOT_FOUND));
return ResponseEntity.ok(answerService.update(answerId, request, currentUser.getId()));
return ResponseEntity.ok(answerService.update(answerId, request, currentUser));
}

@DeleteMapping("/{answerId}")
Expand All @@ -91,7 +91,7 @@ public ResponseEntity<Void> deleteAnswer(@PathVariable("answerId") Long answerId
String currentEmail = SecurityContextHolder.getContext().getAuthentication().getName();
User currentUser = userRepository.findByEmail(currentEmail)
.orElseThrow(() -> new CustomException(ErrorCode.USER_NOT_FOUND));
answerService.delete(answerId, currentUser.getId());
answerService.delete(answerId, currentUser);
return ResponseEntity.noContent().build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@ public interface AnswerRepository extends JpaRepository<Answer, Long> {
void deleteAllByQuestion(Question question);

List<Answer> findAnswerByAuthor(User user);

List<Answer> findByQuestionId(Long questionId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ public AnswerDTO.Response save(AnswerDTO.Request request, Long questionId, Strin

//Update
@Transactional
public AnswerDTO.Response update(Long id, AnswerDTO.Request request, Long userId) throws CustomException {
validateAnswerAuthorIsLoggedInUser(id, userId);
public AnswerDTO.Response update(Long id, AnswerDTO.Request request, User user) throws CustomException {
validateAnswerAuthorIsLoggedInUser(id, user);
Answer answer = answerRepository.findById(id)
.orElseThrow(() -> new CustomException(ErrorCode.ANSWER_NOT_FOUND));
answer.setContent(request.getContent());
Expand All @@ -61,14 +61,15 @@ public AnswerDTO.Response update(Long id, AnswerDTO.Request request, Long userId

// Delete
@Transactional
public void delete(Long id, Long userId) throws CustomException {
validateAnswerAuthorIsLoggedInUser(id, userId);
public void delete(Long id, User user) throws CustomException {
validateAnswerAuthorIsLoggedInUser(id, user);
user.setCountAnswer(user.getCountAnswer() - 1); // 유저 답변 수 1 감소
answerRepository.deleteById(id);
}

@Transactional
public void acceptAnswer(Long questionId, Long answerId, Long userId) {
validateQuestionAuthorIsLoggedInUser(questionId, userId);
public void acceptAnswer(Long questionId, Long answerId, User user) {
validateQuestionAuthorIsLoggedInUser(questionId, user);
Question question = questionRepository.findById(questionId)
.orElseThrow(() -> new CustomException(ErrorCode.QUESTION_NOT_FOUND));
verifyAcceptedAnswer(questionId);
Expand All @@ -91,21 +92,17 @@ private void verifyAcceptedAnswer(Long questionId) {
}
}

private void validateQuestionAuthorIsLoggedInUser(Long questionId, Long userId) {
private void validateQuestionAuthorIsLoggedInUser(Long questionId, User user) {
Question question = questionRepository.findById(questionId)
.orElseThrow(() -> new CustomException(ErrorCode.QUESTION_NOT_FOUND));
User user = userRepository.findById(userId)
.orElseThrow(() -> new CustomException(ErrorCode.USER_NOT_FOUND));
if (!question.getAuthor().equals(user)) {
throw new CustomException(ErrorCode.MISMATCH_AUTHOR);
}
}

private void validateAnswerAuthorIsLoggedInUser(Long answerId, Long userId) {
private void validateAnswerAuthorIsLoggedInUser(Long answerId, User user) {
Answer answer = answerRepository.findById(answerId)
.orElseThrow(() -> new CustomException(ErrorCode.QUESTION_NOT_FOUND));
User user = userRepository.findById(userId)
.orElseThrow(() -> new CustomException(ErrorCode.USER_NOT_FOUND));
if (!answer.getAuthor().equals(user)) {
throw new CustomException(ErrorCode.MISMATCH_AUTHOR);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.example.titto_backend.auth.service.ExperienceService;
import com.example.titto_backend.common.exception.CustomException;
import com.example.titto_backend.common.exception.ErrorCode;
import com.example.titto_backend.questionBoard.domain.Answer;
import com.example.titto_backend.questionBoard.domain.Department;
import com.example.titto_backend.questionBoard.domain.Question;
import com.example.titto_backend.questionBoard.domain.Status;
Expand All @@ -20,6 +21,7 @@
import java.time.LocalTime;
import java.time.ZoneOffset;
import java.util.Arrays;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
Expand All @@ -35,6 +37,7 @@ public class QuestionService {
private final AnswerRepository answerRepository;

private final ExperienceService experienceService;
private final AnswerService answerService;

//Create
@Transactional
Expand Down Expand Up @@ -95,6 +98,17 @@ public void update(QuestionDTO.Update update, Long id, Long userId) throws Custo
@Transactional
public void delete(Long id, Long userId) {
validateAuthorIsLoggedInUser(id, userId);

// 질문에 연관된 답변들을 가져옴
List<Answer> answers = answerRepository.findByQuestionId(id);

// 답변들을 하나씩 삭제
answers.forEach(answer -> {
User answerAuthor = answer.getAuthor();
answerService.delete(answer.getId(), answerAuthor);
});

// 질문 삭제
questionRepository.deleteById(id);
}

Expand All @@ -116,8 +130,7 @@ private void validateAuthorIsLoggedInUser(Long id, Long userId) {

isAcceptAnswer(question, user);

answerRepository.deleteAllByQuestion(question);
if (!question.getAuthor().getId().equals(user.getId())) {
if (!question.getAuthor().equals(user)) {
throw new CustomException(ErrorCode.MISMATCH_AUTHOR);
}
}
Expand Down

0 comments on commit bb4f1a2

Please sign in to comment.