Skip to content

Commit

Permalink
Merge pull request #77 from Mojacknong/feature_76/응원말-API
Browse files Browse the repository at this point in the history
Feature 76/응원말 api
  • Loading branch information
MinchoGreenT authored Aug 14, 2024
2 parents 55f6ee0 + c8b83bc commit b7a683e
Show file tree
Hide file tree
Showing 9 changed files with 104 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<Void> settingMotivation(@AuthenticationPrincipal CustomUser user,
@Validated @RequestBody SetMotivationRequest setMotivationRequest
) {
Expand All @@ -34,7 +36,7 @@ public BaseResponseDto<Void> settingMotivation(@AuthenticationPrincipal CustomUs
setMotivationRequest);
}

@PostMapping(value = "/on-boarding/level")
@PostMapping(value = "/level")
public BaseResponseDto<SetLevelResponse> settingLevel(@AuthenticationPrincipal CustomUser user,
@Validated @RequestBody SetLevelRequest setLevelRequest
) {
Expand All @@ -43,9 +45,15 @@ public BaseResponseDto<SetLevelResponse> settingLevel(@AuthenticationPrincipal C
}


@PatchMapping (value = "/on-boarding/complete")
@PatchMapping (value = "/complete")
public BaseResponseDto<Void> 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<Void> settingProfile(
@AuthenticationPrincipal CustomUser user,
Expand All @@ -67,13 +65,8 @@ public BaseResponseDto<Void> settingProfile(
}

@PostMapping("/init/{userId}")
public void initUser(
@PathVariable Long userId
) {
public void initUser(@PathVariable Long userId) {
userService.initUser(userId);
}




}
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<UserMotivation, Long> {

@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<UserMotivation> findUserMotivationByUserId(@Param("userId") Long userId);



}
Original file line number Diff line number Diff line change
Expand Up @@ -15,34 +15,27 @@
public interface UserRepository extends JpaRepository<User, Long> {

Optional<User> 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);


}
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -32,42 +40,37 @@ public BaseResponseDto settingMotivation(Long userId, SetMotivationRequest setMo
}
@Transactional
public BaseResponseDto<SetLevelResponse> 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<Void> completeOnBoarding(Long userId) {
updateCompleteBoarding(userId);
return BaseResponseDto.of(SuccessCode.SUCCESS, null);
}


@Transactional
public EncouragementMessageDto bringEncouragementMessage(Long userId) {
List<UserMotivation> 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;
Expand Down Expand Up @@ -103,7 +106,6 @@ private String measureLevel(int time,String skill) {
}
return "알 수 없음";
}

public User findUser(Long userId) {
return userRepository.findById(userId).orElseThrow(() -> new UserNotFoundException("해당 유저가 없습니다."));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.Optional;
import java.util.Random;

@Slf4j
@AllArgsConstructor
Expand Down Expand Up @@ -63,15 +64,12 @@ public BaseResponseDto<Void> 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("유저를 찾을 수 없습니다.");
Expand Down
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}

0 comments on commit b7a683e

Please sign in to comment.