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 #24

Merged
merged 8 commits into from
Jul 11, 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 @@ -7,30 +7,32 @@
import sopt.org.umbbaServer.domain.parentchild.controller.dto.request.InviteCodeRequestDto;
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.OnboardingReceiveResponseDto;
import sopt.org.umbbaServer.domain.parentchild.controller.dto.response.OnboardingInviteResponseDto;
import sopt.org.umbbaServer.domain.parentchild.service.ParentchildService;
import sopt.org.umbbaServer.global.common.dto.ApiResponse;
import sopt.org.umbbaServer.global.config.jwt.JwtProvider;
import sopt.org.umbbaServer.global.exception.SuccessType;

import javax.validation.Valid;
import java.security.Principal;

@Slf4j
@RestController
@RequestMapping("/onboard")
@RequiredArgsConstructor
public class ParentchildController {

private final ParentchildService parentchildService;

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

@PatchMapping("/match")
@PatchMapping("/onboard/match")
@ResponseStatus(HttpStatus.OK)
public ApiResponse<InviteResultResponeDto> inviteRelation(@Valid @RequestBody InviteCodeRequestDto request) {
log.info("getUserFromPrincipal에는 문제가 없어요 - 요청 초대코드: {}", request.getInviteCode());
Expand All @@ -41,11 +43,19 @@ public ApiResponse<InviteResultResponeDto> inviteRelation(@Valid @RequestBody In

}

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


@GetMapping("/home/invite")
@ResponseStatus(HttpStatus.OK)
public ApiResponse<GetInviteCodeResponseDto> invitation(Principal principal) {

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


}
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
package sopt.org.umbbaServer.domain.parentchild.controller.dto.response;

import lombok.Builder;
import lombok.Getter;
import sopt.org.umbbaServer.domain.parentchild.model.Parentchild;

@Getter
@Builder
public class GetInviteCodeResponseDto {

private String inviteCode;

public static GetInviteCodeResponseDto of(Parentchild parentchild) {
return GetInviteCodeResponseDto.builder()
.inviteCode(parentchild.getInviteCode())
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package sopt.org.umbbaServer.domain.parentchild.dao;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;
import sopt.org.umbbaServer.domain.parentchild.model.Parentchild;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

@Slf4j
@Repository
public class ParentchildDao {

@PersistenceContext
private EntityManager em;

public Parentchild findByUserId(Long userId) {

String jpql = "SELECT pc FROM Parentchild pc " +
"JOIN User u ON u.parentChild = pc " +
"WHERE u.id = :id";

return em.createQuery(jpql, Parentchild.class)
.setParameter("id", userId)
.getSingleResult();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
import sopt.org.umbbaServer.domain.parentchild.controller.dto.request.InviteCodeRequestDto;
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.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.OnboardingReceiveResponseDto;
import sopt.org.umbbaServer.domain.parentchild.controller.dto.response.OnboardingInviteResponseDto;
Expand All @@ -27,6 +30,7 @@
@Transactional(readOnly = true)
public class ParentchildService {

private final ParentchildDao parentchildDao;
private final ParentchildRepository parentchildRepository;
private final UserRepository userRepository;

Expand Down Expand Up @@ -162,4 +166,13 @@ private List<User> getParentChildUsers(Parentchild newMatchRelation) {

}


// 메인페이지에서 초대장 보내기 (초대코드 조회)
public GetInviteCodeResponseDto getInvitation(Long userId) {

Parentchild parentchild = parentchildDao.findByUserId(userId);

return GetInviteCodeResponseDto.of(parentchild);
}

Comment on lines +171 to +177
Copy link
Member

Choose a reason for hiding this comment

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

지금 두 API가 2개의 Service에 나눠져 있구, 1개의 Controller에서 호출되고 있어가지구!
이거 어떻게 할지 회의좀 해봐야겠다!

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import sopt.org.umbbaServer.domain.parentchild.controller.dto.response.GetInviteCodeResponseDto;
import sopt.org.umbbaServer.domain.parentchild.service.ParentchildService;
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.service.QnAService;
import sopt.org.umbbaServer.global.common.dto.ApiResponse;
Expand Down Expand Up @@ -67,4 +70,12 @@ public ApiResponse<SingleQnAResponseDto> getSingleQna(
return ApiResponse.success(SuccessType.GET_SINGLE_QNA_SUCCESS,
qnAService.getSingleQna(JwtProvider.getUserFromPrincial(principal), qnaId));
}

@GetMapping("/home")
@ResponseStatus(HttpStatus.OK)
public ApiResponse<GetMainViewResponseDto> home(Principal principal) {

return ApiResponse.success(SuccessType.GET_MAIN_HOME_SUCCESS, qnAService.getMainInfo(JwtProvider.getUserFromPrincial(principal)));
}

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

import lombok.Builder;
import lombok.Getter;
import sopt.org.umbbaServer.domain.qna.model.QnA;

@Getter
@Builder
public class GetMainViewResponseDto {

private String section;
private String topic;

public static GetMainViewResponseDto of (QnA qnA) {
return GetMainViewResponseDto.builder()
.section(qnA.getQuestion().getSection().getValue())
.topic(qnA.getQuestion().getTopic())
.build();
}
}
47 changes: 47 additions & 0 deletions src/main/java/sopt/org/umbbaServer/domain/qna/dao/QnADao.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package sopt.org.umbbaServer.domain.qna.dao;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.repository.Query;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import sopt.org.umbbaServer.domain.qna.model.QnA;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import javax.sql.DataSource;
import java.util.List;
import java.util.Optional;

@Slf4j
@Repository
public class QnADao {
Copy link
Member

Choose a reason for hiding this comment

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

기존에 있던 것도 이런식으로 참고해서 리팩토링 진행할게!


@PersistenceContext
private EntityManager em;


// READ

// 유저 아이디로 QnA 리스트 조회하기
public List<QnA> findQnASByUserId(Long userId) {
log.info("jpql 실행 전");
String jpql = "SELECT q FROM Parentchild pc " +
"JOIN pc.qnaList q " +
"LEFT JOIN User u ON u.parentChild.id = pc.id " +
"WHERE u.id = :id";

TypedQuery<QnA> query = em.createQuery(jpql, QnA.class);

log.info("query 실행 성공: {}", query);
List<QnA> qnAList = query
.setParameter("id", userId)
.getResultList();
log.info("query 실행 결과: {}", qnAList.toString());

return qnAList;
}

// 유저 아이디로 최근 QnA 조회하기
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package sopt.org.umbbaServer.domain.qna.repository;

import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.Repository;
import sopt.org.umbbaServer.domain.qna.model.QnA;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import sopt.org.umbbaServer.domain.parentchild.controller.dto.response.GetInviteCodeResponseDto;
import sopt.org.umbbaServer.domain.parentchild.model.Parentchild;
import sopt.org.umbbaServer.domain.parentchild.repository.ParentchildRepository;
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.dao.QnADao;
import sopt.org.umbbaServer.domain.qna.model.QnA;
import sopt.org.umbbaServer.domain.qna.model.Question;
import sopt.org.umbbaServer.domain.qna.repository.QnARepository;
Expand All @@ -33,6 +36,7 @@ public class QnAService {
private final QuestionRepository questionRepository;
private final UserRepository userRepository;
private final ParentchildRepository parentchildRepository;
private final QnADao qnADao;

public TodayQnAResponseDto getTodayQnA(Long userId) {
User myUser = getUserById(userId);
Expand Down Expand Up @@ -162,4 +166,16 @@ private User getOpponentByParentchild(Parentchild parentchild, Long userId) {

return opponentUserList.get(0);
}


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

List<QnA> qnAList = qnADao.findQnASByUserId(userId);
QnA lastQna = qnAList.get(qnAList.size()-1);

return GetMainViewResponseDto.of(lastQna);

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ public enum SuccessType {
GET_QNA_LIST_SUCCESS(HttpStatus.OK, "섹션별 과거의 문답 리스트 조회에 성공했습니다."),
GET_SINGLE_QNA_SUCCESS(HttpStatus.OK, "과거의 문답 개별 조회에 성공했습니다."),
MATCH_PARENT_CHILD_SUCCESS(HttpStatus.OK, "부모자식 관계 매칭에 성공했습니다."),

GET_MAIN_HOME_SUCCESS(HttpStatus.OK, "메인 홈 화면 정보 불러오기에 성공했습니다."),
GET_INVITE_CODE_SUCCESS(HttpStatus.OK, "초대장을 보낼 코드 조회에 성공했습니다."),


/**
Expand Down
3 changes: 3 additions & 0 deletions src/main/resources/application.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,6 @@ logging:
type:
descriptor:
sql: trace

server:
port: 9091