diff --git a/.gitignore b/.gitignore index 8ab1ea93..8ad08fda 100644 --- a/.gitignore +++ b/.gitignore @@ -7,6 +7,8 @@ build/ !**/src/main/**/build/ !**/src/test/**/build/ +umbba-test.http + ## Querydsl umbba-domain/src/main/generated diff --git a/umbba-api/src/main/java/sopt/org/umbba/api/config/SecurityConfig.java b/umbba-api/src/main/java/sopt/org/umbba/api/config/SecurityConfig.java index 76254bd2..20fdfdc5 100644 --- a/umbba-api/src/main/java/sopt/org/umbba/api/config/SecurityConfig.java +++ b/umbba-api/src/main/java/sopt/org/umbba/api/config/SecurityConfig.java @@ -21,7 +21,7 @@ public class SecurityConfig { private static final String[] AUTH_WHITELIST = { "/kakao/**", "/login", "/reissue", - "/qna/**", "onboard/**", "/home", "/dummy", + "/qna/**", "onboard/**", "/home", "/dummy", "/user/me", // "/log-out", "/test", "/profile", "/health", "/actuator/health", "/alarm/qna", "/alarm/drink", diff --git a/umbba-api/src/main/java/sopt/org/umbba/api/controller/qna/QnAController.java b/umbba-api/src/main/java/sopt/org/umbba/api/controller/qna/QnAController.java index ee967e36..27918114 100644 --- a/umbba-api/src/main/java/sopt/org/umbba/api/controller/qna/QnAController.java +++ b/umbba-api/src/main/java/sopt/org/umbba/api/controller/qna/QnAController.java @@ -1,5 +1,7 @@ package sopt.org.umbba.api.controller.qna; +import static sopt.org.umbba.api.config.jwt.JwtProvider.*; + import lombok.RequiredArgsConstructor; import org.springframework.http.HttpStatus; import org.springframework.web.bind.annotation.*; @@ -92,4 +94,10 @@ public ApiResponse invitation(Principal principal) { return ApiResponse.success(SuccessType.GET_INVITE_CODE_SUCCESS, qnAService.getInvitation(JwtProvider.getUserFromPrincial(principal))); } + @GetMapping("/user/me") + @ResponseStatus(HttpStatus.OK) + public ApiResponse getMyUserInfo(Principal principal) { + return ApiResponse.success(SuccessType.GET_MY_USER_INFO_SUCCESS, qnAService.getUserInfo(getUserFromPrincial(principal))); + } + } diff --git a/umbba-api/src/main/java/sopt/org/umbba/api/controller/qna/dto/response/MyUserInfoResponseDto.java b/umbba-api/src/main/java/sopt/org/umbba/api/controller/qna/dto/response/MyUserInfoResponseDto.java new file mode 100644 index 00000000..93f1a0ab --- /dev/null +++ b/umbba-api/src/main/java/sopt/org/umbba/api/controller/qna/dto/response/MyUserInfoResponseDto.java @@ -0,0 +1,58 @@ +package sopt.org.umbba.api.controller.qna.dto.response; + +import static sopt.org.umbba.domain.domain.parentchild.ParentchildRelation.*; + +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.databind.PropertyNamingStrategies; +import com.fasterxml.jackson.databind.annotation.JsonNaming; + +import lombok.Builder; +import lombok.Getter; +import sopt.org.umbba.domain.domain.parentchild.Parentchild; +import sopt.org.umbba.domain.domain.parentchild.ParentchildRelation; +import sopt.org.umbba.domain.domain.qna.QnA; +import sopt.org.umbba.domain.domain.qna.QuestionSection; +import sopt.org.umbba.domain.domain.user.User; + +@Getter +@Builder +@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class) +@JsonInclude(JsonInclude.Include.NON_NULL) +public class MyUserInfoResponseDto { + + private String myUsername; + private String myUserType; + private String opponentUsername; + private String opponentUserType; + + private String parentchildRelation; + private Boolean isMeChild; + + private String section; + private Long matchedDate; + private Integer qnaCnt; + + public static MyUserInfoResponseDto of(User myUser, User opponentUser, Parentchild parentchild, QnA qnA, long date, int qnaCnt) { + + return MyUserInfoResponseDto.builder() + .myUsername(myUser.getUsername()) + .myUserType(getUserType(parentchild.getRelation(), myUser.isMeChild())) + .opponentUsername(opponentUser.getUsername()) + .opponentUserType(getUserType(parentchild.getRelation(), opponentUser.isMeChild())) + .parentchildRelation(parentchild.getRelation().getValue()) + .isMeChild(myUser.isMeChild()) + .section(qnA.getQuestion().getSection().getValue()) + .matchedDate(date) // 일수와 문답 수는 다를 수 있음 + .qnaCnt(qnaCnt).build(); + } + + // 아직 매칭된 유저가 없는 경우 + public static MyUserInfoResponseDto of(User myUser) { + + return MyUserInfoResponseDto.builder() + .myUsername(myUser.getUsername()) + .section(QuestionSection.YOUNG.getValue()) + .matchedDate(0L) + .qnaCnt(0).build(); + } +} diff --git a/umbba-api/src/main/java/sopt/org/umbba/api/service/example.txt b/umbba-api/src/main/java/sopt/org/umbba/api/service/example.txt new file mode 100644 index 00000000..945c9b46 --- /dev/null +++ b/umbba-api/src/main/java/sopt/org/umbba/api/service/example.txt @@ -0,0 +1 @@ +. \ No newline at end of file diff --git a/umbba-api/src/main/java/sopt/org/umbba/api/service/qna/QnAService.java b/umbba-api/src/main/java/sopt/org/umbba/api/service/qna/QnAService.java index f0336af6..4424791d 100644 --- a/umbba-api/src/main/java/sopt/org/umbba/api/service/qna/QnAService.java +++ b/umbba-api/src/main/java/sopt/org/umbba/api/service/qna/QnAService.java @@ -4,6 +4,8 @@ import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; + +import sopt.org.umbba.api.controller.qna.dto.response.MyUserInfoResponseDto; import sopt.org.umbba.api.controller.qna.dto.request.TodayAnswerRequestDto; import sopt.org.umbba.api.controller.qna.dto.response.*; import sopt.org.umbba.api.service.notification.NotificationService; @@ -19,6 +21,12 @@ import sopt.org.umbba.domain.domain.user.repository.UserRepository; import javax.validation.constraints.NotNull; + +import java.time.LocalDateTime; +import java.time.temporal.ChronoUnit; +import java.util.List; +import java.util.Objects; +import java.util.Optional; import java.util.*; import java.util.stream.Collectors; @@ -200,6 +208,35 @@ public void filterAllQuestion(Long userId) { } } + // 마이페이지 - 부모자식 관계 정보 조회 + public MyUserInfoResponseDto getUserInfo(final Long userId) { + + User myUser = getUserById(userId); + Parentchild parentchild = getParentchildByUser(myUser); + List opponentUserList = userRepository.findUserByParentChild(parentchild) + .stream() + .filter(user -> !user.getId().equals(userId)) + .collect(Collectors.toList()); + + // 매칭된 상대 유저가 없는 경우 + if (opponentUserList.isEmpty()) { + return MyUserInfoResponseDto.of(myUser); + } + + User opponentUser = getOpponentByParentchild(parentchild, userId); + QnA todayQnA = getTodayQnAByParentchild(parentchild); + + int qnaCnt = parentchild.getCount(); + if (!todayQnA.isChildAnswer() || !todayQnA.isParentAnswer()) { + qnaCnt -= 1; + } + + LocalDateTime firstQnADate = parentchild.getQnaList().get(0).getCreatedAt(); + long qnaDate = ChronoUnit.DAYS.between(firstQnADate, LocalDateTime.now()); + + return MyUserInfoResponseDto.of(myUser, opponentUser, parentchild, todayQnA, qnaDate, qnaCnt); + } + /* 리팩토링을 위해 아래로 뺀 메서드들 */ @@ -305,10 +342,6 @@ else if (childList.get(3) != YES || parentList.get(3) != YES) { } - /* - 리팩토링을 위해 아래로 뺀 메서드들 끝 - */ - // 메인페이지 정보 public GetMainViewResponseDto getMainInfo(Long userId) { diff --git a/umbba-common/src/main/java/sopt/org/umbba/common/exception/SuccessType.java b/umbba-common/src/main/java/sopt/org/umbba/common/exception/SuccessType.java index df69b310..e68ab841 100644 --- a/umbba-common/src/main/java/sopt/org/umbba/common/exception/SuccessType.java +++ b/umbba-common/src/main/java/sopt/org/umbba/common/exception/SuccessType.java @@ -25,6 +25,7 @@ public enum SuccessType { PUSH_ALARM_SUCCESS(HttpStatus.OK, "푸시알림 전송에 성공했습니다."), PUSH_ALARM_PERIODIC_SUCCESS(HttpStatus.OK, "오늘의 질문 푸시알림 활성에 성공했습니다."), REMIND_QUESTION_SUCCESS(HttpStatus.OK, "상대방에게 질문을 리마인드 하는 데 성공했습니다."), + GET_MY_USER_INFO_SUCCESS(HttpStatus.OK, "마이페이지 내 정보 조회에 성공했습니다."), TEST_SUCCESS(HttpStatus.OK, "데모데이 테스트용 API 호출에 성공했습니다."), RESTART_QNA_SUCCESS(HttpStatus.OK, "7일 이후 문답이 정상적으로 시작되었습니다."), diff --git a/umbba-domain/src/main/java/sopt/org/umbba/domain/domain/parentchild/ParentchildRelation.java b/umbba-domain/src/main/java/sopt/org/umbba/domain/domain/parentchild/ParentchildRelation.java index 39a84dc2..43191e5a 100644 --- a/umbba-domain/src/main/java/sopt/org/umbba/domain/domain/parentchild/ParentchildRelation.java +++ b/umbba-domain/src/main/java/sopt/org/umbba/domain/domain/parentchild/ParentchildRelation.java @@ -59,6 +59,15 @@ public static ParentchildRelation relation(String gender, String relationInfo, b throw new CustomException(ErrorType.INVALID_PARENT_CHILD_RELATION_INFO); } + // 아들 | 딸 | 엄마 | 아빠 구분 + public static String getUserType(ParentchildRelation relation, boolean isChild) { + if (isChild) { + return relation.childGender.equals("남자") ? "아들" : "딸"; + } else { + return relation.parentGender.equals("남자") ? "아빠" : "엄마"; + } + } + // 자식 유저와 부모 유저의 gender와 isMeChild 필드를 통해 ParentchildRelation을 구분하는 로직 public static boolean validate(List parentChildUsers, ParentchildRelation relation) {