diff --git a/src/main/java/sopt/org/umbbaServer/domain/parentchild/controller/ParentchildController.java b/src/main/java/sopt/org/umbbaServer/domain/parentchild/controller/ParentchildController.java index e4d83f7d..2f43c784 100644 --- a/src/main/java/sopt/org/umbbaServer/domain/parentchild/controller/ParentchildController.java +++ b/src/main/java/sopt/org/umbbaServer/domain/parentchild/controller/ParentchildController.java @@ -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 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 inviteRelation(@Valid @RequestBody InviteCodeRequestDto request) { log.info("getUserFromPrincipal에는 문제가 없어요 - 요청 초대코드: {}", request.getInviteCode()); @@ -41,11 +43,19 @@ public ApiResponse inviteRelation(@Valid @RequestBody In } - @PatchMapping("/receive") + @PatchMapping("/onboard/receive") @ResponseStatus(HttpStatus.OK) public ApiResponse onboardReceive(@Valid @RequestBody OnboardingReceiveRequestDto request) { return ApiResponse.success(SuccessType.CREATE_PARENT_CHILD_SUCCESS, parentchildService.onboardReceive(request)); } + @GetMapping("/home/invite") + @ResponseStatus(HttpStatus.OK) + public ApiResponse invitation(Principal principal) { + + return ApiResponse.success(SuccessType.GET_INVITE_CODE_SUCCESS, parentchildService.getInvitation(JwtProvider.getUserFromPrincial(principal))); + } + + } diff --git a/src/main/java/sopt/org/umbbaServer/domain/parentchild/controller/dto/response/GetInviteCodeResponseDto.java b/src/main/java/sopt/org/umbbaServer/domain/parentchild/controller/dto/response/GetInviteCodeResponseDto.java index 18a3aa50..0d85cbfc 100644 --- a/src/main/java/sopt/org/umbbaServer/domain/parentchild/controller/dto/response/GetInviteCodeResponseDto.java +++ b/src/main/java/sopt/org/umbbaServer/domain/parentchild/controller/dto/response/GetInviteCodeResponseDto.java @@ -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(); + } } diff --git a/src/main/java/sopt/org/umbbaServer/domain/parentchild/dao/ParentchildDao.java b/src/main/java/sopt/org/umbbaServer/domain/parentchild/dao/ParentchildDao.java new file mode 100644 index 00000000..4230854b --- /dev/null +++ b/src/main/java/sopt/org/umbbaServer/domain/parentchild/dao/ParentchildDao.java @@ -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(); + } +} diff --git a/src/main/java/sopt/org/umbbaServer/domain/parentchild/service/ParentchildService.java b/src/main/java/sopt/org/umbbaServer/domain/parentchild/service/ParentchildService.java index 7d082e6c..15ea906e 100644 --- a/src/main/java/sopt/org/umbbaServer/domain/parentchild/service/ParentchildService.java +++ b/src/main/java/sopt/org/umbbaServer/domain/parentchild/service/ParentchildService.java @@ -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; @@ -27,6 +30,7 @@ @Transactional(readOnly = true) public class ParentchildService { + private final ParentchildDao parentchildDao; private final ParentchildRepository parentchildRepository; private final UserRepository userRepository; @@ -162,4 +166,13 @@ private List getParentChildUsers(Parentchild newMatchRelation) { } + + // 메인페이지에서 초대장 보내기 (초대코드 조회) + public GetInviteCodeResponseDto getInvitation(Long userId) { + + Parentchild parentchild = parentchildDao.findByUserId(userId); + + return GetInviteCodeResponseDto.of(parentchild); + } + } diff --git a/src/main/java/sopt/org/umbbaServer/domain/qna/controller/QnAController.java b/src/main/java/sopt/org/umbbaServer/domain/qna/controller/QnAController.java index 999810a8..4538e326 100644 --- a/src/main/java/sopt/org/umbbaServer/domain/qna/controller/QnAController.java +++ b/src/main/java/sopt/org/umbbaServer/domain/qna/controller/QnAController.java @@ -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; @@ -67,4 +70,12 @@ public ApiResponse getSingleQna( return ApiResponse.success(SuccessType.GET_SINGLE_QNA_SUCCESS, qnAService.getSingleQna(JwtProvider.getUserFromPrincial(principal), qnaId)); } + + @GetMapping("/home") + @ResponseStatus(HttpStatus.OK) + public ApiResponse home(Principal principal) { + + return ApiResponse.success(SuccessType.GET_MAIN_HOME_SUCCESS, qnAService.getMainInfo(JwtProvider.getUserFromPrincial(principal))); + } + } diff --git a/src/main/java/sopt/org/umbbaServer/domain/qna/controller/dto/response/GetMainViewResponseDto.java b/src/main/java/sopt/org/umbbaServer/domain/qna/controller/dto/response/GetMainViewResponseDto.java new file mode 100644 index 00000000..cc4d282b --- /dev/null +++ b/src/main/java/sopt/org/umbbaServer/domain/qna/controller/dto/response/GetMainViewResponseDto.java @@ -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(); + } +} diff --git a/src/main/java/sopt/org/umbbaServer/domain/qna/dao/QnADao.java b/src/main/java/sopt/org/umbbaServer/domain/qna/dao/QnADao.java new file mode 100644 index 00000000..7205e60d --- /dev/null +++ b/src/main/java/sopt/org/umbbaServer/domain/qna/dao/QnADao.java @@ -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 { + + @PersistenceContext + private EntityManager em; + + + // READ + + // 유저 아이디로 QnA 리스트 조회하기 + public List 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 query = em.createQuery(jpql, QnA.class); + + log.info("query 실행 성공: {}", query); + List qnAList = query + .setParameter("id", userId) + .getResultList(); + log.info("query 실행 결과: {}", qnAList.toString()); + + return qnAList; + } + + // 유저 아이디로 최근 QnA 조회하기 +} diff --git a/src/main/java/sopt/org/umbbaServer/domain/qna/repository/QnARepository.java b/src/main/java/sopt/org/umbbaServer/domain/qna/repository/QnARepository.java index 4bc4033e..0fe78a92 100644 --- a/src/main/java/sopt/org/umbbaServer/domain/qna/repository/QnARepository.java +++ b/src/main/java/sopt/org/umbbaServer/domain/qna/repository/QnARepository.java @@ -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; diff --git a/src/main/java/sopt/org/umbbaServer/domain/qna/service/QnAService.java b/src/main/java/sopt/org/umbbaServer/domain/qna/service/QnAService.java index 5dcd2ce7..5e25a034 100644 --- a/src/main/java/sopt/org/umbbaServer/domain/qna/service/QnAService.java +++ b/src/main/java/sopt/org/umbbaServer/domain/qna/service/QnAService.java @@ -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; @@ -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); @@ -162,4 +166,16 @@ private User getOpponentByParentchild(Parentchild parentchild, Long userId) { return opponentUserList.get(0); } + + + // 메인페이지 정보 + public GetMainViewResponseDto getMainInfo(Long userId) { + + List qnAList = qnADao.findQnASByUserId(userId); + QnA lastQna = qnAList.get(qnAList.size()-1); + + return GetMainViewResponseDto.of(lastQna); + + } + } diff --git a/src/main/java/sopt/org/umbbaServer/global/exception/SuccessType.java b/src/main/java/sopt/org/umbbaServer/global/exception/SuccessType.java index 2573875b..5bd249a1 100644 --- a/src/main/java/sopt/org/umbbaServer/global/exception/SuccessType.java +++ b/src/main/java/sopt/org/umbbaServer/global/exception/SuccessType.java @@ -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, "초대장을 보낼 코드 조회에 성공했습니다."), /** diff --git a/src/main/resources/application.yaml b/src/main/resources/application.yaml index f1f57e88..538281ac 100644 --- a/src/main/resources/application.yaml +++ b/src/main/resources/application.yaml @@ -50,3 +50,6 @@ logging: type: descriptor: sql: trace + +server: + port: 9091 \ No newline at end of file