Skip to content

Commit

Permalink
#6 fix: 3번째 답변 등록 시 쥬시글로 전환
Browse files Browse the repository at this point in the history
  • Loading branch information
JoongHyun-Kim committed Sep 19, 2023
1 parent ed59f28 commit ff52a88
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ public interface AnswerRepository extends JpaRepository<Answer, Long> {
Long countByAnswerer_UserIdxAndStatusEquals(Long userIdx, String status);
Page<Answer> findByAnswerer_UserIdxAndPost_IsJuicyAndStatusEquals(Long userIdx, Boolean isJuicy, String status, Pageable pageable);
Long countByPost_PostIdxAndStatusEquals(Long postIdx, String status);
int countByPostAndIsJuicyFalse(Post post);
}
32 changes: 21 additions & 11 deletions src/main/java/com/ewhatever/qna/answer/service/AnswerService.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,45 @@
import com.ewhatever.qna.answer.entity.Answer;
import com.ewhatever.qna.answer.repository.AnswerRepository;
import com.ewhatever.qna.common.Base.BaseException;
import com.ewhatever.qna.common.enums.Role;
import com.ewhatever.qna.post.entity.Post;
import com.ewhatever.qna.post.repository.PostRepository;
import com.ewhatever.qna.user.entity.User;
import com.ewhatever.qna.user.repository.UserRepository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import static com.ewhatever.qna.common.Base.BaseResponseStatus.*;
import static com.ewhatever.qna.common.Constant.Status.ACTIVE;
import static com.ewhatever.qna.common.enums.Role.SINY;

@Service
public class AnswerService {
UserRepository userRepository;
PostRepository postRepository;
AnswerRepository answerRepository;
/**
* 답변 등록
*/

@Transactional(rollbackFor = Exception.class)
public void addAnswer(Long userIdx, PostAnswerReq postAnswerReq) throws BaseException {
try {
User user = userRepository.findByUserIdxAndStatusEquals(userIdx, ACTIVE).orElseThrow(() -> new BaseException(INVALID_USER));
Post post = postRepository.findById(postAnswerReq.getPostIdx()).orElseThrow(() -> new BaseException(INVALID_POST_IDX));
if (user.getRole().equals(Role.SINY)) {
Answer answer = Answer.builder()
.content(postAnswerReq.getAnswer())
.answerer(user)
.post(post)
.build();
answerRepository.save(answer);
int currentAnswerCount = answerRepository.countByPostAndIsJuicyFalse(post);
if (user.getRole().equals(SINY)) {
if (currentAnswerCount < 3) { // 크론잡 주기 사이에 답변이 등록되어 isJuicy 컬럼값에 아직 반영이 안된 경우를 위해 예외처리
Answer answer = Answer.builder()
.content(postAnswerReq.getAnswer())
.answerer(user)
.post(post)
.build();
answerRepository.save(answer);

currentAnswerCount = answerRepository.countByPostAndIsJuicyFalse(post); //currentAnswerCount 최신화
// 3번째 답변이면 쥬시글로 전환
if (currentAnswerCount == 3) {
post.setIsJuicy(true);
}
postRepository.save(post);
} else throw new BaseException(ALREADY_JUICY_POST);
} else throw new BaseException(NO_SENIOR_ROLE);
} catch (BaseException e) {
throw e;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public enum BaseResponseStatus {
* 2000: Request 오류
*/
// answer(2000-2099)
ALREADY_JUICY_POST(false, 2000, "이미 쥬시글로 등록된 post입니다,"),

// comment(2100-2199)
INVALID_COMMENT_IDX(false, 2100, "잘못된 commentIdx입니다."),
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/ewhatever/qna/post/entity/Post.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ public class Post extends BaseEntity {
public void setScrapCount(Long scrapCount) {
this.scrapCount = scrapCount;
}

public void setCommentCount(Long commentCount) { this.commentCount = commentCount; }
public void setIsJuicy(Boolean isJuicy) { this.isJuicy = isJuicy; }
}


0 comments on commit ff52a88

Please sign in to comment.