From e97844d86e7f5546779a3345d4711b0cd39a0fe1 Mon Sep 17 00:00:00 2001 From: MinchoGreenT Date: Thu, 23 Nov 2023 18:27:41 +0900 Subject: [PATCH] =?UTF-8?q?[feat]=20=EB=8F=99=EA=B8=B0=EB=B6=80=EC=97=AC?= =?UTF-8?q?=20=EB=A9=94=EC=84=B8=EC=A7=80=20api?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../farmususer/global/enums/Motivations.java | 54 +++++++++++++++++++ .../user/controller/UserController.java | 6 +++ .../GetUserLevelAndNicknameResponseDto.java | 1 + .../response/UserMotivationResponseDto.java | 13 +++++ .../farmususer/user/service/UserService.java | 37 +++++++++++-- 5 files changed, 108 insertions(+), 3 deletions(-) create mode 100644 src/main/java/modernfarmer/server/farmususer/global/enums/Motivations.java create mode 100644 src/main/java/modernfarmer/server/farmususer/user/dto/response/UserMotivationResponseDto.java diff --git a/src/main/java/modernfarmer/server/farmususer/global/enums/Motivations.java b/src/main/java/modernfarmer/server/farmususer/global/enums/Motivations.java new file mode 100644 index 0000000..18d8883 --- /dev/null +++ b/src/main/java/modernfarmer/server/farmususer/global/enums/Motivations.java @@ -0,0 +1,54 @@ +package modernfarmer.server.farmususer.global.enums; + +public class Motivations { + + public enum Motive_1 { + MOTIVE1_1("채소를 직접 길러\n식비를 줄여보세요!"), + MOTIVE1_2("직접 기른 채소로\n알뜰하고 현명한 식사를 해봐요!"), + MOTIVE1_3("오늘도 채솟값\n알뜰하게 아끼는 중!"), + MOTIVE1_4("홈파밍으로 지출은 줄이고\n성취감은 더해봐요"), + MOTIVE1_5("직접 기른 채소로\n매일 알뜰하게, 매일 특별하게!"); + + private final String motive1; + + Motive_1(String motive1) { + this.motive1 = motive1; + } + + public String getMotive() { + return motive1; + } + } + public enum Motive_2 { + MOTIVE2_1("직접 기른 채소를\n먹을 수 있는 그날까지!"), + MOTIVE2_2("신선함으로 가득 채워지는 식탁, \n 늘 함께할게요!"), + MOTIVE2_3("눈으로 보는 재미, 입으로 맛보는\n홈파밍의 즐거움!"); + + private final String motive2; + + Motive_2(String motive2) { + this.motive2 = motive2; + } + + public String getMotive() { + return motive2; + } + } + public enum Motive_3 { + MOTIVE3_1("바쁜 일상에서 잠깐 벗어나\n텃밭을 가꿔봐요"), + MOTIVE3_2("홈파밍을 하며\n마음의 안정을 느껴봐요!"), + MOTIVE3_3("스트레스 받는 하루, \n 홈파밍으로 힐링하세요"), + MOTIVE3_4("나만의 채소를 키우며\n소소한 성취감을 느껴보세요"), + MOTIVE3_5("성취감은 꾸준함에서! \n 오늘도 함께 성장해요"); + + private final String motive3; + + Motive_3(String motive3) { + this.motive3 = motive3; + } + + public String getMotive() { + return motive3; + } + } +} diff --git a/src/main/java/modernfarmer/server/farmususer/user/controller/UserController.java b/src/main/java/modernfarmer/server/farmususer/user/controller/UserController.java index 99a6a81..0c46347 100644 --- a/src/main/java/modernfarmer/server/farmususer/user/controller/UserController.java +++ b/src/main/java/modernfarmer/server/farmususer/user/controller/UserController.java @@ -135,4 +135,10 @@ public BaseResponseDto specificUser(@RequestParam("user return userService.specificUser(userId); } + @GetMapping("/motivation") + public BaseResponseDto getMotivation( + @RequestHeader("user") Long userId + ){ + return BaseResponseDto.of(SuccessMessage.SUCCESS, userService.getUserMotivation(userId)); + } } diff --git a/src/main/java/modernfarmer/server/farmususer/user/dto/response/GetUserLevelAndNicknameResponseDto.java b/src/main/java/modernfarmer/server/farmususer/user/dto/response/GetUserLevelAndNicknameResponseDto.java index 7c98608..1ec9d00 100644 --- a/src/main/java/modernfarmer/server/farmususer/user/dto/response/GetUserLevelAndNicknameResponseDto.java +++ b/src/main/java/modernfarmer/server/farmususer/user/dto/response/GetUserLevelAndNicknameResponseDto.java @@ -11,4 +11,5 @@ public class GetUserLevelAndNicknameResponseDto { private String level; private String nickname; + private String motivation; } diff --git a/src/main/java/modernfarmer/server/farmususer/user/dto/response/UserMotivationResponseDto.java b/src/main/java/modernfarmer/server/farmususer/user/dto/response/UserMotivationResponseDto.java new file mode 100644 index 0000000..65ca41f --- /dev/null +++ b/src/main/java/modernfarmer/server/farmususer/user/dto/response/UserMotivationResponseDto.java @@ -0,0 +1,13 @@ +package modernfarmer.server.farmususer.user.dto.response; + +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; + +@AllArgsConstructor(staticName = "of") +@NoArgsConstructor +@Getter +public class UserMotivationResponseDto { + + private String motivation; +} diff --git a/src/main/java/modernfarmer/server/farmususer/user/service/UserService.java b/src/main/java/modernfarmer/server/farmususer/user/service/UserService.java index be251bf..4d4c109 100644 --- a/src/main/java/modernfarmer/server/farmususer/user/service/UserService.java +++ b/src/main/java/modernfarmer/server/farmususer/user/service/UserService.java @@ -5,10 +5,12 @@ import modernfarmer.server.farmususer.community.CommunityServiceFeignClient; import modernfarmer.server.farmususer.farm.FarmServiceFeignClient; import modernfarmer.server.farmususer.global.config.s3.S3Uploader; +import modernfarmer.server.farmususer.global.enums.Motivations; import modernfarmer.server.farmususer.global.exception.fail.ErrorMessage; import modernfarmer.server.farmususer.global.exception.success.SuccessMessage; import modernfarmer.server.farmususer.user.dto.response.*; import modernfarmer.server.farmususer.user.entity.User; +import modernfarmer.server.farmususer.user.entity.UserMotivation; import modernfarmer.server.farmususer.user.repository.UserRepository; import modernfarmer.server.farmususer.user.util.JwtTokenProvider; import modernfarmer.server.farmususer.user.util.TimeCalculator; @@ -18,8 +20,7 @@ import org.springframework.web.multipart.MultipartFile; import java.io.IOException; -import java.util.List; -import java.util.Optional; +import java.util.*; import java.util.stream.Collectors; @@ -207,10 +208,40 @@ public String getUserLevel(Long userId) { public GetUserLevelAndNicknameResponseDto getUserLevelAndNickname(Long userId) { User user = userRepository.findById(userId).orElseThrow(() -> new IllegalArgumentException("해당 유저가 없습니다. id=" + userId)); - return GetUserLevelAndNicknameResponseDto.of(user.getLevel(), user.getNickname()); + return GetUserLevelAndNicknameResponseDto.of(user.getLevel(), user.getNickname(), getUserMotivation(userId).getMotivation()); } + public UserMotivationResponseDto getUserMotivation(Long userId) { + User user = userRepository.findById(userId).orElseThrow(() -> new IllegalArgumentException("해당 유저가 없습니다. id=" + userId)); + + Set motivations = user.getUserMotivations(); + + log.info(motivations.toString()); + + List result = motivations.stream() + .map(um -> { return um.getMotivation().getId(); } ) + .collect(Collectors.toList()); + + List motivationResult = new ArrayList<>(); + + log.info(result.toString()); + + for (Long l : result) { + if (l == 1) { + Arrays.stream(Motivations.Motive_1.values()).forEach(m -> motivationResult.add(m.getMotive())); + } else if (l == 2) { + Arrays.stream(Motivations.Motive_2.values()).forEach(m -> motivationResult.add(m.getMotive())); + } else { + Arrays.stream(Motivations.Motive_3.values()).forEach(m -> motivationResult.add(m.getMotive())); + } + } + + log.info(motivationResult.toString()); + + // motivationResult에서 랜덤으로 하나를 뽑아서 리턴 + return UserMotivationResponseDto.of(motivationResult.get((int) (Math.random() * motivationResult.size()))); + }