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

[REFACTOR] 꼼꼼한 예외처리 추가 #75

Merged
merged 12 commits into from
Jul 21, 2023
Merged
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<img src="https://user-images.githubusercontent.com/80024278/254717698-0849c495-c344-4cd2-a369-8b5def6c1154.jpg" width="750"/>
<br/>


## 🌸 금쪽이들
| 이동섭 | 박예준 |
| :----------------------------------------------------------: |:----------------------------------------------------------------------------------------------------------------------------------:|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,24 +33,24 @@ public ApiResponse<OnboardingInviteResponseDto> onboardInvite(@RequestBody @Vali

Long userId = JwtProvider.getUserFromPrincial(principal);
OnboardingInviteResponseDto response = parentchildService.onboardInvite(userId, request);
qnAService.filterFirstQuestion(userId, request.getOnboardingAnswerList());
qnAService.filterFirstQuestion(userId);

return ApiResponse.success(SuccessType.CREATE_PARENT_CHILD_SUCCESS, response);
}

@PatchMapping("/onboard/match")
@ResponseStatus(HttpStatus.OK)
@ResponseStatus(HttpStatus.CREATED)
public ApiResponse<InviteResultResponseDto> inviteRelation(@RequestBody @Valid final InviteCodeRequestDto request, Principal principal) {
return ApiResponse.success(SuccessType.MATCH_PARENT_CHILD_SUCCESS, parentchildService.matchRelation(JwtProvider.getUserFromPrincial(principal), request));
}

