From 55da26595fbc89f72d9eec7f73247f46953b5d2a Mon Sep 17 00:00:00 2001 From: david-parkk Date: Fri, 16 Aug 2024 14:57:55 +0900 Subject: [PATCH 1/8] feat: onBoardingState --- .../onboarding/domain/OnBoardingState.java | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 src/main/java/com/ourMenu/backend/domain/onboarding/domain/OnBoardingState.java diff --git a/src/main/java/com/ourMenu/backend/domain/onboarding/domain/OnBoardingState.java b/src/main/java/com/ourMenu/backend/domain/onboarding/domain/OnBoardingState.java new file mode 100644 index 0000000..c157cb7 --- /dev/null +++ b/src/main/java/com/ourMenu/backend/domain/onboarding/domain/OnBoardingState.java @@ -0,0 +1,32 @@ +package com.ourMenu.backend.domain.onboarding.domain; + +import jakarta.persistence.Entity; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.GenerationType; +import jakarta.persistence.Id; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.NoArgsConstructor; + +@Entity +@Builder +@NoArgsConstructor +@AllArgsConstructor +public class OnBoardingState { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + private Long userId; + private int questionId; + private AnswerType answerType; + + public static OnBoardingState toEntity(Long userId, int questionId, AnswerType answerType) { + return OnBoardingState.builder() + .userId(userId) + .questionId(questionId) + .answerType(answerType) + .build(); + } +} From 93f65206b187921cf69ccd7152aed2c8c4982cf8 Mon Sep 17 00:00:00 2001 From: david-parkk Date: Fri, 16 Aug 2024 14:58:29 +0900 Subject: [PATCH 2/8] feat: onBoardingStateRepository --- .../onboarding/dao/OnBoardingStateRepository.java | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 src/main/java/com/ourMenu/backend/domain/onboarding/dao/OnBoardingStateRepository.java diff --git a/src/main/java/com/ourMenu/backend/domain/onboarding/dao/OnBoardingStateRepository.java b/src/main/java/com/ourMenu/backend/domain/onboarding/dao/OnBoardingStateRepository.java new file mode 100644 index 0000000..b4ec36f --- /dev/null +++ b/src/main/java/com/ourMenu/backend/domain/onboarding/dao/OnBoardingStateRepository.java @@ -0,0 +1,14 @@ +package com.ourMenu.backend.domain.onboarding.dao; + +import com.ourMenu.backend.domain.menu.domain.Tag; +import com.ourMenu.backend.domain.onboarding.domain.OnBoardingState; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +import java.util.Optional; + +@Repository +public interface OnBoardingStateRepository extends JpaRepository { + + Optional findByOnBoardingState(Long userId); +} From 6f59eb2b07466a6396ef915ef315507bf92b1e1b Mon Sep 17 00:00:00 2001 From: david-parkk Date: Fri, 16 Aug 2024 14:59:08 +0900 Subject: [PATCH 3/8] feat: exception --- .../exception/SearchResultNotFoundException.java | 12 ++++++++++++ .../ourMenu/backend/global/exception/ErrorCode.java | 4 +++- 2 files changed, 15 insertions(+), 1 deletion(-) create mode 100644 src/main/java/com/ourMenu/backend/domain/onboarding/exception/SearchResultNotFoundException.java diff --git a/src/main/java/com/ourMenu/backend/domain/onboarding/exception/SearchResultNotFoundException.java b/src/main/java/com/ourMenu/backend/domain/onboarding/exception/SearchResultNotFoundException.java new file mode 100644 index 0000000..049b61a --- /dev/null +++ b/src/main/java/com/ourMenu/backend/domain/onboarding/exception/SearchResultNotFoundException.java @@ -0,0 +1,12 @@ +package com.ourMenu.backend.domain.onboarding.exception; + +import com.ourMenu.backend.global.exception.CustomException; +import com.ourMenu.backend.global.exception.ErrorCode; + +public class SearchResultNotFoundException extends CustomException { + + public SearchResultNotFoundException(){ + super(ErrorCode.ON_BOARDING_STATE_NOT_FOUND); + } + +} \ No newline at end of file diff --git a/src/main/java/com/ourMenu/backend/global/exception/ErrorCode.java b/src/main/java/com/ourMenu/backend/global/exception/ErrorCode.java index bfd1966..6cdb9d2 100644 --- a/src/main/java/com/ourMenu/backend/global/exception/ErrorCode.java +++ b/src/main/java/com/ourMenu/backend/global/exception/ErrorCode.java @@ -39,8 +39,10 @@ public enum ErrorCode { IMAGE_NOT_LOADED_ERROR(HttpStatus.BAD_REQUEST, "I400", "이미지 업로드에 실패하였습니다."), //place - PLACE_NOT_FOUND(HttpStatus.NOT_FOUND, "P401", "식당을 찾을 수 없습니다."); + PLACE_NOT_FOUND(HttpStatus.NOT_FOUND, "P401", "식당을 찾을 수 없습니다."), + //onboarding + ON_BOARDING_STATE_NOT_FOUND(HttpStatus.NOT_FOUND,"O401","저장된 온보딩 상태가 없습니다.") ; private final HttpStatus status; From 49c2e0efe63061e13c83e4efb2c65ef418451910 Mon Sep 17 00:00:00 2001 From: david-parkk Date: Fri, 16 Aug 2024 15:03:37 +0900 Subject: [PATCH 4/8] fix: GET/onboarding/recommend --- .../onboarding/api/OnBoardingController.java | 10 ++++-- .../application/OnBoardingService.java | 36 ++++++++++++++++--- 2 files changed, 39 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/ourMenu/backend/domain/onboarding/api/OnBoardingController.java b/src/main/java/com/ourMenu/backend/domain/onboarding/api/OnBoardingController.java index 110ac3d..31f4e69 100644 --- a/src/main/java/com/ourMenu/backend/domain/onboarding/api/OnBoardingController.java +++ b/src/main/java/com/ourMenu/backend/domain/onboarding/api/OnBoardingController.java @@ -1,7 +1,6 @@ package com.ourMenu.backend.domain.onboarding.api; import com.ourMenu.backend.domain.menu.domain.Menu; -import com.ourMenu.backend.domain.menu.dto.response.MenuDto; import com.ourMenu.backend.domain.onboarding.api.response.GetOnboardingResponse; import com.ourMenu.backend.domain.onboarding.api.response.GetQuestionRecommands; import com.ourMenu.backend.domain.onboarding.api.response.GetTagRecommends; @@ -39,7 +38,7 @@ public ApiResponse> getOnboarding() { public ApiResponse getQuestionRecommend(@RequestParam("questionId") int questionId, @RequestParam("answer") AnswerType answerType, @UserId Long userId) { - List menus = onBoardService.findStoreByQuestionAnswer(userId, questionId, answerType); + List menus = onBoardService.saveAndFindStoreByQuestionAnswer(userId, questionId, answerType); return ApiUtils.success(GetQuestionRecommands.toDto(menus, questionId, answerType)); } @@ -51,10 +50,15 @@ public ApiResponse> getQuestionRecommend(@UserId Long use List getTagRecommendsList = new ArrayList<>(); for (DefaultTag defaultTag : defaultTagList) { List menuList = onBoardService.findStoreByRandomTag(userId, defaultTag); - getTagRecommendsList.add(GetTagRecommends.toDto(menuList,defaultTag)); + getTagRecommendsList.add(GetTagRecommends.toDto(menuList, defaultTag)); } return ApiUtils.success(getTagRecommendsList); + } + @GetMapping("/state") + public String getOnboardingState(@UserId Long userId) { + onBoardService.findOneById(userId); + return "success"; } } diff --git a/src/main/java/com/ourMenu/backend/domain/onboarding/application/OnBoardingService.java b/src/main/java/com/ourMenu/backend/domain/onboarding/application/OnBoardingService.java index 2937d55..f733ab7 100644 --- a/src/main/java/com/ourMenu/backend/domain/onboarding/application/OnBoardingService.java +++ b/src/main/java/com/ourMenu/backend/domain/onboarding/application/OnBoardingService.java @@ -3,12 +3,12 @@ import com.ourMenu.backend.domain.menu.application.MenuService; import com.ourMenu.backend.domain.menu.dao.MenuRepository; import com.ourMenu.backend.domain.menu.domain.Menu; -import com.ourMenu.backend.domain.menu.dto.response.MenuDto; +import com.ourMenu.backend.domain.onboarding.dao.OnBoardingStateRepository; import com.ourMenu.backend.domain.onboarding.domain.AnswerType; import com.ourMenu.backend.domain.onboarding.domain.DefaultTag; +import com.ourMenu.backend.domain.onboarding.domain.OnBoardingState; import com.ourMenu.backend.domain.onboarding.domain.Question; -import com.ourMenu.backend.domain.onboarding.util.S3Util; -import com.ourMenu.backend.domain.store.domain.Store; +import com.ourMenu.backend.domain.onboarding.exception.SearchResultNotFoundException; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -21,11 +21,19 @@ public class OnBoardingService { private final MenuRepository menuRepository; private final MenuService menuService; + private final OnBoardingStateRepository onBoardingStateRepository; public List getAllQuestion() { return Question.getAllQuestions(); } + @Transactional + public List saveAndFindStoreByQuestionAnswer(Long userId, int questionId, AnswerType answerType) { + OnBoardingState onBoardingState = OnBoardingState.toEntity(userId, questionId, answerType); + OnBoardingState saveOnBoardingState = this.save(onBoardingState); + return findStoreByQuestionAnswer(userId, questionId, answerType); + } + @Transactional public List findStoreByQuestionAnswer(Long userId, int questionId, AnswerType answerType) { List foodStringList = Question.getAnswerFoodByIdAndAnswerType(questionId, answerType); @@ -33,7 +41,7 @@ public List findStoreByQuestionAnswer(Long userId, int questionId, AnswerT for (String s : foodStringList) { List menus = menuRepository.findMenusByTitleContainingAndUserId(s, userId); for (Menu menu : menus) { - map.put(menu.getId(),menu); + map.put(menu.getId(), menu); } } @@ -44,4 +52,24 @@ public List findStoreByQuestionAnswer(Long userId, int questionId, AnswerT public List findStoreByRandomTag(Long userId, DefaultTag randomTag) { return menuService.getAllMenusByTagName(randomTag.getTagName(), userId); } + + @Transactional(readOnly = true) + public String findOnboardingStateByUserId(Long userId) { + OnBoardingState onBoardingState = findOneById(userId); + + return "success"; + } + + @Transactional + public OnBoardingState findOneById(Long userId) { + Optional onBoardingStateOptional = onBoardingStateRepository.findByOnBoardingState(userId); + if (onBoardingStateOptional.isEmpty()) + throw new SearchResultNotFoundException(); + return onBoardingStateOptional.get(); + } + + @Transactional + public OnBoardingState save(OnBoardingState onBoardingState) { + return onBoardingStateRepository.save(onBoardingState); + } } From ed80c00e6e804d0a6988f49115aad20a85fe4ee4 Mon Sep 17 00:00:00 2001 From: david-parkk Date: Fri, 16 Aug 2024 15:29:18 +0900 Subject: [PATCH 5/8] fix: init.sql --- mysql/init/init.sql | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/mysql/init/init.sql b/mysql/init/init.sql index d255479..e14951c 100644 --- a/mysql/init/init.sql +++ b/mysql/init/init.sql @@ -75,6 +75,14 @@ create table menu_tag ( primary key (menu_tag_id) ) engine=InnoDB; +create table on_boarding_state ( + answer_type tinyint check (answer_type between 0 and 1), + question_id integer not null, + id bigint not null auto_increment, + user_id bigint, + primary key (id) +) engine=InnoDB; + create table place ( latitude float(53), longitude float(53), From 379a60f754ac2b7e2130247474cccf81b6176a85 Mon Sep 17 00:00:00 2001 From: david-parkk Date: Fri, 16 Aug 2024 15:29:54 +0900 Subject: [PATCH 6/8] =?UTF-8?q?fix:=20=EB=B2=84=EA=B7=B8=20=EC=88=98?= =?UTF-8?q?=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/onboarding/application/OnBoardingService.java | 2 +- .../domain/onboarding/dao/OnBoardingStateRepository.java | 2 +- .../backend/domain/onboarding/domain/OnBoardingState.java | 3 ++- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/ourMenu/backend/domain/onboarding/application/OnBoardingService.java b/src/main/java/com/ourMenu/backend/domain/onboarding/application/OnBoardingService.java index f733ab7..340eacb 100644 --- a/src/main/java/com/ourMenu/backend/domain/onboarding/application/OnBoardingService.java +++ b/src/main/java/com/ourMenu/backend/domain/onboarding/application/OnBoardingService.java @@ -62,7 +62,7 @@ public String findOnboardingStateByUserId(Long userId) { @Transactional public OnBoardingState findOneById(Long userId) { - Optional onBoardingStateOptional = onBoardingStateRepository.findByOnBoardingState(userId); + Optional onBoardingStateOptional = onBoardingStateRepository.findByUserId(userId); if (onBoardingStateOptional.isEmpty()) throw new SearchResultNotFoundException(); return onBoardingStateOptional.get(); diff --git a/src/main/java/com/ourMenu/backend/domain/onboarding/dao/OnBoardingStateRepository.java b/src/main/java/com/ourMenu/backend/domain/onboarding/dao/OnBoardingStateRepository.java index b4ec36f..af93616 100644 --- a/src/main/java/com/ourMenu/backend/domain/onboarding/dao/OnBoardingStateRepository.java +++ b/src/main/java/com/ourMenu/backend/domain/onboarding/dao/OnBoardingStateRepository.java @@ -10,5 +10,5 @@ @Repository public interface OnBoardingStateRepository extends JpaRepository { - Optional findByOnBoardingState(Long userId); + Optional findByUserId(Long userId); } diff --git a/src/main/java/com/ourMenu/backend/domain/onboarding/domain/OnBoardingState.java b/src/main/java/com/ourMenu/backend/domain/onboarding/domain/OnBoardingState.java index c157cb7..290ed2e 100644 --- a/src/main/java/com/ourMenu/backend/domain/onboarding/domain/OnBoardingState.java +++ b/src/main/java/com/ourMenu/backend/domain/onboarding/domain/OnBoardingState.java @@ -4,13 +4,14 @@ import jakarta.persistence.GeneratedValue; import jakarta.persistence.GenerationType; import jakarta.persistence.Id; +import lombok.AccessLevel; import lombok.AllArgsConstructor; import lombok.Builder; import lombok.NoArgsConstructor; @Entity @Builder -@NoArgsConstructor +@NoArgsConstructor(access = AccessLevel.PROTECTED) @AllArgsConstructor public class OnBoardingState { From a703dd45dbf18b6edf3d3cf6da03f2d3edbfe3b5 Mon Sep 17 00:00:00 2001 From: david-parkk Date: Fri, 16 Aug 2024 15:35:57 +0900 Subject: [PATCH 7/8] feat: GET/onboarding/state response --- .../onboarding/api/OnBoardingController.java | 8 ++++--- .../api/response/GetOnboardingState.java | 24 +++++++++++++++++++ .../onboarding/domain/OnBoardingState.java | 6 ++--- 3 files changed, 31 insertions(+), 7 deletions(-) create mode 100644 src/main/java/com/ourMenu/backend/domain/onboarding/api/response/GetOnboardingState.java diff --git a/src/main/java/com/ourMenu/backend/domain/onboarding/api/OnBoardingController.java b/src/main/java/com/ourMenu/backend/domain/onboarding/api/OnBoardingController.java index 31f4e69..ec6779e 100644 --- a/src/main/java/com/ourMenu/backend/domain/onboarding/api/OnBoardingController.java +++ b/src/main/java/com/ourMenu/backend/domain/onboarding/api/OnBoardingController.java @@ -2,11 +2,13 @@ import com.ourMenu.backend.domain.menu.domain.Menu; import com.ourMenu.backend.domain.onboarding.api.response.GetOnboardingResponse; +import com.ourMenu.backend.domain.onboarding.api.response.GetOnboardingState; import com.ourMenu.backend.domain.onboarding.api.response.GetQuestionRecommands; import com.ourMenu.backend.domain.onboarding.api.response.GetTagRecommends; import com.ourMenu.backend.domain.onboarding.application.OnBoardingService; import com.ourMenu.backend.domain.onboarding.domain.AnswerType; import com.ourMenu.backend.domain.onboarding.domain.DefaultTag; +import com.ourMenu.backend.domain.onboarding.domain.OnBoardingState; import com.ourMenu.backend.domain.onboarding.domain.Question; import com.ourMenu.backend.global.argument_resolver.UserId; import com.ourMenu.backend.global.common.ApiResponse; @@ -57,8 +59,8 @@ public ApiResponse> getQuestionRecommend(@UserId Long use } @GetMapping("/state") - public String getOnboardingState(@UserId Long userId) { - onBoardService.findOneById(userId); - return "success"; + public ApiResponse getOnboardingState(@UserId Long userId) { + OnBoardingState onBoardingState = onBoardService.findOneById(userId); + return ApiUtils.success(GetOnboardingState.toDto(onBoardingState)); } } diff --git a/src/main/java/com/ourMenu/backend/domain/onboarding/api/response/GetOnboardingState.java b/src/main/java/com/ourMenu/backend/domain/onboarding/api/response/GetOnboardingState.java new file mode 100644 index 0000000..a4f5511 --- /dev/null +++ b/src/main/java/com/ourMenu/backend/domain/onboarding/api/response/GetOnboardingState.java @@ -0,0 +1,24 @@ +package com.ourMenu.backend.domain.onboarding.api.response; + +import com.ourMenu.backend.domain.onboarding.domain.AnswerType; +import com.ourMenu.backend.domain.onboarding.domain.OnBoardingState; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Getter; + +@AllArgsConstructor +@Builder +@Getter +public class GetOnboardingState { + + private int questionId; + + private AnswerType answerType; + + public static GetOnboardingState toDto(OnBoardingState onBoardingState) { + return GetOnboardingState.builder() + .questionId(onBoardingState.getQuestionId()) + .answerType(onBoardingState.getAnswerType()) + .build(); + } +} diff --git a/src/main/java/com/ourMenu/backend/domain/onboarding/domain/OnBoardingState.java b/src/main/java/com/ourMenu/backend/domain/onboarding/domain/OnBoardingState.java index 290ed2e..8b65cad 100644 --- a/src/main/java/com/ourMenu/backend/domain/onboarding/domain/OnBoardingState.java +++ b/src/main/java/com/ourMenu/backend/domain/onboarding/domain/OnBoardingState.java @@ -4,13 +4,11 @@ import jakarta.persistence.GeneratedValue; import jakarta.persistence.GenerationType; import jakarta.persistence.Id; -import lombok.AccessLevel; -import lombok.AllArgsConstructor; -import lombok.Builder; -import lombok.NoArgsConstructor; +import lombok.*; @Entity @Builder +@Getter @NoArgsConstructor(access = AccessLevel.PROTECTED) @AllArgsConstructor public class OnBoardingState { From d4dc193a3d8e014616d2233bd755193cc50faad7 Mon Sep 17 00:00:00 2001 From: david-parkk Date: Fri, 16 Aug 2024 15:46:06 +0900 Subject: [PATCH 8/8] fix: onBoardingState update --- .../onboarding/application/OnBoardingService.java | 15 +++++++++++++-- .../domain/onboarding/domain/OnBoardingState.java | 5 +++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/ourMenu/backend/domain/onboarding/application/OnBoardingService.java b/src/main/java/com/ourMenu/backend/domain/onboarding/application/OnBoardingService.java index 340eacb..1c173bb 100644 --- a/src/main/java/com/ourMenu/backend/domain/onboarding/application/OnBoardingService.java +++ b/src/main/java/com/ourMenu/backend/domain/onboarding/application/OnBoardingService.java @@ -29,11 +29,22 @@ public List getAllQuestion() { @Transactional public List saveAndFindStoreByQuestionAnswer(Long userId, int questionId, AnswerType answerType) { - OnBoardingState onBoardingState = OnBoardingState.toEntity(userId, questionId, answerType); - OnBoardingState saveOnBoardingState = this.save(onBoardingState); + OnBoardingState onBoardingState = saveAndUpdateOnBoardingState(userId, questionId, answerType); return findStoreByQuestionAnswer(userId, questionId, answerType); } + @Transactional + public OnBoardingState saveAndUpdateOnBoardingState(Long userId, int questionId, AnswerType answerType){ + Optional onBoardingStateOptional = onBoardingStateRepository.findByUserId(userId); + if(onBoardingStateOptional.isEmpty()){ + OnBoardingState onBoardingState = OnBoardingState.toEntity(userId, questionId, answerType); + return save(onBoardingState); + } + OnBoardingState onBoardingState = onBoardingStateOptional.get(); + onBoardingState.update(questionId,answerType); + return onBoardingState; + } + @Transactional public List findStoreByQuestionAnswer(Long userId, int questionId, AnswerType answerType) { List foodStringList = Question.getAnswerFoodByIdAndAnswerType(questionId, answerType); diff --git a/src/main/java/com/ourMenu/backend/domain/onboarding/domain/OnBoardingState.java b/src/main/java/com/ourMenu/backend/domain/onboarding/domain/OnBoardingState.java index 8b65cad..daf8385 100644 --- a/src/main/java/com/ourMenu/backend/domain/onboarding/domain/OnBoardingState.java +++ b/src/main/java/com/ourMenu/backend/domain/onboarding/domain/OnBoardingState.java @@ -28,4 +28,9 @@ public static OnBoardingState toEntity(Long userId, int questionId, AnswerType a .answerType(answerType) .build(); } + + public void update(int questionId, AnswerType answerType) { + this.questionId = questionId; + this.answerType = answerType; + } }