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

[FEAT] 초대장 조회 API로 로직 분리 #46

Merged
merged 2 commits into from
Jul 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@
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.GetMainViewResponseDto;
import sopt.org.umbbaServer.domain.qna.controller.dto.response.TodayQnAResponseDto;
import sopt.org.umbbaServer.domain.qna.controller.dto.response.*;
import sopt.org.umbbaServer.domain.qna.service.QnAService;
import sopt.org.umbbaServer.global.common.dto.ApiResponse;
import sopt.org.umbbaServer.global.config.jwt.JwtProvider;
Expand All @@ -30,6 +27,7 @@ public ApiResponse<TodayQnAResponseDto> getTodayQna(Principal principal) {
return ApiResponse.success(SuccessType.GET_TODAY_QNA_SUCCESS, qnAService.getTodayQnA(JwtProvider.getUserFromPrincial(principal)));
}


@PostMapping("/qna/answer")
@ResponseStatus(HttpStatus.CREATED)
public ApiResponse answerTodayQuestion(
Expand Down Expand Up @@ -68,4 +66,10 @@ public ApiResponse<GetMainViewResponseDto> home(Principal principal) {
return ApiResponse.success(SuccessType.GET_MAIN_HOME_SUCCESS, qnAService.getMainInfo(JwtProvider.getUserFromPrincial(principal)));
}

@GetMapping("/home/case")
public ApiResponse<GetInvitationResponseDto> invitation(Principal principal) {

return ApiResponse.success(SuccessType.GET_INVITE_CODE_SUCCESS, qnAService.getInvitation(JwtProvider.getUserFromPrincial(principal)));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package sopt.org.umbbaServer.domain.qna.controller.dto.response;

import com.fasterxml.jackson.databind.PropertyNamingStrategies;
import com.fasterxml.jackson.databind.annotation.JsonNaming;
import lombok.Builder;
import lombok.Getter;

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

private int responseCase; // case를 1,2,3으로 구분 (Client)

// 예외상황에 따른 필드
private String inviteCode;
private String inviteUsername;
private String installUrl; // TODO Firebase Dynamic Link

private Boolean relativeUserActive;


// 1. 오늘의 질문을 조회한 일반적인 경우
public static GetInvitationResponseDto of () {
return GetInvitationResponseDto.builder()
.responseCase(1)
.relativeUserActive(true)
.build();
}

// 2. 아직 부모자식 관계가 매칭되지 않은 경우
public static GetInvitationResponseDto of (String inviteCode, String inviteUsername, String installUrl) {
return GetInvitationResponseDto.builder()
.responseCase(2)
.inviteCode(inviteCode)
.inviteUsername(inviteUsername)
.installUrl(installUrl)
.relativeUserActive(true)
.build();
}

// 3. 부모자식 중 상대 측 유저가 탈퇴한 경우
public static GetInvitationResponseDto of (boolean relativeUserActive) {
return GetInvitationResponseDto.builder()
.responseCase(3)
.relativeUserActive(relativeUserActive)
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@
@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class)
public class TodayQnAResponseDto {

private int responseCase; // case를 1,2,3으로 구분 (Client)

private Long qnaId;
private String section;
private String topic;
Expand All @@ -30,15 +28,7 @@ public class TodayQnAResponseDto {
private String opponentUsername;
private String myUsername;

// 예외상황에 따른 필드
private String inviteCode;
private String inviteUsername;
private String installUrl; // TODO Firebase Dynamic Link

private Boolean relativeUserActive;


// 1. 오늘의 질문을 조회한 일반적인 경우
public static TodayQnAResponseDto of(User myUser, User opponentUser, QnA todayQnA, Question todayQuestion, boolean isMeChild) {
String opponentQuestion;
String myQuestion;
Expand All @@ -64,7 +54,6 @@ public static TodayQnAResponseDto of(User myUser, User opponentUser, QnA todayQn
}

return TodayQnAResponseDto.builder()
.responseCase(1)
.qnaId(todayQnA.getId())
.section(todayQuestion.getSection().getValue())
.topic(todayQuestion.getTopic())
Expand All @@ -79,23 +68,6 @@ public static TodayQnAResponseDto of(User myUser, User opponentUser, QnA todayQn
.build();
}

// 2. 아직 부모자식 관계가 매칭되지 않은 경우
public static TodayQnAResponseDto of (String inviteCode, String inviteUsername, String installUrl) {
return TodayQnAResponseDto.builder()
.responseCase(2)
.inviteCode(inviteCode)
.inviteUsername(inviteUsername)
.installUrl(installUrl)
.build();
}

// 3. 부모자식 중 상대 측 유저가 탈퇴한 경우
public static TodayQnAResponseDto of (boolean relativeUserActive) {
return TodayQnAResponseDto.builder()
.responseCase(3)
.relativeUserActive(relativeUserActive)
.build();
}
}


Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,10 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import sopt.org.umbbaServer.domain.parentchild.dao.ParentchildDao;
import sopt.org.umbbaServer.domain.qna.controller.dto.response.*;
import sopt.org.umbbaServer.domain.qna.model.*;
import sopt.org.umbbaServer.domain.parentchild.model.Parentchild;
import sopt.org.umbbaServer.domain.qna.controller.dto.request.TodayAnswerRequestDto;
import sopt.org.umbbaServer.domain.qna.controller.dto.response.GetMainViewResponseDto;
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.dao.QnADao;
import sopt.org.umbbaServer.domain.qna.repository.QnARepository;
import sopt.org.umbbaServer.domain.qna.repository.QuestionRepository;
Expand Down Expand Up @@ -46,6 +43,18 @@ public class QnAService {

public TodayQnAResponseDto getTodayQnA(Long userId) {

User myUser = getUserById(userId);
Parentchild parentchild = getParentchildByUser(myUser);
QnA todayQnA = getTodayQnAByParentchild(parentchild);
Question todayQuestion = todayQnA.getQuestion();
User opponentUser = getOpponentByParentchild(parentchild, userId);


return TodayQnAResponseDto.of(myUser, opponentUser, todayQnA, todayQuestion, myUser.isMeChild());
}

public GetInvitationResponseDto getInvitation(Long userId) {

Optional<User> matchUser = parentchildDao.findMatchUserByUserId(userId);
log.info("matchUser: {} -> parentchildDao.findMatchUserByUserId()의 결과", matchUser);

Expand All @@ -57,14 +66,7 @@ public TodayQnAResponseDto getTodayQnA(Long userId) {
return withdrawUser();
}

User myUser = getUserById(userId);
Parentchild parentchild = getParentchildByUser(myUser);
QnA todayQnA = getTodayQnAByParentchild(parentchild);
Question todayQuestion = todayQnA.getQuestion();
User opponentUser = getOpponentByParentchild(parentchild, userId);


return TodayQnAResponseDto.of(myUser, opponentUser, todayQnA, todayQuestion, myUser.isMeChild());
return GetInvitationResponseDto.of();
}

@Transactional
Expand Down Expand Up @@ -273,17 +275,17 @@ public GetMainViewResponseDto getMainInfo(Long userId) {
return GetMainViewResponseDto.of(lastQna, qnAList.size());
}

private TodayQnAResponseDto invitation(Long userId) {
private GetInvitationResponseDto invitation(Long userId) {

User user = getUserById(userId);
Parentchild parentchild = parentchildDao.findByUserId(userId).orElseThrow(
() -> new CustomException(ErrorType.USER_HAVE_NO_PARENTCHILD)
);

return TodayQnAResponseDto.of(parentchild.getInviteCode(), user.getUsername(), "url"); // TODO url 설정 필요 (Firebase)
return GetInvitationResponseDto.of(parentchild.getInviteCode(), user.getUsername(), "url"); // TODO url 설정 필요 (Firebase)
}

private TodayQnAResponseDto withdrawUser() {
return TodayQnAResponseDto.of(false);
private GetInvitationResponseDto withdrawUser() {
return GetInvitationResponseDto.of(false);
}
}