From 34c3f24472f5c4d70bfffb0ac67f2e9194c2c265 Mon Sep 17 00:00:00 2001 From: ddongseop Date: Tue, 11 Jul 2023 03:19:10 +0900 Subject: [PATCH 1/4] =?UTF-8?q?[FIX]=20=ED=9A=A8=EC=9A=A9=EC=84=B1=20?= =?UTF-8?q?=EB=8C=80=EC=8B=A0=20=EC=A3=BC=EC=A0=9C=EB=A1=9C=20=ED=95=84?= =?UTF-8?q?=EB=93=9C=EB=AA=85=20=EB=B3=80=EA=B2=BD=20=EB=B0=8F=20=EC=84=B9?= =?UTF-8?q?=EC=85=98=20id=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dto/response/TodayQnAResponseDto.java | 4 ++-- .../umbbaServer/domain/qna/model/Question.java | 3 +-- .../domain/qna/model/QuestionEffect.java | 15 --------------- .../domain/qna/model/QuestionSection.java | 9 ++++++--- 4 files changed, 9 insertions(+), 22 deletions(-) delete mode 100644 src/main/java/sopt/org/umbbaServer/domain/qna/model/QuestionEffect.java diff --git a/src/main/java/sopt/org/umbbaServer/domain/qna/controller/dto/response/TodayQnAResponseDto.java b/src/main/java/sopt/org/umbbaServer/domain/qna/controller/dto/response/TodayQnAResponseDto.java index b2102487..f15af386 100644 --- a/src/main/java/sopt/org/umbbaServer/domain/qna/controller/dto/response/TodayQnAResponseDto.java +++ b/src/main/java/sopt/org/umbbaServer/domain/qna/controller/dto/response/TodayQnAResponseDto.java @@ -12,7 +12,7 @@ public class TodayQnAResponseDto { private String section; - private String effect; + private String topic; private String opponentQuestion; private String myQuestion; @@ -54,7 +54,7 @@ public static TodayQnAResponseDto of(User myUser, User opponentUser, QnA todayQn return TodayQnAResponseDto.builder() .section(todayQuestion.getSection().getValue()) - .effect(todayQuestion.getEffect().getValue()) + .topic(todayQuestion.getTopic()) .opponentQuestion(opponentQuestion) .myQuestion(myQuestion) .opponentAnswer(opponentAnswer) diff --git a/src/main/java/sopt/org/umbbaServer/domain/qna/model/Question.java b/src/main/java/sopt/org/umbbaServer/domain/qna/model/Question.java index 72cbc4d3..97d5d3db 100644 --- a/src/main/java/sopt/org/umbbaServer/domain/qna/model/Question.java +++ b/src/main/java/sopt/org/umbbaServer/domain/qna/model/Question.java @@ -28,6 +28,5 @@ public class Question extends AuditingTimeEntity { private QuestionSection section; @Column(nullable = false) - @Enumerated(EnumType.STRING) - private QuestionEffect effect; + private String topic; } diff --git a/src/main/java/sopt/org/umbbaServer/domain/qna/model/QuestionEffect.java b/src/main/java/sopt/org/umbbaServer/domain/qna/model/QuestionEffect.java deleted file mode 100644 index 9fb77d27..00000000 --- a/src/main/java/sopt/org/umbbaServer/domain/qna/model/QuestionEffect.java +++ /dev/null @@ -1,15 +0,0 @@ -package sopt.org.umbbaServer.domain.qna.model; - -import lombok.AccessLevel; -import lombok.Getter; -import lombok.RequiredArgsConstructor; - -@Getter -@RequiredArgsConstructor(access = AccessLevel.PRIVATE) -public enum QuestionEffect { - DREAM("꿈에 대한 생각"), - SYMPATHY("공감대형성"), - HOOKING("후킹 요소"); - - private final String value; -} diff --git a/src/main/java/sopt/org/umbbaServer/domain/qna/model/QuestionSection.java b/src/main/java/sopt/org/umbbaServer/domain/qna/model/QuestionSection.java index 2159250a..11bf22a6 100644 --- a/src/main/java/sopt/org/umbbaServer/domain/qna/model/QuestionSection.java +++ b/src/main/java/sopt/org/umbbaServer/domain/qna/model/QuestionSection.java @@ -7,9 +7,12 @@ @Getter @RequiredArgsConstructor(access = AccessLevel.PRIVATE) public enum QuestionSection { - YOUNG("어린시절"), - SCHOOL("학창시절"), - GOLDEN("청춘시절"); + YOUNG(1L, "어린시절"), + SCHOOL(2L, "학창시절"), + GOLDEN(3L, "청춘시절"), + COUPLE(4L, "연애시절"), + MARRIAGE(5L, "결혼시절"); + private final Long sectionId; private final String value; } \ No newline at end of file From 744b1016ddffa714ff5b18cf7bf931c29c41bb2a Mon Sep 17 00:00:00 2001 From: ddongseop Date: Tue, 11 Jul 2023 04:17:13 +0900 Subject: [PATCH 2/4] =?UTF-8?q?[FEAT]=20=EA=B3=BC=EA=B1=B0=EC=9D=98=20?= =?UTF-8?q?=EC=A7=88=EB=AC=B8=EA=B3=BC=20=EB=8B=B5=EB=B3=80=20=EB=A6=AC?= =?UTF-8?q?=EC=8A=A4=ED=8A=B8=20=EC=A1=B0=ED=9A=8C=20API=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84=20#22?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../repository/ParentchildRepository.java | 3 -- .../domain/qna/controller/QnAController.java | 16 ++++++- .../dto/response/QnAListResponseDto.java | 15 +++++++ .../org/umbbaServer/domain/qna/model/QnA.java | 2 +- .../domain/qna/service/QnAService.java | 43 ++++++++++++++++--- .../global/exception/SuccessType.java | 2 +- 6 files changed, 70 insertions(+), 11 deletions(-) create mode 100644 src/main/java/sopt/org/umbbaServer/domain/qna/controller/dto/response/QnAListResponseDto.java diff --git a/src/main/java/sopt/org/umbbaServer/domain/parentchild/repository/ParentchildRepository.java b/src/main/java/sopt/org/umbbaServer/domain/parentchild/repository/ParentchildRepository.java index 2b62525a..4cc98f94 100644 --- a/src/main/java/sopt/org/umbbaServer/domain/parentchild/repository/ParentchildRepository.java +++ b/src/main/java/sopt/org/umbbaServer/domain/parentchild/repository/ParentchildRepository.java @@ -14,9 +14,6 @@ public interface ParentchildRepository extends Repository { // CREATE void save(Parentchild parentchild); - @Query("SELECT u FROM User u WHERE u.parentChild = :parentChild") - List findUsersByParentChild(@Param("parentChild") Parentchild parentChild); - // READ Optional findById(Long id); Optional findByInviteCode(String inviteCode); diff --git a/src/main/java/sopt/org/umbbaServer/domain/qna/controller/QnAController.java b/src/main/java/sopt/org/umbbaServer/domain/qna/controller/QnAController.java index 0c6dc679..48a37942 100644 --- a/src/main/java/sopt/org/umbbaServer/domain/qna/controller/QnAController.java +++ b/src/main/java/sopt/org/umbbaServer/domain/qna/controller/QnAController.java @@ -4,6 +4,7 @@ import org.springframework.http.HttpStatus; import org.springframework.web.bind.annotation.*; import sopt.org.umbbaServer.domain.qna.controller.dto.request.TodayAnswerRequestDto; +import sopt.org.umbbaServer.domain.qna.controller.dto.response.QnAListResponseDto; import sopt.org.umbbaServer.domain.qna.controller.dto.response.TodayQnAResponseDto; import sopt.org.umbbaServer.domain.qna.service.QnAService; import sopt.org.umbbaServer.global.common.dto.ApiResponse; @@ -12,6 +13,7 @@ import javax.validation.Valid; import java.security.Principal; +import java.util.List; @RestController @RequiredArgsConstructor @@ -40,8 +42,20 @@ public ApiResponse answerTodayQuestion( Principal principal, @Valid @RequestBody TodayAnswerRequestDto request) { - qnAService.answerTodayQuestion(request, JwtProvider.getUserFromPrincial(principal)); + qnAService.answerTodayQuestion(JwtProvider.getUserFromPrincial(principal), request); return ApiResponse.success(SuccessType.ANSWER_TODAY_QUESTION_SUCCESS); } + + @GetMapping("/qna/{sectionId}") + @ResponseStatus(HttpStatus.OK) + public ApiResponse> getQnaList( + Principal principal, + @PathVariable(name = "sectionId") Long sectionId) { + + return ApiResponse.success(SuccessType.GET_QNA_LIST_SUCCESS, + qnAService.getQnaList(JwtProvider.getUserFromPrincial(principal), sectionId)); + + } + } diff --git a/src/main/java/sopt/org/umbbaServer/domain/qna/controller/dto/response/QnAListResponseDto.java b/src/main/java/sopt/org/umbbaServer/domain/qna/controller/dto/response/QnAListResponseDto.java new file mode 100644 index 00000000..661bb50b --- /dev/null +++ b/src/main/java/sopt/org/umbbaServer/domain/qna/controller/dto/response/QnAListResponseDto.java @@ -0,0 +1,15 @@ +package sopt.org.umbbaServer.domain.qna.controller.dto.response; + +import lombok.AccessLevel; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Getter; + +@Getter +@Builder +@AllArgsConstructor(access = AccessLevel.PRIVATE) +public class QnAListResponseDto { + + private int index; + private String question; +} diff --git a/src/main/java/sopt/org/umbbaServer/domain/qna/model/QnA.java b/src/main/java/sopt/org/umbbaServer/domain/qna/model/QnA.java index 3716ac8a..8e988b54 100644 --- a/src/main/java/sopt/org/umbbaServer/domain/qna/model/QnA.java +++ b/src/main/java/sopt/org/umbbaServer/domain/qna/model/QnA.java @@ -18,7 +18,7 @@ public class QnA extends AuditingTimeEntity { private Long id; @OneToOne - @JoinColumn(name = "question_id") + @JoinColumn(name = "question_id", nullable = false) private Question question; private String parentAnswer; diff --git a/src/main/java/sopt/org/umbbaServer/domain/qna/service/QnAService.java b/src/main/java/sopt/org/umbbaServer/domain/qna/service/QnAService.java index 6cdc3e28..92b4a567 100644 --- a/src/main/java/sopt/org/umbbaServer/domain/qna/service/QnAService.java +++ b/src/main/java/sopt/org/umbbaServer/domain/qna/service/QnAService.java @@ -6,6 +6,7 @@ import sopt.org.umbbaServer.domain.parentchild.model.Parentchild; import sopt.org.umbbaServer.domain.parentchild.repository.ParentchildRepository; import sopt.org.umbbaServer.domain.qna.controller.dto.request.TodayAnswerRequestDto; +import sopt.org.umbbaServer.domain.qna.controller.dto.response.QnAListResponseDto; import sopt.org.umbbaServer.domain.qna.controller.dto.response.TodayQnAResponseDto; import sopt.org.umbbaServer.domain.qna.model.QnA; import sopt.org.umbbaServer.domain.qna.model.Question; @@ -17,7 +18,9 @@ import sopt.org.umbbaServer.global.exception.ErrorType; import java.util.List; +import java.util.Objects; import java.util.stream.Collectors; +import java.util.stream.IntStream; @Service @RequiredArgsConstructor @@ -32,7 +35,7 @@ public class QnAService { public TodayQnAResponseDto getTodayQnA(Long userId) { User myUser = getUserById(userId); Parentchild parentchild = getParentchildByUser(myUser); - QnA todayQnA = getQnAByParentchild(parentchild); + QnA todayQnA = getTodayQnAByParentchild(parentchild); Question todayQuestion = todayQnA.getQuestion(); User opponentUser = getOpponentByParentchild(parentchild, userId); @@ -43,10 +46,10 @@ public TodayQnAResponseDto getTodayQnA(Long userId) { } @Transactional - public void answerTodayQuestion(TodayAnswerRequestDto request, Long userId) { + public void answerTodayQuestion(Long userId, TodayAnswerRequestDto request) { User myUser = getUserById(userId); Parentchild parentchild = getParentchildByUser(myUser); - QnA todayQnA = getQnAByParentchild(parentchild); + QnA todayQnA = getTodayQnAByParentchild(parentchild); User opponentUser = getOpponentByParentchild(parentchild, userId); boolean isMeChild = myUser.getBornYear() >= opponentUser.getBornYear(); @@ -58,6 +61,27 @@ public void answerTodayQuestion(TodayAnswerRequestDto request, Long userId) { } } + public List getQnaList(Long userId, Long sectionId) { + User myUser = getUserById(userId); + Parentchild parentchild = getParentchildByUser(myUser); + List qnaList = getQnAListByParentchild(parentchild); + User opponentUser = getOpponentByParentchild(parentchild, userId); + + boolean isMeChild = myUser.getBornYear() >= opponentUser.getBornYear(); + + return IntStream.range(0, qnaList.size()) + .filter(i -> Objects.equals(qnaList.get(i).getQuestion().getSection().getSectionId(), sectionId)) + .mapToObj(i -> { + QnA qna = qnaList.get(i); + String question = isMeChild ? qna.getQuestion().getChildQuestion() : qna.getQuestion().getParentQuestion(); + return QnAListResponseDto.builder() + .index(i) + .question(question) + .build(); + }) + .collect(Collectors.toList()); + } + @Transactional public void createQnA() { QnA newQnA = QnA.builder() @@ -89,7 +113,16 @@ private Parentchild getParentchildByUser(User user) { return parentchild; } - private QnA getQnAByParentchild(Parentchild parentchild) { + private List getQnAListByParentchild(Parentchild parentchild) { + List qnAList = parentchild.getQnaList(); + if (qnAList == null || qnAList.isEmpty()) { + throw new CustomException(ErrorType.PARENTCHILD_HAVE_NO_QNALIST); + } + + return qnAList; + } + + private QnA getTodayQnAByParentchild(Parentchild parentchild) { List qnAList = parentchild.getQnaList(); if (qnAList == null || qnAList.isEmpty()) { throw new CustomException(ErrorType.PARENTCHILD_HAVE_NO_QNALIST); @@ -100,7 +133,7 @@ private QnA getQnAByParentchild(Parentchild parentchild) { private User getOpponentByParentchild(Parentchild parentchild, Long userId) { // Parentchild에 속한 User들 중 자신이 아닌 객체를 가져옴 - List opponentUserList = parentchildRepository.findUsersByParentChild(parentchild) + List opponentUserList = userRepository.findUserByParentChild(parentchild) .stream() .filter(user -> !user.getId().equals(userId)) .collect(Collectors.toList()); diff --git a/src/main/java/sopt/org/umbbaServer/global/exception/SuccessType.java b/src/main/java/sopt/org/umbbaServer/global/exception/SuccessType.java index f51a29db..9d3e6e12 100644 --- a/src/main/java/sopt/org/umbbaServer/global/exception/SuccessType.java +++ b/src/main/java/sopt/org/umbbaServer/global/exception/SuccessType.java @@ -17,7 +17,7 @@ public enum SuccessType { LOGOUT_SUCCESS(HttpStatus.OK, "로그아웃에 성공했습니다."), KAKAO_ACCESS_TOKEN_SUCCESS(HttpStatus.OK, "카카오 엑세스 토큰을 가져오는데 성공했습니다"), GET_TODAY_QNA_SUCCESS(HttpStatus.OK, "일일문답 조회에 성공했습니다."), - + GET_QNA_LIST_SUCCESS(HttpStatus.OK, "섹션별 과거의 문답 리스트 조회에 성공했습니다"), MATCH_PARENT_CHILD_SUCCESS(HttpStatus.OK, "부모자식 관계 매칭에 성공했습니다."), From af616b778b2b9e1bc2b17a712ce730b9205edaf4 Mon Sep 17 00:00:00 2001 From: ddongseop Date: Tue, 11 Jul 2023 04:24:47 +0900 Subject: [PATCH 3/4] =?UTF-8?q?[FIX]=20=EC=82=AC=EC=9A=A9=EC=9E=90?= =?UTF-8?q?=EA=B0=80=20=EB=B0=9B=EC=9D=80=20=EC=A0=84=EC=B2=B4=20=EC=A7=88?= =?UTF-8?q?=EB=AC=B8=EC=9D=98=20=EC=9D=B8=EB=8D=B1=EC=8A=A4=EB=A1=9C=20?= =?UTF-8?q?=EB=B3=80=EA=B2=BD=20#22?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../org/umbbaServer/domain/qna/service/QnAService.java | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/main/java/sopt/org/umbbaServer/domain/qna/service/QnAService.java b/src/main/java/sopt/org/umbbaServer/domain/qna/service/QnAService.java index 92b4a567..aacdaf98 100644 --- a/src/main/java/sopt/org/umbbaServer/domain/qna/service/QnAService.java +++ b/src/main/java/sopt/org/umbbaServer/domain/qna/service/QnAService.java @@ -69,13 +69,12 @@ public List getQnaList(Long userId, Long sectionId) { boolean isMeChild = myUser.getBornYear() >= opponentUser.getBornYear(); - return IntStream.range(0, qnaList.size()) - .filter(i -> Objects.equals(qnaList.get(i).getQuestion().getSection().getSectionId(), sectionId)) - .mapToObj(i -> { - QnA qna = qnaList.get(i); + return qnaList.stream() + .filter(qna -> Objects.equals(qna.getQuestion().getSection().getSectionId(), sectionId)) + .map(qna -> { String question = isMeChild ? qna.getQuestion().getChildQuestion() : qna.getQuestion().getParentQuestion(); return QnAListResponseDto.builder() - .index(i) + .index(qnaList.indexOf(qna) + 1) .question(question) .build(); }) From bc6d5130fb369f1f51c6f80a19df07e483857bb2 Mon Sep 17 00:00:00 2001 From: ddongseop Date: Tue, 11 Jul 2023 04:54:55 +0900 Subject: [PATCH 4/4] =?UTF-8?q?[FEAT]=20=EA=B3=BC=EA=B1=B0=EC=9D=98=20?= =?UTF-8?q?=EB=AC=B8=EB=8B=B5=20=EA=B0=9C=EB=B3=84=20=EC=A1=B0=ED=9A=8C=20?= =?UTF-8?q?API=20=EA=B5=AC=ED=98=84=20#22?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/qna/controller/QnAController.java | 13 ++++- .../dto/response/SingleQnAResponseDto.java | 57 +++++++++++++++++++ .../domain/qna/repository/QnARepository.java | 4 ++ .../domain/qna/service/QnAService.java | 20 +++++++ .../global/exception/ErrorType.java | 2 +- .../global/exception/SuccessType.java | 3 +- 6 files changed, 95 insertions(+), 4 deletions(-) create mode 100644 src/main/java/sopt/org/umbbaServer/domain/qna/controller/dto/response/SingleQnAResponseDto.java diff --git a/src/main/java/sopt/org/umbbaServer/domain/qna/controller/QnAController.java b/src/main/java/sopt/org/umbbaServer/domain/qna/controller/QnAController.java index 48a37942..999810a8 100644 --- a/src/main/java/sopt/org/umbbaServer/domain/qna/controller/QnAController.java +++ b/src/main/java/sopt/org/umbbaServer/domain/qna/controller/QnAController.java @@ -5,6 +5,7 @@ import org.springframework.web.bind.annotation.*; import sopt.org.umbbaServer.domain.qna.controller.dto.request.TodayAnswerRequestDto; import sopt.org.umbbaServer.domain.qna.controller.dto.response.QnAListResponseDto; +import sopt.org.umbbaServer.domain.qna.controller.dto.response.SingleQnAResponseDto; import sopt.org.umbbaServer.domain.qna.controller.dto.response.TodayQnAResponseDto; import sopt.org.umbbaServer.domain.qna.service.QnAService; import sopt.org.umbbaServer.global.common.dto.ApiResponse; @@ -47,7 +48,7 @@ public ApiResponse answerTodayQuestion( return ApiResponse.success(SuccessType.ANSWER_TODAY_QUESTION_SUCCESS); } - @GetMapping("/qna/{sectionId}") + @GetMapping("/qna/list/{sectionId}") @ResponseStatus(HttpStatus.OK) public ApiResponse> getQnaList( Principal principal, @@ -55,7 +56,15 @@ public ApiResponse> getQnaList( return ApiResponse.success(SuccessType.GET_QNA_LIST_SUCCESS, qnAService.getQnaList(JwtProvider.getUserFromPrincial(principal), sectionId)); - } + @GetMapping("/qna/{qnaId}") + @ResponseStatus(HttpStatus.OK) + public ApiResponse getSingleQna( + Principal principal, + @PathVariable(name = "qnaId") Long qnaId) { + + return ApiResponse.success(SuccessType.GET_SINGLE_QNA_SUCCESS, + qnAService.getSingleQna(JwtProvider.getUserFromPrincial(principal), qnaId)); + } } diff --git a/src/main/java/sopt/org/umbbaServer/domain/qna/controller/dto/response/SingleQnAResponseDto.java b/src/main/java/sopt/org/umbbaServer/domain/qna/controller/dto/response/SingleQnAResponseDto.java new file mode 100644 index 00000000..220956e0 --- /dev/null +++ b/src/main/java/sopt/org/umbbaServer/domain/qna/controller/dto/response/SingleQnAResponseDto.java @@ -0,0 +1,57 @@ +package sopt.org.umbbaServer.domain.qna.controller.dto.response; + +import lombok.AccessLevel; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Getter; +import sopt.org.umbbaServer.domain.qna.model.QnA; +import sopt.org.umbbaServer.domain.qna.model.Question; +import sopt.org.umbbaServer.domain.user.model.User; + +@Getter +@Builder +@AllArgsConstructor(access = AccessLevel.PRIVATE) +public class SingleQnAResponseDto { + + private String section; + private String topic; + + private String opponentQuestion; + private String myQuestion; + + private String opponentAnswer; + private String myAnswer; + + private String opponentUsername; + private String myUsername; + + public static SingleQnAResponseDto of(User myUser, User opponentUser, QnA todayQnA, Question todayQuestion, boolean isMeChild) { + String opponentQuestion; + String myQuestion; + String opponentAnswer; + String myAnswer; + + if (isMeChild) { + opponentQuestion = todayQuestion.getParentQuestion(); + myQuestion = todayQuestion.getChildQuestion(); + opponentAnswer = todayQnA.getParentAnswer(); + myAnswer = todayQnA.getChildAnswer(); + } else { + opponentQuestion = todayQuestion.getChildQuestion(); + myQuestion = todayQuestion.getParentQuestion(); + opponentAnswer = todayQnA.getChildAnswer(); + myAnswer = todayQnA.getParentAnswer(); + } + + return SingleQnAResponseDto.builder() + .section(todayQuestion.getSection().getValue()) + .topic(todayQuestion.getTopic()) + .opponentQuestion(opponentQuestion) + .myQuestion(myQuestion) + .opponentAnswer(opponentAnswer) + .myAnswer(myAnswer) + .opponentUsername(opponentUser.getUsername()) + .myUsername(myUser.getUsername()) + .build(); + } +} diff --git a/src/main/java/sopt/org/umbbaServer/domain/qna/repository/QnARepository.java b/src/main/java/sopt/org/umbbaServer/domain/qna/repository/QnARepository.java index 5cdb54f6..4bc4033e 100644 --- a/src/main/java/sopt/org/umbbaServer/domain/qna/repository/QnARepository.java +++ b/src/main/java/sopt/org/umbbaServer/domain/qna/repository/QnARepository.java @@ -3,7 +3,11 @@ import org.springframework.data.repository.Repository; import sopt.org.umbbaServer.domain.qna.model.QnA; +import java.util.Optional; + public interface QnARepository extends Repository { void save(QnA qnA); + + Optional findQnAById(Long id); } diff --git a/src/main/java/sopt/org/umbbaServer/domain/qna/service/QnAService.java b/src/main/java/sopt/org/umbbaServer/domain/qna/service/QnAService.java index aacdaf98..5dcd2ce7 100644 --- a/src/main/java/sopt/org/umbbaServer/domain/qna/service/QnAService.java +++ b/src/main/java/sopt/org/umbbaServer/domain/qna/service/QnAService.java @@ -7,6 +7,7 @@ import sopt.org.umbbaServer.domain.parentchild.repository.ParentchildRepository; import sopt.org.umbbaServer.domain.qna.controller.dto.request.TodayAnswerRequestDto; import sopt.org.umbbaServer.domain.qna.controller.dto.response.QnAListResponseDto; +import sopt.org.umbbaServer.domain.qna.controller.dto.response.SingleQnAResponseDto; import sopt.org.umbbaServer.domain.qna.controller.dto.response.TodayQnAResponseDto; import sopt.org.umbbaServer.domain.qna.model.QnA; import sopt.org.umbbaServer.domain.qna.model.Question; @@ -19,6 +20,7 @@ import java.util.List; import java.util.Objects; +import java.util.Optional; import java.util.stream.Collectors; import java.util.stream.IntStream; @@ -81,6 +83,19 @@ public List getQnaList(Long userId, Long sectionId) { .collect(Collectors.toList()); } + public SingleQnAResponseDto getSingleQna(Long userId, Long qnaId) { + User myUser = getUserById(userId); + Parentchild parentchild = getParentchildByUser(myUser); + QnA targetQnA = getQnAById(qnaId); // 이거 qnA로 할건지 qna로 할건지 통일 필요 + Question todayQuestion = targetQnA.getQuestion(); + User opponentUser = getOpponentByParentchild(parentchild, userId); + + // 현재 회원이 자식이면 isMeChild가 true, 부모면 false + boolean isMeChild = myUser.getBornYear() >= opponentUser.getBornYear(); + + return SingleQnAResponseDto.of(myUser, opponentUser, targetQnA, todayQuestion, isMeChild); + } + @Transactional public void createQnA() { QnA newQnA = QnA.builder() @@ -130,6 +145,11 @@ private QnA getTodayQnAByParentchild(Parentchild parentchild) { return qnAList.get(qnAList.size() - 1); // 가장 최근의 QnA를 가져옴 } + private QnA getQnAById(Long qnaId) { + return qnARepository.findQnAById(qnaId) + .orElseThrow(() -> new CustomException(ErrorType.NOT_FOUND_QNA)); + } + private User getOpponentByParentchild(Parentchild parentchild, Long userId) { // Parentchild에 속한 User들 중 자신이 아닌 객체를 가져옴 List opponentUserList = userRepository.findUserByParentChild(parentchild) diff --git a/src/main/java/sopt/org/umbbaServer/global/exception/ErrorType.java b/src/main/java/sopt/org/umbbaServer/global/exception/ErrorType.java index e2ef01f6..35c8f19a 100644 --- a/src/main/java/sopt/org/umbbaServer/global/exception/ErrorType.java +++ b/src/main/java/sopt/org/umbbaServer/global/exception/ErrorType.java @@ -36,7 +36,7 @@ public enum ErrorType { */ INVALID_USER(HttpStatus.NOT_FOUND, "존재하지 않는 회원입니다."), NOT_EXIST_PARENT_CHILD_USER(HttpStatus.NOT_FOUND, "해당 부모자식 관계에 해당하는 유저가 존재하지 않습니다."), - + NOT_FOUND_QNA(HttpStatus.NOT_FOUND, "해당 아이디와 일치하는 QnA 데이터가 없습니다."), USER_HAVE_NO_PARENTCHILD(HttpStatus.NOT_FOUND, "회원이 속한 부모자식 관계가 없습니다."), PARENTCHILD_HAVE_NO_QNALIST(HttpStatus.NOT_FOUND, "부모자식 관계가 가지고 있는 QnA 데이터가 없습니다."), PARENTCHILD_HAVE_NO_OPPONENT(HttpStatus.NOT_FOUND, "부모자식 관계에 1명만 참여하고 있습니다."), diff --git a/src/main/java/sopt/org/umbbaServer/global/exception/SuccessType.java b/src/main/java/sopt/org/umbbaServer/global/exception/SuccessType.java index 9d3e6e12..2573875b 100644 --- a/src/main/java/sopt/org/umbbaServer/global/exception/SuccessType.java +++ b/src/main/java/sopt/org/umbbaServer/global/exception/SuccessType.java @@ -17,7 +17,8 @@ public enum SuccessType { LOGOUT_SUCCESS(HttpStatus.OK, "로그아웃에 성공했습니다."), KAKAO_ACCESS_TOKEN_SUCCESS(HttpStatus.OK, "카카오 엑세스 토큰을 가져오는데 성공했습니다"), GET_TODAY_QNA_SUCCESS(HttpStatus.OK, "일일문답 조회에 성공했습니다."), - GET_QNA_LIST_SUCCESS(HttpStatus.OK, "섹션별 과거의 문답 리스트 조회에 성공했습니다"), + GET_QNA_LIST_SUCCESS(HttpStatus.OK, "섹션별 과거의 문답 리스트 조회에 성공했습니다."), + GET_SINGLE_QNA_SUCCESS(HttpStatus.OK, "과거의 문답 개별 조회에 성공했습니다."), MATCH_PARENT_CHILD_SUCCESS(HttpStatus.OK, "부모자식 관계 매칭에 성공했습니다."),