Skip to content

Commit

Permalink
Merge pull request #23 from Team-Umbba/feat/#22-get_qna_list
Browse files Browse the repository at this point in the history
[FEAT] 과거의 질문과 답변 리스트 + 개별 조회 API
  • Loading branch information
jun02160 authored Jul 11, 2023
2 parents e82fd08 + bc6d513 commit 0de743e
Show file tree
Hide file tree
Showing 13 changed files with 170 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@ public interface ParentchildRepository extends Repository<Parentchild, Long> {
// CREATE
void save(Parentchild parentchild);

@Query("SELECT u FROM User u WHERE u.parentChild = :parentChild")
List<User> findUsersByParentChild(@Param("parentChild") Parentchild parentChild);

// READ
Optional<Parentchild> findById(Long id);
Optional<Parentchild> findByInviteCode(String inviteCode);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
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.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;
Expand All @@ -12,6 +14,7 @@

import javax.validation.Valid;
import java.security.Principal;
import java.util.List;

@RestController
@RequiredArgsConstructor
Expand Down Expand Up @@ -40,8 +43,28 @@ 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/list/{sectionId}")
@ResponseStatus(HttpStatus.OK)
public ApiResponse<List<QnAListResponseDto>> getQnaList(
Principal principal,
@PathVariable(name = "sectionId") Long sectionId) {

return ApiResponse.success(SuccessType.GET_QNA_LIST_SUCCESS,
qnAService.getQnaList(JwtProvider.getUserFromPrincial(principal), sectionId));
}

@GetMapping("/qna/{qnaId}")
@ResponseStatus(HttpStatus.OK)
public ApiResponse<SingleQnAResponseDto> getSingleQna(
Principal principal,
@PathVariable(name = "qnaId") Long qnaId) {

return ApiResponse.success(SuccessType.GET_SINGLE_QNA_SUCCESS,
qnAService.getSingleQna(JwtProvider.getUserFromPrincial(principal), qnaId));
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
public class TodayQnAResponseDto {

private String section;
private String effect;
private String topic;
private String opponentQuestion;
private String myQuestion;

Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,5 @@ public class Question extends AuditingTimeEntity {
private QuestionSection section;

@Column(nullable = false)
@Enumerated(EnumType.STRING)
private QuestionEffect effect;
private String topic;
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<QnA, Long> {

void save(QnA qnA);

Optional<QnA> findQnAById(Long id);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
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.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;
Expand All @@ -17,7 +19,10 @@
import sopt.org.umbbaServer.global.exception.ErrorType;

import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

@Service
@RequiredArgsConstructor
Expand All @@ -32,7 +37,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);

Expand All @@ -43,10 +48,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();
Expand All @@ -58,6 +63,39 @@ public void answerTodayQuestion(TodayAnswerRequestDto request, Long userId) {
}
}

public List<QnAListResponseDto> getQnaList(Long userId, Long sectionId) {
User myUser = getUserById(userId);
Parentchild parentchild = getParentchildByUser(myUser);
List<QnA> qnaList = getQnAListByParentchild(parentchild);
User opponentUser = getOpponentByParentchild(parentchild, userId);

boolean isMeChild = myUser.getBornYear() >= opponentUser.getBornYear();

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(qnaList.indexOf(qna) + 1)
.question(question)
.build();
})
.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()
Expand Down Expand Up @@ -89,7 +127,16 @@ private Parentchild getParentchildByUser(User user) {
return parentchild;
}

private QnA getQnAByParentchild(Parentchild parentchild) {
private List<QnA> getQnAListByParentchild(Parentchild parentchild) {
List<QnA> qnAList = parentchild.getQnaList();
if (qnAList == null || qnAList.isEmpty()) {
throw new CustomException(ErrorType.PARENTCHILD_HAVE_NO_QNALIST);
}

return qnAList;
}

private QnA getTodayQnAByParentchild(Parentchild parentchild) {
List<QnA> qnAList = parentchild.getQnaList();
if (qnAList == null || qnAList.isEmpty()) {
throw new CustomException(ErrorType.PARENTCHILD_HAVE_NO_QNALIST);
Expand All @@ -98,9 +145,14 @@ private QnA getQnAByParentchild(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<User> opponentUserList = parentchildRepository.findUsersByParentChild(parentchild)
List<User> opponentUserList = userRepository.findUserByParentChild(parentchild)
.stream()
.filter(user -> !user.getId().equals(userId))
.collect(Collectors.toList());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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명만 참여하고 있습니다."),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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_SINGLE_QNA_SUCCESS(HttpStatus.OK, "과거의 문답 개별 조회에 성공했습니다."),
MATCH_PARENT_CHILD_SUCCESS(HttpStatus.OK, "부모자식 관계 매칭에 성공했습니다."),


Expand Down

0 comments on commit 0de743e

Please sign in to comment.