@PatchMapping("/onboard/receive")
@ResponseStatus(HttpStatus.OK)
@ResponseStatus(HttpStatus.CREATED)
public ApiResponse<OnboardingReceiveResponseDto> onboardReceive(@RequestBody @Valid final OnboardingReceiveRequestDto request, Principal principal) throws InterruptedException {

Long userId = JwtProvider.getUserFromPrincial(principal);
OnboardingReceiveResponseDto response = parentchildService.onboardReceive(userId, request);
qnAService.filterAllQuestion(userId, request.getOnboardingAnswerList());
qnAService.filterAllQuestion(userId);

return ApiResponse.success(SuccessType.CREATE_PARENT_CHILD_SUCCESS, response);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@ public class OnboardingInviteRequestDto {
@JsonFormat(pattern = "kk:mm")
private LocalTime pushTime;

@NotEmpty // TODO 여기서 걸러지게 만들어야함
@NotEmpty
private List<String> onboardingAnswerList;
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public static ParentchildRelation relation(String gender, String relationInfo, b
if (relationInfo.equals("아들")) {
return ParentchildRelation.DAD_SON;
} else if (relationInfo.equals("딸")) {
return ParentchildRelation.DAD_DAU; // TODO 클라에서 둘 중 하나의 값만 받도록 처리하니까 else if 구문 빼도 무관
return ParentchildRelation.DAD_DAU;
}
} else if(gender.equals("여자")) { // 엄마
if (relationInfo.equals("아들")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import sopt.org.umbbaServer.domain.parentchild.model.Parentchild;
import sopt.org.umbbaServer.domain.parentchild.model.ParentchildRelation;
import sopt.org.umbbaServer.domain.parentchild.repository.ParentchildRepository;
import sopt.org.umbbaServer.domain.qna.model.OnboardingAnswer;
import sopt.org.umbbaServer.domain.user.model.User;
import sopt.org.umbbaServer.domain.user.repository.UserRepository;
import sopt.org.umbbaServer.global.config.ScheduleConfig;
Expand All @@ -24,6 +25,7 @@
import sopt.org.umbbaServer.global.util.fcm.FCMService;

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

@Slf4j
@Service
Expand All @@ -42,34 +44,55 @@ public class ParentchildService {
public OnboardingInviteResponseDto onboardInvite(Long userId, OnboardingInviteRequestDto request) {

User user = getUserById(userId);
user.updateOnboardingInfo(
request.getUserInfo().getName(),
request.getUserInfo().getGender(),
request.getUserInfo().getBornYear()
);
log.info("isInvitorChild 요청값: {}", request.getIsInvitorChild());
user.updateIsMeChild(request.getIsInvitorChild());
log.info("업데이트 된 isMeChild 필드: {}", user.isMeChild());

Parentchild parentchild = Parentchild.builder()
.inviteCode(generateInviteCode())
.isInvitorChild(request.getIsInvitorChild())
.relation(ParentchildRelation.relation(request.getUserInfo().getGender(), request.getRelationInfo(), request.getIsInvitorChild()))
.pushTime(request.getPushTime()) // TODO 케이스에 따라 없을 수도 있음
.count(1)
.build();
parentchildRepository.save(parentchild);
user.updateParentchild(parentchild);
user.updateIsMatchFinish(true);
log.info("userInfo: {}", request.getUserInfo().getBornYear());
return OnboardingInviteResponseDto.of(parentchild, user);
if (user.getParentChild() == null) {
user.updateOnboardingInfo(
request.getUserInfo().getName(),
request.getUserInfo().getGender(),
request.getUserInfo().getBornYear()
);
log.info("isInvitorChild 요청값: {}", request.getIsInvitorChild());
user.updateIsMeChild(request.getIsInvitorChild());
log.info("업데이트 된 isMeChild 필드: {}", user.isMeChild());

Parentchild parentchild = Parentchild.builder()
.inviteCode(generateInviteCode())
.isInvitorChild(request.getIsInvitorChild())
.relation(ParentchildRelation.relation(request.getUserInfo().getGender(), request.getRelationInfo(), request.getIsInvitorChild()))
.pushTime(request.getPushTime())
.count(1)
.build();
parentchildRepository.save(parentchild);
user.updateParentchild(parentchild);
user.updateIsMatchFinish(true);
log.info("userInfo: {}", request.getUserInfo().getBornYear());

// String을 Enum으로 변경
List<OnboardingAnswer> onboardingAnswerList = request.getOnboardingAnswerList().stream()
.map(OnboardingAnswer::of)
.collect(Collectors.toList());

if (onboardingAnswerList.size() != 5) {
throw new CustomException(ErrorType.INVALID_ONBOARDING_ANSWER_SIZE);
}

if (getUserById(userId).isMeChild()) {
parentchild.changeChildOnboardingAnswerList(onboardingAnswerList);
} else {
parentchild.changeParentOnboardingAnswerList(onboardingAnswerList);
}

return OnboardingInviteResponseDto.of(parentchild, user);
}

throw new CustomException(ErrorType.ALREADY_EXISTS_PARENT_CHILD_USER);
}


// [수신] 초대받는 측의 온보딩 정보 입력
@Transactional
public OnboardingReceiveResponseDto onboardReceive(Long userId, OnboardingReceiveRequestDto request) throws InterruptedException {

if (getParentchildByUserId(userId) != null) {
if (getUserById(userId).getParentChild() != null) {

User user = getUserById(userId);
user.updateOnboardingInfo(
Expand All @@ -78,10 +101,25 @@ public OnboardingReceiveResponseDto onboardReceive(Long userId, OnboardingReceiv
request.getUserInfo().getBornYear()
);

Parentchild parentchild = getParentchildByUserId(userId);
Parentchild parentchild = user.getParentChild();
// parentchild.updateInfo(); TODO 온보딩 송수신 측의 관계 정보가 불일치한 경우에 대한 처리
List<User> parentChildUsers = getParentChildUsers(parentchild);

// String을 Enum으로 변경
List<OnboardingAnswer> onboardingAnswerList = request.getOnboardingAnswerList().stream()
.map(OnboardingAnswer::of)
.collect(Collectors.toList());

if (onboardingAnswerList.size() != 5) {
throw new CustomException(ErrorType.INVALID_ONBOARDING_ANSWER_SIZE);
}

if (getUserById(userId).isMeChild()) {
parentchild.changeChildOnboardingAnswerList(onboardingAnswerList);
} else {
parentchild.changeParentOnboardingAnswerList(onboardingAnswerList);
}

/*if (!ParentchildRelation.validate(parentChildUsers, parentchild.getRelation())) {
throw new CustomException(ErrorType.INVALID_PARENT_CHILD_RELATION);
}*/
Expand Down Expand Up @@ -116,8 +154,7 @@ public InviteResultResponseDto matchRelation(Long userId, InviteCodeRequestDto r
throw new CustomException(ErrorType.ALREADY_EXISTS_PARENT_CHILD_USER);
}

// TODO ParentChild에 연관된 User 수에 따른 예외처리
// TODO 하나의 유저는 하나의 관계만 가지도록 예외처리
// TODO ParentChild에 연관된 User 수에 따른 예외 메시지 출력
user.updateParentchild(newMatchRelation);
user.updateIsMatchFinish(true);
log.info("로그인한 유저가 성립된 Parentchild Id: {}", user.getParentChild().getId());
Expand All @@ -135,13 +172,6 @@ public List<User> getParentChildUsers(Parentchild newMatchRelation) {
}


private Parentchild getParentchildByUserId(Long userId) {

return parentchildDao.findByUserId(userId).orElseThrow(
() -> new CustomException(ErrorType.USER_HAVE_NO_PARENTCHILD)
);
}

private User getUserById(Long userId) {

return userRepository.findById(userId).orElseThrow(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import sopt.org.umbbaServer.domain.parentchild.dao.ParentchildDao;
Expand Down Expand Up @@ -117,21 +118,14 @@ public SingleQnAResponseDto getSingleQna(Long userId, Long qnaId) {
}

@Transactional
public void filterFirstQuestion(Long userId, List<String> onboardingAnswerStringList) {
public void filterFirstQuestion(Long userId) {

Parentchild parentchild = getParentchildByUserId(userId);

// String을 Enum으로 변경
List<OnboardingAnswer> onboardingAnswerList = onboardingAnswerStringList.stream()
.map(OnboardingAnswer::of)
.collect(Collectors.toList());

if (getUserById(userId).isMeChild()) {
parentchild.changeChildOnboardingAnswerList(onboardingAnswerList);
} else {
parentchild.changeParentOnboardingAnswerList(onboardingAnswerList);
Parentchild parentchild = getUserById(userId).getParentChild();
if (parentchild == null) {
throw new CustomException(ErrorType.USER_HAVE_NO_PARENTCHILD);
}


// 첫번째 질문은 MVP 단에서는 고정
QnA newQnA = QnA.builder()
.question(questionRepository.findByType(FIX).get(0))
Expand All @@ -145,19 +139,11 @@ public void filterFirstQuestion(Long userId, List<String> onboardingAnswerString
}

@Transactional
public void filterAllQuestion(Long userId, List<String> onboardingAnswerStringList) {

Parentchild parentchild = getParentchildByUserId(userId);

// String을 Enum으로 변경
List<OnboardingAnswer> onboardingAnswerList = onboardingAnswerStringList.stream()
.map(OnboardingAnswer::of)
.collect(Collectors.toList());
public void filterAllQuestion(Long userId) {

if (getUserById(userId).isMeChild()) {
parentchild.changeChildOnboardingAnswerList(onboardingAnswerList);
} else {
parentchild.changeParentOnboardingAnswerList(onboardingAnswerList);
Parentchild parentchild = getUserById(userId).getParentChild();
if (parentchild == null) {
throw new CustomException(ErrorType.USER_HAVE_NO_PARENTCHILD);
}

List<OnboardingAnswer> childList = parentchild.getChildOnboardingAnswerList();
Expand Down Expand Up @@ -206,12 +192,6 @@ private Parentchild getParentchildByUser(User user) {
return parentchild;
}

private Parentchild getParentchildByUserId(Long userId) {

return parentchildDao.findByUserId(userId).orElseThrow(
() -> new CustomException(ErrorType.USER_HAVE_NO_PARENTCHILD)
);
}

private List<QnA> getQnAListByParentchild(Parentchild parentchild) {
List<QnA> qnaList = parentchild.getQnaList();
Expand Down Expand Up @@ -306,14 +286,24 @@ else if (childList.get(3) != YES || parentList.get(3) != YES) {
// 메인페이지 정보
public GetMainViewResponseDto getMainInfo(Long userId) {

Parentchild parentchild = getParentchildByUserId(userId);
Parentchild parentchild = getParentchild(userId);

List<QnA> qnaList = getQnAListByParentchild(parentchild);

QnA lastQna = qnaList.get(parentchild.getCount() - 1);

return GetMainViewResponseDto.of(lastQna, parentchild.getCount());
}

@NotNull
private Parentchild getParentchild(Long userId) {
Parentchild parentchild = getUserById(userId).getParentChild();
if (parentchild == null) {
throw new CustomException(ErrorType.USER_HAVE_NO_PARENTCHILD);
}
return parentchild;
}

private GetInvitationResponseDto invitation(Long userId) {

User user = getUserById(userId);
Expand Down
Loading