diff --git a/src/main/java/modernfarmer/server/farmususer/global/exception/fail/ErrorMessage.java b/src/main/java/modernfarmer/server/farmususer/global/exception/fail/ErrorMessage.java index d219dc5..eb07736 100644 --- a/src/main/java/modernfarmer/server/farmususer/global/exception/fail/ErrorMessage.java +++ b/src/main/java/modernfarmer/server/farmususer/global/exception/fail/ErrorMessage.java @@ -9,8 +9,9 @@ public enum ErrorMessage { INTERVAL_SERVER_ERROR(1001, "요청을 처리하는 과정에서 서버가 예상하지 못한 오류가 발생하였습니다."), - REFRESH_NOTIFICATION_ERROR(4017, "Refresh Token 인증 오류"), - NO_USER_DATA(1002, "유저에 대한 정보가 없습니다."); + REFRESH_NOTIFICATION_ERROR(1002, "Refresh Token 인증 오류"), + NO_USER_DATA(1003, "유저에 대한 정보가 없습니다."), + NO_MOTIVATION_DATA(1004, "동기에 대한 정보가 없습니다."); diff --git a/src/main/java/modernfarmer/server/farmususer/user/entity/Motivation.java b/src/main/java/modernfarmer/server/farmususer/user/entity/Motivation.java new file mode 100644 index 0000000..4ae5584 --- /dev/null +++ b/src/main/java/modernfarmer/server/farmususer/user/entity/Motivation.java @@ -0,0 +1,33 @@ +package modernfarmer.server.farmususer.user.entity; + +import lombok.*; +import org.hibernate.annotations.OnDelete; +import org.hibernate.annotations.OnDeleteAction; + +import javax.persistence.*; +import javax.validation.constraints.NotNull; +import java.util.LinkedHashSet; +import java.util.Set; + +@Builder +@AllArgsConstructor +@NoArgsConstructor +@Getter +@Entity +@Table(name = "motivation") +public class Motivation extends BaseEntity{ + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + @Column(name = "motivation_id", nullable = false) + private Long id; + + + @Column(name = "motivation_reason", nullable = false, length = 40) + private String motivationReason; + + @OneToMany(mappedBy = "motivation", fetch = FetchType.LAZY) + private Set userMotivations = new LinkedHashSet<>(); + + + +} \ No newline at end of file diff --git a/src/main/java/modernfarmer/server/farmususer/user/entity/UserMotivation.java b/src/main/java/modernfarmer/server/farmususer/user/entity/UserMotivation.java index 4dbbceb..c0d6d0f 100644 --- a/src/main/java/modernfarmer/server/farmususer/user/entity/UserMotivation.java +++ b/src/main/java/modernfarmer/server/farmususer/user/entity/UserMotivation.java @@ -6,8 +6,6 @@ import javax.persistence.*; import javax.validation.constraints.NotNull; -import javax.validation.constraints.Size; -import java.time.Instant; @Builder @AllArgsConstructor @@ -28,10 +26,18 @@ public class UserMotivation extends BaseEntity{ @JoinColumn(name = "user_id", nullable = false) private User user; - @Size(max = 40) @NotNull - @Column(name = "user_motivation", nullable = false, length = 40) - private String userMotivation; + @ManyToOne(fetch = FetchType.LAZY, optional = false) + @OnDelete(action = OnDeleteAction.CASCADE) + @JoinColumn(name = "motivation_id", nullable = false) + private Motivation motivation; + + + + + + + diff --git a/src/main/java/modernfarmer/server/farmususer/user/repository/MotivationRepository.java b/src/main/java/modernfarmer/server/farmususer/user/repository/MotivationRepository.java new file mode 100644 index 0000000..fcb6f50 --- /dev/null +++ b/src/main/java/modernfarmer/server/farmususer/user/repository/MotivationRepository.java @@ -0,0 +1,23 @@ +package modernfarmer.server.farmususer.user.repository; + + +import modernfarmer.server.farmususer.user.entity.Motivation; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.query.Param; +import org.springframework.stereotype.Repository; + +import java.util.Optional; + +@Repository +public interface MotivationRepository extends JpaRepository { + + @Query("select m.id from Motivation as m where m.motivationReason = :motivationReason") + Long getMotivationId(@Param("motivationReason") String motivationReason); + + + Optional findByMotivationReason(String motivationReason); + + + +} diff --git a/src/main/java/modernfarmer/server/farmususer/user/service/AuthService.java b/src/main/java/modernfarmer/server/farmususer/user/service/AuthService.java index 0c2fb09..2b9e006 100644 --- a/src/main/java/modernfarmer/server/farmususer/user/service/AuthService.java +++ b/src/main/java/modernfarmer/server/farmususer/user/service/AuthService.java @@ -40,20 +40,15 @@ public BaseResponseDto googleLogin(String accessToken) { User user; boolean early; - Mono userInfoMono = getUserGoogleInfo(accessToken); + Mono userInfoMono = getUserInfo(accessToken, "https://www.googleapis.com/oauth2/v2/userinfo", GoogleUserResponseDto.class); GoogleUserResponseDto userInfo = userInfoMono.block(); Optional userData = userRepository.findByUserNumber(String.valueOf(userInfo.getId())); - log.info(String.valueOf(userInfo.getEmail())); - log.info(String.valueOf(userInfo.getPicture())); - log.info(String.valueOf(userInfo.getId())); - if(userData.isEmpty()){ user = User.builder() .userNumber(String.valueOf(userInfo.getId())) .roles("USER") - .profileImage(userInfo.getPicture()) .early(true) .build(); @@ -86,15 +81,9 @@ public BaseResponseDto kakaoLogin(String accessToken) { User user; boolean early; - Mono userInfoMono = getUserKakaoInfo(accessToken); + Mono userInfoMono = getUserInfo(accessToken, "https://kapi.kakao.com/v2/user/me", KakaoUserResponseDto.class); KakaoUserResponseDto userInfo = userInfoMono.block(); - - log.info(String.valueOf(userInfo.getKakao_account().getEmail())); - log.info(String.valueOf(userInfo.getKakao_account().getProfile().getProfile_image_url())); - log.info(String.valueOf(userInfo.getKakao_account().getProfile().getNickname())); - - Optional userData = userRepository.findByUserNumber(String.valueOf(userInfo.getId())); @@ -102,7 +91,6 @@ public BaseResponseDto kakaoLogin(String accessToken) { user = User.builder() .userNumber(String.valueOf(userInfo.getId())) .roles("USER") - .profileImage(userInfo.getKakao_account().getProfile().getProfile_image_url()) .early(true) .build(); @@ -131,21 +119,13 @@ public BaseResponseDto kakaoLogin(String accessToken) { return baseResponseDto; } - public Mono getUserKakaoInfo(String accessToken) { + public Mono getUserInfo(String accessToken, String apiUrl, Class responseType) { return webClient .get() - .uri("https://kapi.kakao.com/v2/user/me") // 카카오 사용자 정보 엔드포인트 + .uri(apiUrl) .headers(headers -> headers.setBearerAuth(accessToken)) .retrieve() - .bodyToMono(KakaoUserResponseDto.class); + .bodyToMono(responseType); } - public Mono getUserGoogleInfo(String accessToken) { - return webClient - .get() - .uri("https://www.googleapis.com/oauth2/v2/userinfo") // 카카오 사용자 정보 엔드포인트 - .headers(headers -> headers.setBearerAuth(accessToken)) - .retrieve() - .bodyToMono(GoogleUserResponseDto.class); - } } \ No newline at end of file diff --git a/src/main/java/modernfarmer/server/farmususer/user/service/OnBoardingService.java b/src/main/java/modernfarmer/server/farmususer/user/service/OnBoardingService.java index 6b2f61b..92433e6 100644 --- a/src/main/java/modernfarmer/server/farmususer/user/service/OnBoardingService.java +++ b/src/main/java/modernfarmer/server/farmususer/user/service/OnBoardingService.java @@ -9,16 +9,17 @@ import modernfarmer.server.farmususer.user.dto.request.OnBoardingMotivationRequest; import modernfarmer.server.farmususer.user.dto.response.BaseResponseDto; import modernfarmer.server.farmususer.user.dto.response.OnBoardingLevelResponse; +import modernfarmer.server.farmususer.user.entity.Motivation; import modernfarmer.server.farmususer.user.entity.User; import modernfarmer.server.farmususer.user.entity.UserMotivation; +import modernfarmer.server.farmususer.user.repository.MotivationRepository; import modernfarmer.server.farmususer.user.repository.UserMotivationRepository; import modernfarmer.server.farmususer.user.repository.UserRepository; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import javax.transaction.Transactional; -import java.util.ArrayList; +import java.util.Optional; @RequiredArgsConstructor @@ -31,16 +32,24 @@ public class OnBoardingService { private final UserMotivationRepository userMotivationRepository; + private final MotivationRepository motivationRepository; + public BaseResponseDto onBoardingMotivation(Long userId, OnBoardingMotivationRequest onBoardingMotivationRequest){ User user = User.builder().id(userId).build(); for(String motivations : onBoardingMotivationRequest.getMotivation()){ + Optional motivation = motivationRepository.findByMotivationReason(motivations); + + if(motivation.isEmpty()){ + return BaseResponseDto.of(ErrorMessage.NO_MOTIVATION_DATA); + } + UserMotivation userMotivation = UserMotivation .builder() .user(user) - .userMotivation(motivations) + .motivation(motivation.get()) .build(); userMotivationRepository.save(userMotivation); @@ -62,39 +71,38 @@ public BaseResponseDto onBoardingLevel(Long userId, OnBoardingLevelRequest onBoa private String recommendAlgorithms(int time,String skill) { boolean isIntermediate = false; - boolean isAdvanced = false; - boolean isExperienced = false; + boolean isMaster = false; + boolean isElementary = false; boolean isBeginner = false; if ("홈파밍 중급".equals(skill)) { isIntermediate = true; } else if ("홈파밍 고수".equals(skill)) { - isAdvanced = true; + isMaster = true; } else if ("홈파밍 초보".equals(skill)) { - isExperienced = true; + isElementary = true; } else if ("홈파밍 입문".equals(skill)) { isBeginner = true; } - if (time == 2 && (isIntermediate || isAdvanced)) { + if (time == 2 && (isIntermediate || isMaster)) { return "HARD"; - } else if (time == 2 && (isBeginner || isExperienced)) { + } else if (time == 2 && (isBeginner || isElementary)) { return "NORMAL"; - } else if (time == 1 && isAdvanced) { + } else if (time == 1 && isMaster) { return "HARD"; - } else if (time == 1 && (isIntermediate || isExperienced)) { + } else if (time == 1 && (isIntermediate || isElementary)) { return "NORMAL"; } else if (time == 1 && isBeginner) { return "EASY"; - } else if (time == 0 && (isExperienced && isBeginner)) { - return "EASY"; - } else if (time == 0 && isExperienced) { - return "NORMAL"; - - } else if (time == 0 && isAdvanced) { + } else if (time == 0 && isMaster) { return "HARD"; + } else if (time == 0 && isIntermediate) { + return "NORMAL"; + } else if (time == 0 && (isElementary || isBeginner)) { + return "EASY"; } return "알 수 없음";