Skip to content

Commit

Permalink
Merge pull request #124 from Team-Umbba/feat/#117-mypage_api
Browse files Browse the repository at this point in the history
[FEAT] 마이페이지 API 구현
  • Loading branch information
jun02160 authored Feb 11, 2024
2 parents 92a07e3 + 924d3ee commit 2261d87
Show file tree
Hide file tree
Showing 8 changed files with 117 additions and 5 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ build/
!**/src/main/**/build/
!**/src/test/**/build/

umbba-test.http

## Querydsl
umbba-domain/src/main/generated

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
@@ -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.*;
Expand Down Expand Up @@ -92,4 +94,10 @@ public ApiResponse<GetInvitationResponseDto> invitation(Principal principal) {
return ApiResponse.success(SuccessType.GET_INVITE_CODE_SUCCESS, qnAService.getInvitation(JwtProvider.getUserFromPrincial(principal)));
}

@GetMapping("/user/me")
@ResponseStatus(HttpStatus.OK)
public ApiResponse<MyUserInfoResponseDto> getMyUserInfo(Principal principal) {
return ApiResponse.success(SuccessType.GET_MY_USER_INFO_SUCCESS, qnAService.getUserInfo(getUserFromPrincial(principal)));
}

}
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -200,6 +208,35 @@ public void filterAllQuestion(Long userId) {
}
}

// 마이페이지 - 부모자식 관계 정보 조회
public MyUserInfoResponseDto getUserInfo(final Long userId) {

User myUser = getUserById(userId);
Parentchild parentchild = getParentchildByUser(myUser);
List<User> 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);
}

/*
리팩토링을 위해 아래로 뺀 메서드들
*/
Expand Down Expand Up @@ -305,10 +342,6 @@ else if (childList.get(3) != YES || parentList.get(3) != YES) {
}


/*
리팩토링을 위해 아래로 뺀 메서드들 끝
*/

// 메인페이지 정보
public GetMainViewResponseDto getMainInfo(Long userId) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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일 이후 문답이 정상적으로 시작되었습니다."),

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<User> parentChildUsers, ParentchildRelation relation) {
Expand Down

0 comments on commit 2261d87

Please sign in to comment.