diff --git a/src/main/java/com/modernfarmer/farmusspring/domain/user/controller/OnBoardingController.java b/src/main/java/com/modernfarmer/farmusspring/domain/user/controller/OnBoardingController.java index 222218e7..067a1dfd 100644 --- a/src/main/java/com/modernfarmer/farmusspring/domain/user/controller/OnBoardingController.java +++ b/src/main/java/com/modernfarmer/farmusspring/domain/user/controller/OnBoardingController.java @@ -4,10 +4,12 @@ import com.modernfarmer.farmusspring.domain.auth.entity.CustomUser; import com.modernfarmer.farmusspring.domain.user.dto.request.SetLevelRequest; import com.modernfarmer.farmusspring.domain.user.dto.request.SetMotivationRequest; +import com.modernfarmer.farmusspring.domain.user.dto.response.EncouragementMessageDto; import com.modernfarmer.farmusspring.domain.user.dto.response.SetLevelResponse; import com.modernfarmer.farmusspring.domain.user.entity.User; import com.modernfarmer.farmusspring.domain.user.service.OnBoardingService; import com.modernfarmer.farmusspring.global.response.BaseResponseDto; +import com.modernfarmer.farmusspring.global.response.SuccessCode; import jakarta.servlet.http.HttpServletRequest; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; @@ -18,14 +20,14 @@ @Slf4j @RequiredArgsConstructor @RestController -@RequestMapping("/api/user") +@RequestMapping("/api/user/on-boarding") public class OnBoardingController { private final OnBoardingService onBoardingService; - @PostMapping(value = "/on-boarding/motivation") + @PostMapping(value = "/motivation") public BaseResponseDto settingMotivation(@AuthenticationPrincipal CustomUser user, @Validated @RequestBody SetMotivationRequest setMotivationRequest ) { @@ -34,7 +36,7 @@ public BaseResponseDto settingMotivation(@AuthenticationPrincipal CustomUs setMotivationRequest); } - @PostMapping(value = "/on-boarding/level") + @PostMapping(value = "/level") public BaseResponseDto settingLevel(@AuthenticationPrincipal CustomUser user, @Validated @RequestBody SetLevelRequest setLevelRequest ) { @@ -43,9 +45,15 @@ public BaseResponseDto settingLevel(@AuthenticationPrincipal C } - @PatchMapping (value = "/on-boarding/complete") + @PatchMapping (value = "/complete") public BaseResponseDto completeOnBoarding(@AuthenticationPrincipal CustomUser user) { return onBoardingService.completeOnBoarding(user.getUserId()); } + + @GetMapping("/encouragement-message") + public BaseResponseDto bringEncouragementMessage(@AuthenticationPrincipal CustomUser user) { + EncouragementMessageDto result = onBoardingService.bringEncouragementMessage(user.getUserId()); + return BaseResponseDto.of(SuccessCode.SUCCESS, result); + } } diff --git a/src/main/java/com/modernfarmer/farmusspring/domain/user/controller/UserController.java b/src/main/java/com/modernfarmer/farmusspring/domain/user/controller/UserController.java index c797ff2e..f83cde17 100644 --- a/src/main/java/com/modernfarmer/farmusspring/domain/user/controller/UserController.java +++ b/src/main/java/com/modernfarmer/farmusspring/domain/user/controller/UserController.java @@ -55,8 +55,6 @@ public BaseResponseDto bringNotification(@AuthenticationPrincipal CustomUser return BaseResponseDto.of(SuccessCode.SUCCESS, result); } - - @PostMapping(value = "/profile", consumes = MediaType.MULTIPART_FORM_DATA_VALUE) public BaseResponseDto settingProfile( @AuthenticationPrincipal CustomUser user, @@ -67,13 +65,8 @@ public BaseResponseDto settingProfile( } @PostMapping("/init/{userId}") - public void initUser( - @PathVariable Long userId - ) { + public void initUser(@PathVariable Long userId) { userService.initUser(userId); } - - - } diff --git a/src/main/java/com/modernfarmer/farmusspring/domain/user/dto/response/EncouragementMessageDto.java b/src/main/java/com/modernfarmer/farmusspring/domain/user/dto/response/EncouragementMessageDto.java new file mode 100644 index 00000000..eb242162 --- /dev/null +++ b/src/main/java/com/modernfarmer/farmusspring/domain/user/dto/response/EncouragementMessageDto.java @@ -0,0 +1,14 @@ +package com.modernfarmer.farmusspring.domain.user.dto.response; + + +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; + +@NoArgsConstructor +@AllArgsConstructor(staticName = "of") +@Getter +public class EncouragementMessageDto { + + String encouragementMessage; +} diff --git a/src/main/java/com/modernfarmer/farmusspring/domain/user/repository/UserMotivationRepository.java b/src/main/java/com/modernfarmer/farmusspring/domain/user/repository/UserMotivationRepository.java index b33467fa..822fc5bc 100644 --- a/src/main/java/com/modernfarmer/farmusspring/domain/user/repository/UserMotivationRepository.java +++ b/src/main/java/com/modernfarmer/farmusspring/domain/user/repository/UserMotivationRepository.java @@ -7,10 +7,14 @@ import org.springframework.data.jpa.repository.Modifying; import org.springframework.data.jpa.repository.Query; +import java.util.List; +import java.util.Optional; + public interface UserMotivationRepository extends JpaRepository { - @Modifying - @Query(value = "INSERT INTO user_motivation(motivation, user_id) VALUES (:motivation, :userId)", nativeQuery = true) - void insertMotivation(@Param("motivation") String motivation, @Param("userId") Long userId); + @Query("select um from user_motivation as um where um.user.id = :userId") + List findUserMotivationByUserId(@Param("userId") Long userId); + + } diff --git a/src/main/java/com/modernfarmer/farmusspring/domain/user/repository/UserRepository.java b/src/main/java/com/modernfarmer/farmusspring/domain/user/repository/UserRepository.java index 816b1991..259eb563 100644 --- a/src/main/java/com/modernfarmer/farmusspring/domain/user/repository/UserRepository.java +++ b/src/main/java/com/modernfarmer/farmusspring/domain/user/repository/UserRepository.java @@ -15,34 +15,27 @@ public interface UserRepository extends JpaRepository { Optional findByUserNumber(String usernumber); - @Query("SELECT a FROM user AS a WHERE a.id = :userId") User findUserData(@Param("userId") Long userId); - User findUserById(Long userId); - @Modifying @Query("update user as u set u.level= :level where u.id = :userId") void insertUserLevel(@Param("userId") Long userId, @Param("level") String level); - - @Modifying @Query("update user as u set u.early = false where u.id = :userId") void updateEarly( @Param("userId") Long userId); - @Modifying @Query("update user as u set u.profileImage = null where u.id = :userId") void updateProfileImage(@Param("userId") Long userId); - @Modifying @Query("update user as u set u.notificationStatus = :status where u.id = :userId") void updateNotification(@Param("userId") Long userId, @Param("status") Boolean status); @Modifying @Query("update user as u set u.profileImage = :profileImage, u.nickname = :nickName where u.id = :userId") void selectProfileAndNickname(@Param("userId") Long userId, @Param("profileImage") String profileImage, @Param("nickName") String nickName); - - @Modifying @Query("update user as u set u.nickname = :nickName where u.id = :userId") void updateNickname(@Param("userId") Long userId, @Param("nickName") String nickName); + + } diff --git a/src/main/java/com/modernfarmer/farmusspring/domain/user/service/OnBoardingService.java b/src/main/java/com/modernfarmer/farmusspring/domain/user/service/OnBoardingService.java index a920f567..4547839c 100644 --- a/src/main/java/com/modernfarmer/farmusspring/domain/user/service/OnBoardingService.java +++ b/src/main/java/com/modernfarmer/farmusspring/domain/user/service/OnBoardingService.java @@ -3,25 +3,33 @@ import com.modernfarmer.farmusspring.domain.user.dto.request.SetLevelRequest; import com.modernfarmer.farmusspring.domain.user.dto.request.SetMotivationRequest; +import com.modernfarmer.farmusspring.domain.user.dto.response.EncouragementMessageDto; import com.modernfarmer.farmusspring.domain.user.dto.response.SetLevelResponse; import com.modernfarmer.farmusspring.domain.user.entity.User; import com.modernfarmer.farmusspring.domain.user.entity.UserMotivation; import com.modernfarmer.farmusspring.domain.user.exception.UserNotFoundException; import com.modernfarmer.farmusspring.domain.user.repository.UserMotivationRepository; import com.modernfarmer.farmusspring.domain.user.repository.UserRepository; +import com.modernfarmer.farmusspring.domain.user.util.EncouragementMessages; +import com.modernfarmer.farmusspring.domain.user.util.RamdomFunction; import com.modernfarmer.farmusspring.global.response.BaseResponseDto; import com.modernfarmer.farmusspring.global.response.SuccessCode; import lombok.AllArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.EnableTransactionManagement; import org.springframework.transaction.annotation.Transactional; +import java.util.List; +import java.util.Optional; + @Slf4j @AllArgsConstructor @Service public class OnBoardingService { private final UserRepository userRepository; + private final UserMotivationRepository userMotivationRepository; @Transactional public BaseResponseDto settingMotivation(Long userId, SetMotivationRequest setMotivationRequest) { @@ -32,42 +40,37 @@ public BaseResponseDto settingMotivation(Long userId, SetMotivationRequest setMo } @Transactional public BaseResponseDto settingLevel(Long userId, SetLevelRequest setLevelRequest){ - String level = measureLevel(setLevelRequest.getTime(), setLevelRequest.getSkill()); insertLevel(userId, level); return BaseResponseDto.of(SuccessCode.SUCCESS, SetLevelResponse.of(level)); - } @Transactional public BaseResponseDto completeOnBoarding(Long userId) { updateCompleteBoarding(userId); return BaseResponseDto.of(SuccessCode.SUCCESS, null); } - - + @Transactional + public EncouragementMessageDto bringEncouragementMessage(Long userId) { + List userMotivationList = userMotivationRepository.findUserMotivationByUserId(userId); + int ramdomNumber = RamdomFunction.getRandomIntInRange(0, userMotivationList.size()-1); + String encouragementMessage = EncouragementMessages.getRandomMessageByKey(EncouragementMessages.valueOf(userMotivationList.get(ramdomNumber).getMotivation())); + return EncouragementMessageDto.of(encouragementMessage); + } private void updateCompleteBoarding(Long userId){ userRepository.updateEarly(userId); } - - - public void insertMotivation(User user, SetMotivationRequest setMotivationRequest) { for (String motivation : setMotivationRequest.getMotivation()) { insertOneMotivation(user, motivation); } } - public void insertOneMotivation(User user, String motivation) { UserMotivation newMotivation = UserMotivation.createUserMotivation(motivation, user); user.addUserMotivation(newMotivation); - } - private void insertLevel(Long userId, String level){ userRepository.insertUserLevel(userId, level); } - - private String measureLevel(int time,String skill) { boolean isIntermediate = false; boolean isMaster = false; @@ -103,7 +106,6 @@ private String measureLevel(int time,String skill) { } return "알 수 없음"; } - public User findUser(Long userId) { return userRepository.findById(userId).orElseThrow(() -> new UserNotFoundException("해당 유저가 없습니다.")); } diff --git a/src/main/java/com/modernfarmer/farmusspring/domain/user/service/UserService.java b/src/main/java/com/modernfarmer/farmusspring/domain/user/service/UserService.java index f6fc95c9..389b7ffb 100644 --- a/src/main/java/com/modernfarmer/farmusspring/domain/user/service/UserService.java +++ b/src/main/java/com/modernfarmer/farmusspring/domain/user/service/UserService.java @@ -23,6 +23,7 @@ import java.time.LocalDateTime; import java.time.temporal.ChronoUnit; import java.util.Optional; +import java.util.Random; @Slf4j @AllArgsConstructor @@ -63,15 +64,12 @@ public BaseResponseDto settingProfile( return BaseResponseDto.of(SuccessCode.SUCCESS,null); } - - public User selectUserById(Long userId){ User user = userRepository.findUserData(userId); checkUserData(user); return user; } - public void checkUserData(User user){ if(user == null) { throw new UserNotFoundException("유저를 찾을 수 없습니다."); diff --git a/src/main/java/com/modernfarmer/farmusspring/domain/user/util/EncouragementMessages.java b/src/main/java/com/modernfarmer/farmusspring/domain/user/util/EncouragementMessages.java new file mode 100644 index 00000000..ea18573e --- /dev/null +++ b/src/main/java/com/modernfarmer/farmusspring/domain/user/util/EncouragementMessages.java @@ -0,0 +1,40 @@ +package com.modernfarmer.farmusspring.domain.user.util; + + +public enum EncouragementMessages { + 채소값절약(new String[]{ + "채소를 직접 길러_식비를 줄여보세요!", + "직접 기른 채소로_알뜰하고 현명한 식사를 해봐요!", + "오늘도 채솟값_알뜰하게 아끼는 중!", + "홈파밍으로 지출은 줄이고_성취감은 더해봐요", + "직접 기른 채소로_매일 알뜰하게, 매일 특별하게!" + }), + 신선하고안전한식재료(new String[]{ + "파머님, 직접 기른 채소로_건강한 삶을 챙겨봐요!", + "파머님의 사랑으로_오늘도 쑥쑥 자라는 중", + "직접 기른 채소를_먹을 수 있는 그날까지!", + "신선함으로 가득 채워지는 식탁, _ 늘 함께할게요!", + "눈으로 보는 재미, 입으로 맛보는_홈파밍의 즐거움!" + }), + 스트레스해소와정서적안정(new String[]{ + "바쁜 일상에서 잠깐 벗어나_텃밭을 가꿔봐요", + "홈파밍을 하며_마음의 안정을 느껴봐요!", + "스트레스 받는 하루, _ 홈파밍으로 힐링하세요", + "나만의 채소를 키우며_소소한 성취감을 느껴보세요", + "성취감은 꾸준함에서! _ 오늘도 함께 성장해요" + }); + private final String[] messages; + + EncouragementMessages(String[] messages) { + this.messages = messages; + } + + public String getRandomMessage() { + int index = (int) (Math.random() * messages.length); + return messages[index]; + } + + public static String getRandomMessageByKey(EncouragementMessages key) { + return key.getRandomMessage(); + } +} diff --git a/src/main/java/com/modernfarmer/farmusspring/domain/user/util/RamdomFunction.java b/src/main/java/com/modernfarmer/farmusspring/domain/user/util/RamdomFunction.java new file mode 100644 index 00000000..02a8fc6c --- /dev/null +++ b/src/main/java/com/modernfarmer/farmusspring/domain/user/util/RamdomFunction.java @@ -0,0 +1,12 @@ +package com.modernfarmer.farmusspring.domain.user.util; + +import java.util.Random; + +public class RamdomFunction { + + private static final Random RANDOM = new Random(); + + public static int getRandomIntInRange(int start, int end) { + return RANDOM.nextInt(end - start + 1) + start; + } +}