Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FIX] 요청, 응답 값에 관한 에러 해결 #32

Merged
merged 9 commits into from
Jul 14, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import sopt.org.umbbaServer.domain.parentchild.controller.dto.request.OnboardingInviteRequestDto;
import sopt.org.umbbaServer.domain.parentchild.controller.dto.request.OnboardingReceiveRequestDto;
import sopt.org.umbbaServer.domain.parentchild.controller.dto.response.GetInviteCodeResponseDto;
import sopt.org.umbbaServer.domain.parentchild.controller.dto.response.InviteResultResponeDto;
import sopt.org.umbbaServer.domain.parentchild.controller.dto.response.InviteResultResponseDto;
import sopt.org.umbbaServer.domain.parentchild.controller.dto.response.OnboardingReceiveResponseDto;
import sopt.org.umbbaServer.domain.parentchild.controller.dto.response.OnboardingInviteResponseDto;
import sopt.org.umbbaServer.domain.parentchild.service.ParentchildService;
Expand All @@ -28,13 +28,13 @@ public class ParentchildController {

@PostMapping("/onboard/invite")
@ResponseStatus(HttpStatus.CREATED)
public ApiResponse<OnboardingInviteResponseDto> onboardInvite(@Valid @RequestBody final OnboardingInviteRequestDto request, Principal principal) {
public ApiResponse<OnboardingInviteResponseDto> onboardInvite(@RequestBody @Valid final OnboardingInviteRequestDto request, Principal principal) {
return ApiResponse.success(SuccessType.CREATE_PARENT_CHILD_SUCCESS, parentchildService.onboardInvite(JwtProvider.getUserFromPrincial(principal), request));
}

@PatchMapping("/onboard/match")
@ResponseStatus(HttpStatus.OK)
public ApiResponse<InviteResultResponeDto> inviteRelation(@Valid @RequestBody final InviteCodeRequestDto request, Principal principal) {
public ApiResponse<InviteResultResponseDto> inviteRelation(@RequestBody @Valid final InviteCodeRequestDto request, Principal principal) {
log.info("getUserFromPrincipal에는 문제가 없어요 - 요청 초대코드: {}", request.getInviteCode());

// Long userId = JwtProvider.getUserFromPrincial(principal);
Expand All @@ -45,7 +45,7 @@ public ApiResponse<InviteResultResponeDto> inviteRelation(@Valid @RequestBody fi

@PatchMapping("/onboard/receive")
@ResponseStatus(HttpStatus.OK)
public ApiResponse<OnboardingReceiveResponseDto> onboardReceive(@Valid @RequestBody final OnboardingReceiveRequestDto request, Principal principal) {
public ApiResponse<OnboardingReceiveResponseDto> onboardReceive(@RequestBody @Valid final OnboardingReceiveRequestDto request, Principal principal) {
return ApiResponse.success(SuccessType.CREATE_PARENT_CHILD_SUCCESS, parentchildService.onboardReceive(JwtProvider.getUserFromPrincial(principal), request));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
package sopt.org.umbbaServer.domain.parentchild.controller.dto.request;


import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import com.fasterxml.jackson.databind.annotation.JsonNaming;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;

@Getter
@NoArgsConstructor(access = AccessLevel.PRIVATE)
@JsonNaming(PropertyNamingStrategy.SnakeCaseStrategy.class)
public class InviteCodeRequestDto {

@NotBlank(message = "초대코드는 필수 입력 값입니다.")
@Pattern(regexp = "[A-Z]{4}-[A-Za-z0-9]{6}", message = "초대코드 형식에 맞지 않습니다.")
@Size(max = 11)
private String inviteCode;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,36 @@

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
import com.fasterxml.jackson.databind.annotation.JsonNaming;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import sopt.org.umbbaServer.domain.user.controller.dto.request.UserInfoDto;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.time.LocalTime;

@Slf4j
@Getter
@AllArgsConstructor(access = AccessLevel.PRIVATE)
@JsonNaming(PropertyNamingStrategy.SnakeCaseStrategy.class)
@NoArgsConstructor(access = AccessLevel.PRIVATE)
@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class)
public class OnboardingInviteRequestDto {

@NotNull
@Valid
private UserInfoDto userInfo;

@JsonProperty("is_invitor_child")
private boolean isInvitorChild;
private Boolean isInvitorChild;

private String relationInfo; // 아들 or 딸 | 아빠 or 엄마

@JsonFormat(pattern = "kk:mm")
private LocalTime pushTime;

// TODO 선택질문에 대한 답변 필드 추가 필요

}
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
package sopt.org.umbbaServer.domain.parentchild.controller.dto.request;

import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
import com.fasterxml.jackson.databind.annotation.JsonNaming;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import sopt.org.umbbaServer.domain.user.controller.dto.request.UserInfoDto;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;

@Getter
@AllArgsConstructor(access = AccessLevel.PRIVATE)
@JsonNaming(PropertyNamingStrategy.SnakeCaseStrategy.class)
@NoArgsConstructor(access = AccessLevel.PRIVATE)
@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class)
public class OnboardingReceiveRequestDto {

@NotBlank(message = "부모자식 관계 아이디는 필수 입력 값입니다.")
private Long parentChildId;

@NotNull
@Valid
private UserInfoDto userInfo;

// TODO 선택질문에 대한 답변 필드 추가 필요
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package sopt.org.umbbaServer.domain.parentchild.controller.dto.response;

import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
import com.fasterxml.jackson.databind.annotation.JsonNaming;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import sopt.org.umbbaServer.domain.parentchild.model.Parentchild;
Expand All @@ -15,17 +13,17 @@

@Getter
@Builder
@JsonNaming(PropertyNamingStrategy.SnakeCaseStrategy.class)
public class InviteResultResponeDto {
@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class)
public class InviteResultResponseDto {

private Long parentchildId;
private List<UserInfoDto> parentChildUsers;
private List<UserInfoDto> parentchildUsers;
private String parentchildRelation;

public static InviteResultResponeDto of(Parentchild parentchild, List<User> parentChildUsers) {
return InviteResultResponeDto.builder()
public static InviteResultResponseDto of(Parentchild parentchild, List<User> parentChildUsers) {
return InviteResultResponseDto.builder()
.parentchildId(parentchild.getId())
.parentChildUsers(parentChildUsers.stream().map(u -> UserInfoDto.of(u)).collect(Collectors.toList()))
.parentchildUsers(parentChildUsers.stream().map(u -> UserInfoDto.of(u)).collect(Collectors.toList()))
.parentchildRelation(parentchild.getRelation().getValue())
.build();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
package sopt.org.umbbaServer.domain.parentchild.controller.dto.response;

import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
import com.fasterxml.jackson.databind.annotation.JsonNaming;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import sopt.org.umbbaServer.domain.parentchild.model.Parentchild;
import sopt.org.umbbaServer.domain.user.controller.dto.request.UserInfoDto;
import sopt.org.umbbaServer.domain.user.model.User;

import java.time.LocalDate;
import java.time.LocalTime;

@Getter
@Builder
@JsonNaming(PropertyNamingStrategy.SnakeCaseStrategy.class)
@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class)
public class OnboardingInviteResponseDto {

private Long parentchildId;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package sopt.org.umbbaServer.domain.parentchild.controller.dto.response;

import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
import com.fasterxml.jackson.databind.annotation.JsonNaming;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import sopt.org.umbbaServer.domain.parentchild.model.Parentchild;
Expand All @@ -15,19 +13,19 @@

@Getter
@Builder
@JsonNaming(PropertyNamingStrategy.SnakeCaseStrategy.class)
@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class)
public class OnboardingReceiveResponseDto {

private UserInfoDto userInfo;

private InviteResultResponeDto parentchildInfo;
private InviteResultResponseDto parentchildInfo;

private LocalTime pushTime;

public static OnboardingReceiveResponseDto of(Parentchild parentchild, User user, List<User> parentChildUsers) {
return OnboardingReceiveResponseDto.builder()
.userInfo(UserInfoDto.of(user))
.parentchildInfo(InviteResultResponeDto.of(parentchild, parentChildUsers))
.parentchildInfo(InviteResultResponseDto.of(parentchild, parentChildUsers))
.pushTime(parentchild.getPushTime())
.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@ public Parentchild findByUserId(Long userId) {
.setParameter("id", userId)
.getSingleResult();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
import sopt.org.umbbaServer.domain.parentchild.controller.dto.request.OnboardingReceiveRequestDto;
import sopt.org.umbbaServer.domain.parentchild.controller.dto.response.GetInviteCodeResponseDto;
import sopt.org.umbbaServer.domain.parentchild.dao.ParentchildDao;
import sopt.org.umbbaServer.domain.qna.controller.dto.response.GetMainViewResponseDto;
import sopt.org.umbbaServer.domain.parentchild.controller.dto.response.InviteResultResponeDto;
import sopt.org.umbbaServer.domain.parentchild.controller.dto.response.InviteResultResponseDto;
import sopt.org.umbbaServer.domain.parentchild.controller.dto.response.OnboardingReceiveResponseDto;
import sopt.org.umbbaServer.domain.parentchild.controller.dto.response.OnboardingInviteResponseDto;
import sopt.org.umbbaServer.domain.parentchild.model.Parentchild;
Expand All @@ -23,7 +22,7 @@
import sopt.org.umbbaServer.global.exception.ErrorType;

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

@Slf4j
@Service
Expand All @@ -40,23 +39,21 @@ public class ParentchildService {
public OnboardingInviteResponseDto onboardInvite(Long userId, OnboardingInviteRequestDto request) {

// TODO userId 토큰 provider에서 정보 꺼내오도록
User user = userRepository.findById(userId).orElseThrow(
() -> new CustomException(ErrorType.INVALID_USER)
);
User user = getUserById(userId);
user.updateOnboardingInfo(
request.getUserInfo().getName(),
request.getUserInfo().getGender(),
request.getUserInfo().getBornYear()
);
log.info("isInvitorChild 요청값: {}", request.isInvitorChild());
user.updateIsMeChild(request.isInvitorChild());
log.info("isInvitorChild 요청값: {}", request.getIsInvitorChild());
user.updateIsMeChild(request.getIsInvitorChild());
log.info("업데이트 된 isMeChild 필드: {}", user.isMeChild());


Parentchild parentchild = Parentchild.builder()
.inviteCode(generateInviteCode())
.isInvitorChild(request.isInvitorChild())
.relation(getRelation(request.getUserInfo().getGender(), request.getRelationInfo(), request.isInvitorChild()))
.isInvitorChild(request.getIsInvitorChild())
.relation(getRelation(request.getUserInfo().getGender(), request.getRelationInfo(), request.getIsInvitorChild()))
.pushTime(request.getPushTime()) // TODO 케이스에 따라 없을 수도 있음
.build();
parentchildRepository.save(parentchild);
Expand All @@ -69,9 +66,7 @@ public OnboardingInviteResponseDto onboardInvite(Long userId, OnboardingInviteRe
@Transactional
public OnboardingReceiveResponseDto onboardReceive(Long userId, OnboardingReceiveRequestDto request) {

User user = userRepository.findById(userId).orElseThrow(
() -> new CustomException(ErrorType.INVALID_USER)
);
User user = getUserById(userId);
user.updateOnboardingInfo(
request.getUserInfo().getName(),
request.getUserInfo().getGender(),
Expand All @@ -80,10 +75,7 @@ public OnboardingReceiveResponseDto onboardReceive(Long userId, OnboardingReceiv


// TODO 추가 질문 답변 저장
Parentchild parentchild = parentchildRepository.findById(request.getParentChildId()).orElseThrow(
() -> new CustomException(ErrorType.INVALID_PARENT_CHILD_RELATION)
);
user.updateIsMeChild(!parentchild.isInvitorChild());
Parentchild parentchild = getParentchildById(parentchildRepository.findById(request.getParentChildId()), ErrorType.NOT_EXIST_PARENT_CHILD_RELATION);

// parentchild.updateInfo();
List<User> parentChildUsers = getParentChildUsers(parentchild);
Expand Down Expand Up @@ -136,21 +128,25 @@ private String generateInviteCode() {

// 초대코드 확인 후 부모자식 관계 성립
@Transactional
public InviteResultResponeDto matchRelation(Long userId, InviteCodeRequestDto request) {
public InviteResultResponseDto matchRelation(Long userId, InviteCodeRequestDto request) {

log.info("ParentchlidService 실행 - 요청 초대코드: {}", request.getInviteCode());
Parentchild newMatchRelation = parentchildRepository.findByInviteCode(request.getInviteCode()).orElseThrow(
() -> new CustomException(ErrorType.INVALID_INVITE_CODE)
);
User user = userRepository.findById(userId).orElseThrow(
() -> new CustomException(ErrorType.INVALID_USER)
);
Parentchild newMatchRelation = getParentchildById(parentchildRepository.findByInviteCode(request.getInviteCode()), ErrorType.INVALID_INVITE_CODE);
User user = getUserById(userId);
user.updateIsMeChild(!newMatchRelation.isInvitorChild());

if (user.getParentChild() != null) {
throw new CustomException(ErrorType.ALREADY_EXISTS_PARENT_CHILD_USER);
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이부분 다시 확인 부탁드려욥!

// TODO ParentChild에 연관된 User 수에 따른 예외처리
// TODO 하나의 유저는 하나의 관계만 가지도록 예외처리
user.updateParentchild(newMatchRelation);
log.info("로그인한 유저가 성립된 Parentchild Id: {}", user.getParentChild().getId());

List<User> parentChildUsers = getParentChildUsers(newMatchRelation);

return InviteResultResponeDto.of(newMatchRelation, parentChildUsers);
return InviteResultResponseDto.of(newMatchRelation, parentChildUsers);
}

private List<User> getParentChildUsers(Parentchild newMatchRelation) {
Expand Down Expand Up @@ -178,8 +174,24 @@ private List<User> getParentChildUsers(Parentchild newMatchRelation) {
public GetInviteCodeResponseDto getInvitation(Long userId) {

Parentchild parentchild = parentchildDao.findByUserId(userId);
if (parentchild == null) {
throw new CustomException(ErrorType.NOT_MATCH_PARENT_CHILD_RELATION);
}

return GetInviteCodeResponseDto.of(parentchild);
}

private Parentchild getParentchildById(Optional<Parentchild> parentchildRepository, ErrorType notExistParentChildRelation) {
return parentchildRepository.orElseThrow(
() -> new CustomException(notExistParentChildRelation)
);
}

private User getUserById(Long userId) {
User user = userRepository.findById(userId).orElseThrow(
() -> new CustomException(ErrorType.INVALID_USER)
);
return user;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@ public class GetMainViewResponseDto {

private String section;
private String topic;
private Integer count;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

변수명 index로 통일하면 좋을 것 같아욤!


public static GetMainViewResponseDto of (QnA qnA) {
public static GetMainViewResponseDto of (QnA qnA, int count) {
return GetMainViewResponseDto.builder()
.section(qnA.getQuestion().getSection().getValue())
.topic(qnA.getQuestion().getTopic())
.count(count)
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ public GetMainViewResponseDto getMainInfo(Long userId) {
List<QnA> qnAList = qnADao.findQnASByUserId(userId);
QnA lastQna = qnAList.get(qnAList.size()-1);

return GetMainViewResponseDto.of(lastQna);
return GetMainViewResponseDto.of(lastQna, qnAList.size()-1);

}

Expand Down
Loading