diff --git a/src/main/java/com/wakeUpTogetUp/togetUp/api/alarm/AlarmService.java b/src/main/java/com/wakeUpTogetUp/togetUp/api/alarm/AlarmService.java index 682f5dde..a1a9c619 100644 --- a/src/main/java/com/wakeUpTogetUp/togetUp/api/alarm/AlarmService.java +++ b/src/main/java/com/wakeUpTogetUp/togetUp/api/alarm/AlarmService.java @@ -10,7 +10,7 @@ import com.wakeUpTogetUp.togetUp.api.mission.repository.MissionObjectRepository; import com.wakeUpTogetUp.togetUp.api.mission.repository.MissionRepository; import com.wakeUpTogetUp.togetUp.api.users.UserRepository; -import com.wakeUpTogetUp.togetUp.api.users.model.User; +import com.wakeUpTogetUp.togetUp.api.users.domain.User; import com.wakeUpTogetUp.togetUp.common.Status; import com.wakeUpTogetUp.togetUp.exception.BaseException; import java.time.LocalTime; diff --git a/src/main/java/com/wakeUpTogetUp/togetUp/api/alarm/model/Alarm.java b/src/main/java/com/wakeUpTogetUp/togetUp/api/alarm/model/Alarm.java index f90571db..8e6059d2 100644 --- a/src/main/java/com/wakeUpTogetUp/togetUp/api/alarm/model/Alarm.java +++ b/src/main/java/com/wakeUpTogetUp/togetUp/api/alarm/model/Alarm.java @@ -3,7 +3,7 @@ import com.wakeUpTogetUp.togetUp.api.mission.model.Mission; import com.wakeUpTogetUp.togetUp.api.mission.model.MissionObject; import com.wakeUpTogetUp.togetUp.api.room.model.Room; -import com.wakeUpTogetUp.togetUp.api.users.model.User; +import com.wakeUpTogetUp.togetUp.api.users.domain.User; import java.time.DayOfWeek; import java.time.LocalTime; @@ -16,7 +16,6 @@ import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.ManyToOne; -import javax.persistence.OneToOne; import javax.persistence.Table; import lombok.AllArgsConstructor; diff --git a/src/main/java/com/wakeUpTogetUp/togetUp/api/auth/dto/response/LoginRes.java b/src/main/java/com/wakeUpTogetUp/togetUp/api/auth/dto/response/LoginRes.java index 48f578b9..b5676167 100644 --- a/src/main/java/com/wakeUpTogetUp/togetUp/api/auth/dto/response/LoginRes.java +++ b/src/main/java/com/wakeUpTogetUp/togetUp/api/auth/dto/response/LoginRes.java @@ -1,6 +1,6 @@ package com.wakeUpTogetUp.togetUp.api.auth.dto.response; -import com.wakeUpTogetUp.togetUp.api.users.vo.UserStat; +import com.wakeUpTogetUp.togetUp.api.users.domain.vo.UserStat; import lombok.*; @Builder diff --git a/src/main/java/com/wakeUpTogetUp/togetUp/api/auth/service/AuthService.java b/src/main/java/com/wakeUpTogetUp/togetUp/api/auth/service/AuthService.java index 65e11701..fb300a18 100644 --- a/src/main/java/com/wakeUpTogetUp/togetUp/api/auth/service/AuthService.java +++ b/src/main/java/com/wakeUpTogetUp/togetUp/api/auth/service/AuthService.java @@ -5,10 +5,10 @@ import com.wakeUpTogetUp.togetUp.api.auth.dto.response.AppleTokenRes; import com.wakeUpTogetUp.togetUp.api.auth.dto.response.LoginRes; import com.wakeUpTogetUp.togetUp.api.auth.dto.response.SocialUserRes; -import com.wakeUpTogetUp.togetUp.api.avatar.application.UserAvatarService; +import com.wakeUpTogetUp.togetUp.api.avatar.application.UserAvatarQueryService; import com.wakeUpTogetUp.togetUp.api.users.UserService; -import com.wakeUpTogetUp.togetUp.api.users.model.User; -import com.wakeUpTogetUp.togetUp.api.users.vo.UserStat; +import com.wakeUpTogetUp.togetUp.api.users.domain.User; +import com.wakeUpTogetUp.togetUp.api.users.domain.vo.UserStat; import com.wakeUpTogetUp.togetUp.utils.JwtService; import java.io.IOException; import java.util.List; @@ -27,7 +27,7 @@ public class AuthService { private final List loginServices; private final JwtService jwtService; private final UserService userService; - private final UserAvatarService userAvatarService; + private final UserAvatarQueryService userAvatarQueryService; private final AppleLoginServiceImpl appleLoginService; @Transactional @@ -53,7 +53,7 @@ public LoginRes socialLogin(SocialLoginReq socialLoginReq) { .userName(user.getName()) .email(socialUserRes.getEmail()) .accessToken(accessToken) - .avatarId(userAvatarService.getUserAvatarId(user.getId())) + .avatarId(userAvatarQueryService.getUserAvatarId(user.getId())) .userStat(UserStat.from(user)) .build(); } diff --git a/src/main/java/com/wakeUpTogetUp/togetUp/api/avatar/application/UserAvatarQueryService.java b/src/main/java/com/wakeUpTogetUp/togetUp/api/avatar/application/UserAvatarQueryService.java new file mode 100644 index 00000000..42f401b6 --- /dev/null +++ b/src/main/java/com/wakeUpTogetUp/togetUp/api/avatar/application/UserAvatarQueryService.java @@ -0,0 +1,37 @@ +package com.wakeUpTogetUp.togetUp.api.avatar.application; + +import com.wakeUpTogetUp.togetUp.api.avatar.dto.response.UserAvatarResponse; +import com.wakeUpTogetUp.togetUp.api.avatar.model.Avatar; +import com.wakeUpTogetUp.togetUp.api.avatar.model.UserAvatar; +import com.wakeUpTogetUp.togetUp.api.avatar.repository.AvatarRepository; +import com.wakeUpTogetUp.togetUp.api.avatar.repository.UserAvatarRepository; +import com.wakeUpTogetUp.togetUp.common.Status; +import com.wakeUpTogetUp.togetUp.exception.BaseException; +import com.wakeUpTogetUp.togetUp.utils.mapper.EntityDtoMapper; +import java.util.List; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +@Service +@RequiredArgsConstructor +@Transactional(readOnly = true) +public class UserAvatarQueryService { + + private final AvatarRepository avatarRepository; + private final UserAvatarRepository userAvatarRepository; + + public List findUserAvatarList(int userId) { + List avatarList = avatarRepository.findAll(); + List userAvatarList = userAvatarRepository.findAllByUser_Id(userId); + + return EntityDtoMapper.INSTANCE.toUserAvatarDataList(avatarList, userAvatarList); + } + + public int getUserAvatarId(int userId) { + return userAvatarRepository.findByUser_IdAndIsActiveIsTrue(userId) + .orElseThrow(() -> new BaseException(Status.FIND_USER_AVATAR_FAIL)) + .getAvatar() + .getId(); + } +} diff --git a/src/main/java/com/wakeUpTogetUp/togetUp/api/avatar/application/UserAvatarService.java b/src/main/java/com/wakeUpTogetUp/togetUp/api/avatar/application/UserAvatarService.java index faa7b954..00382d8c 100644 --- a/src/main/java/com/wakeUpTogetUp/togetUp/api/avatar/application/UserAvatarService.java +++ b/src/main/java/com/wakeUpTogetUp/togetUp/api/avatar/application/UserAvatarService.java @@ -2,15 +2,14 @@ import static com.wakeUpTogetUp.togetUp.common.Constant.DEFAULT_AVATAR_ID; -import com.wakeUpTogetUp.togetUp.api.avatar.dto.response.UserAvatarResponse; +import com.wakeUpTogetUp.togetUp.api.avatar.application.model.UnlockAvatarResult; import com.wakeUpTogetUp.togetUp.api.avatar.model.Avatar; import com.wakeUpTogetUp.togetUp.api.avatar.model.UserAvatar; import com.wakeUpTogetUp.togetUp.api.avatar.repository.AvatarRepository; import com.wakeUpTogetUp.togetUp.api.avatar.repository.UserAvatarRepository; -import com.wakeUpTogetUp.togetUp.api.users.model.User; +import com.wakeUpTogetUp.togetUp.api.users.domain.User; import com.wakeUpTogetUp.togetUp.common.Status; import com.wakeUpTogetUp.togetUp.exception.BaseException; -import com.wakeUpTogetUp.togetUp.utils.mapper.EntityDtoMapper; import java.util.List; import java.util.Optional; import lombok.RequiredArgsConstructor; @@ -19,34 +18,13 @@ @Service @RequiredArgsConstructor +@Transactional public class UserAvatarService { private final AvatarRepository avatarRepository; private final UserAvatarRepository userAvatarRepository; private final UserAvatarValidationService userAvatarValidationService; - @Transactional(readOnly = true) - public List findUserAvatarList(int userId) { - List avatarList = avatarRepository.findAll(); - List userAvatarList = userAvatarRepository.findAllByUser_Id(userId); - - return EntityDtoMapper.INSTANCE.toUserAvatarDataList(avatarList, userAvatarList); - } - - @Transactional - public boolean unlockAvatarIfAvailableExist(User user) { - Optional avatarAvailable = avatarRepository.findAvatarByUnlockLevel(user.getLevel()); - avatarAvailable.ifPresent(avatar -> unlockAvatar(user, avatar)); - - return avatarAvailable.isPresent(); - } - - private void unlockAvatar(User user, Avatar avatar) { - if (userAvatarValidationService.isUserAvatarNotExist(user.getId(), avatar.getId())) { - createUserAvatar(user, avatar); - } - } - private void createUserAvatar(User user, Avatar avatar) { UserAvatar userAvatar = UserAvatar.builder() .user(user) @@ -56,7 +34,6 @@ private void createUserAvatar(User user, Avatar avatar) { userAvatarRepository.save(userAvatar); } - @Transactional public void setUserDefaultAvatar(User user) { Avatar defaultAvatar = avatarRepository.findById(DEFAULT_AVATAR_ID) .orElseThrow(() -> new BaseException(Status.FIND_DEFAULT_AVATAR_FAIL)); @@ -65,10 +42,8 @@ public void setUserDefaultAvatar(User user) { changeUserAvatar(user.getId(), DEFAULT_AVATAR_ID); } - @Transactional public void changeUserAvatar(int userId, int avatarId) { - List userAvatarList = - userAvatarRepository.findAllByUser_Id(userId); + List userAvatarList = userAvatarRepository.findAllByUser_Id(userId); userAvatarList.stream() .filter(i -> i.getAvatar().getId() == avatarId) @@ -80,11 +55,20 @@ public void changeUserAvatar(int userId, int avatarId) { } } - @Transactional(readOnly = true) - public int getUserAvatarId(int userId) { - return userAvatarRepository.findByUser_IdAndIsActiveIsTrue(userId) - .orElseThrow(() -> new BaseException(Status.FIND_USER_AVATAR_FAIL)) - .getAvatar() - .getId(); + public UnlockAvatarResult attemptToUnlockAvatar(User user) { + Optional avatarAvailable = avatarRepository.findAvatarByUnlockLevel(user.getLevel()); + + if (avatarAvailable.isPresent()) { + unlockAvatar(user, avatarAvailable.get()); + return new UnlockAvatarResult(true); + } + + return new UnlockAvatarResult(false); + } + + private void unlockAvatar(User user, Avatar avatar) { + if (userAvatarValidationService.isUserAvatarNotExist(user.getId(), avatar.getId())) { + createUserAvatar(user, avatar); + } } } diff --git a/src/main/java/com/wakeUpTogetUp/togetUp/api/avatar/application/model/UnlockAvatarResult.java b/src/main/java/com/wakeUpTogetUp/togetUp/api/avatar/application/model/UnlockAvatarResult.java new file mode 100644 index 00000000..286b83b2 --- /dev/null +++ b/src/main/java/com/wakeUpTogetUp/togetUp/api/avatar/application/model/UnlockAvatarResult.java @@ -0,0 +1,11 @@ +package com.wakeUpTogetUp.togetUp.api.avatar.application.model; + +import lombok.Getter; +import lombok.RequiredArgsConstructor; + +@Getter +@RequiredArgsConstructor +public class UnlockAvatarResult { + + private final boolean isUnlocked; +} diff --git a/src/main/java/com/wakeUpTogetUp/togetUp/api/avatar/model/UserAvatar.java b/src/main/java/com/wakeUpTogetUp/togetUp/api/avatar/model/UserAvatar.java index f1165e47..1def039f 100644 --- a/src/main/java/com/wakeUpTogetUp/togetUp/api/avatar/model/UserAvatar.java +++ b/src/main/java/com/wakeUpTogetUp/togetUp/api/avatar/model/UserAvatar.java @@ -1,6 +1,6 @@ package com.wakeUpTogetUp.togetUp.api.avatar.model; -import com.wakeUpTogetUp.togetUp.api.users.model.User; +import com.wakeUpTogetUp.togetUp.api.users.domain.User; import java.time.LocalDateTime; import javax.persistence.Column; import javax.persistence.Entity; diff --git a/src/main/java/com/wakeUpTogetUp/togetUp/api/dev/DevController.java b/src/main/java/com/wakeUpTogetUp/togetUp/api/dev/DevController.java index e184f1b4..49696bc5 100644 --- a/src/main/java/com/wakeUpTogetUp/togetUp/api/dev/DevController.java +++ b/src/main/java/com/wakeUpTogetUp/togetUp/api/dev/DevController.java @@ -5,8 +5,8 @@ import com.wakeUpTogetUp.togetUp.api.dev.dto.response.AppStoreUrlRes; import com.wakeUpTogetUp.togetUp.api.mission.dto.request.MissionCompleteReq; import com.wakeUpTogetUp.togetUp.api.mission.dto.response.MissionCompleteRes; -import com.wakeUpTogetUp.togetUp.api.users.model.User; -import com.wakeUpTogetUp.togetUp.api.users.vo.UserStat; +import com.wakeUpTogetUp.togetUp.api.users.domain.User; +import com.wakeUpTogetUp.togetUp.api.users.domain.vo.UserStat; import com.wakeUpTogetUp.togetUp.common.Status; import com.wakeUpTogetUp.togetUp.common.dto.BaseResponse; import io.swagger.v3.oas.annotations.Operation; @@ -68,13 +68,15 @@ public BaseResponse processMissionCompletion( @Parameter(hidden = true) @AuthUser Integer userId, @RequestBody @Valid MissionCompleteReq missionCompleteReq ) { - User user = new User(userId, - "1234", - "이예원", - "example@gmail.com", - LoginType.KAKAO, - 30, - 3); + User user = User.builder() + .id(userId) + .socialId("1234") + .name("이예원") + .email("example@gmail.com") + .loginType(LoginType.KAKAO) + .expPoint(30) + .level(3) + .build(); MissionCompleteRes response = MissionCompleteRes.builder() .userStat(UserStat.from(user)) diff --git a/src/main/java/com/wakeUpTogetUp/togetUp/api/dev/DevService.java b/src/main/java/com/wakeUpTogetUp/togetUp/api/dev/DevService.java index 4574f712..ecb96a6d 100644 --- a/src/main/java/com/wakeUpTogetUp/togetUp/api/dev/DevService.java +++ b/src/main/java/com/wakeUpTogetUp/togetUp/api/dev/DevService.java @@ -2,7 +2,7 @@ import com.wakeUpTogetUp.togetUp.api.users.UserRepository; -import com.wakeUpTogetUp.togetUp.api.users.model.User; +import com.wakeUpTogetUp.togetUp.api.users.domain.User; import com.wakeUpTogetUp.togetUp.utils.JwtService; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; diff --git a/src/main/java/com/wakeUpTogetUp/togetUp/api/mission/dto/response/MissionCompleteRes.java b/src/main/java/com/wakeUpTogetUp/togetUp/api/mission/dto/response/MissionCompleteRes.java index a3aacb55..c67be9fc 100644 --- a/src/main/java/com/wakeUpTogetUp/togetUp/api/mission/dto/response/MissionCompleteRes.java +++ b/src/main/java/com/wakeUpTogetUp/togetUp/api/mission/dto/response/MissionCompleteRes.java @@ -1,6 +1,6 @@ package com.wakeUpTogetUp.togetUp.api.mission.dto.response; -import com.wakeUpTogetUp.togetUp.api.users.vo.UserStat; +import com.wakeUpTogetUp.togetUp.api.users.domain.vo.UserStat; import io.swagger.v3.oas.annotations.media.Schema; import lombok.AccessLevel; import lombok.AllArgsConstructor; diff --git a/src/main/java/com/wakeUpTogetUp/togetUp/api/mission/model/MissionLog.java b/src/main/java/com/wakeUpTogetUp/togetUp/api/mission/model/MissionLog.java index ead87df5..185ebc43 100644 --- a/src/main/java/com/wakeUpTogetUp/togetUp/api/mission/model/MissionLog.java +++ b/src/main/java/com/wakeUpTogetUp/togetUp/api/mission/model/MissionLog.java @@ -1,8 +1,8 @@ package com.wakeUpTogetUp.togetUp.api.mission.model; import com.wakeUpTogetUp.togetUp.api.room.model.Room; -import com.wakeUpTogetUp.togetUp.api.users.model.User; -import com.wakeUpTogetUp.togetUp.utils.TimeFormatter; +import com.wakeUpTogetUp.togetUp.api.users.domain.User; + import lombok.*; import org.hibernate.annotations.DynamicInsert; diff --git a/src/main/java/com/wakeUpTogetUp/togetUp/api/mission/service/MissionService.java b/src/main/java/com/wakeUpTogetUp/togetUp/api/mission/service/MissionService.java index 6087ea06..1cd3442f 100644 --- a/src/main/java/com/wakeUpTogetUp/togetUp/api/mission/service/MissionService.java +++ b/src/main/java/com/wakeUpTogetUp/togetUp/api/mission/service/MissionService.java @@ -5,6 +5,7 @@ import com.wakeUpTogetUp.togetUp.api.alarm.AlarmRepository; import com.wakeUpTogetUp.togetUp.api.alarm.model.Alarm; import com.wakeUpTogetUp.togetUp.api.avatar.application.UserAvatarService; +import com.wakeUpTogetUp.togetUp.api.avatar.application.model.UnlockAvatarResult; import com.wakeUpTogetUp.togetUp.api.mission.domain.CustomAnalysisEntity; import com.wakeUpTogetUp.togetUp.api.mission.domain.VisionAnalysisResult; import com.wakeUpTogetUp.togetUp.api.mission.dto.request.MissionCompleteReq; @@ -13,11 +14,11 @@ import com.wakeUpTogetUp.togetUp.api.mission.model.MissionType; import com.wakeUpTogetUp.togetUp.api.mission.repository.MissionLogRepository; import com.wakeUpTogetUp.togetUp.api.notification.NotificationService; +import com.wakeUpTogetUp.togetUp.api.users.UserProgressService; import com.wakeUpTogetUp.togetUp.api.users.UserRepository; -import com.wakeUpTogetUp.togetUp.api.users.UserService; -import com.wakeUpTogetUp.togetUp.api.users.model.User; -import com.wakeUpTogetUp.togetUp.api.users.vo.UserProgressResult; -import com.wakeUpTogetUp.togetUp.api.users.vo.UserStat; +import com.wakeUpTogetUp.togetUp.api.users.domain.User; +import com.wakeUpTogetUp.togetUp.api.users.domain.model.UserProgressResult; +import com.wakeUpTogetUp.togetUp.api.users.domain.vo.UserStat; import com.wakeUpTogetUp.togetUp.common.Status; import com.wakeUpTogetUp.togetUp.exception.BaseException; import java.util.List; @@ -32,15 +33,17 @@ @Slf4j public class MissionService { - private final int MAX_MATCHES_LIMIT = 3; + private static final int MAX_MATCHES_LIMIT = 3; + + private final UserProgressService userProgressService; private final VisionServiceFactory visionServiceFactory; + private final UserAvatarService userAvatarService; + private final NotificationService notificationService; + private final AlarmRepository alarmRepository; private final MissionLogRepository missionLogRepository; - private final UserService userService; - private final UserAvatarService userAvatarService; private final UserRepository userRepository; - private final NotificationService notificationService; public List getMissionResult(MissionType type, String object, MultipartFile missionImage) { VisionAnalysisResult result = visionServiceFactory @@ -63,19 +66,21 @@ public MissionCompleteRes afterMissionComplete(int userId, MissionCompleteReq re Alarm alarm = alarmRepository.findById(req.getAlarmId()) .orElseThrow(() -> new BaseException(Status.ALARM_NOT_FOUND)); - UserProgressResult progressResult = userService.userProgress(user); - createMissionLog(user, req); - sendNotificationIfRoomExists(alarm, user); + UserProgressResult progressResult = userProgressService.progress(user); - boolean isNewAvatarAvailable = false; + boolean isAvatarUnlocked = false; if (progressResult.isUserLevelUp()) { - isNewAvatarAvailable = userAvatarService.unlockAvatarIfAvailableExist(user); + UnlockAvatarResult unlockAvatarResult = userAvatarService.attemptToUnlockAvatar(user); + isAvatarUnlocked = unlockAvatarResult.isUnlocked(); } + createMissionLog(user, req); + sendNotificationIfRoomExists(alarm, user); + return MissionCompleteRes.builder() .userStat(UserStat.from(user)) .isUserLevelUp(progressResult.isUserLevelUp()) - .isUserLevelUp(isNewAvatarAvailable) + .isAvatarUnlockAvailable(isAvatarUnlocked) .build(); } diff --git a/src/main/java/com/wakeUpTogetUp/togetUp/api/notification/NotificationService.java b/src/main/java/com/wakeUpTogetUp/togetUp/api/notification/NotificationService.java index 405cd5ee..40e16747 100644 --- a/src/main/java/com/wakeUpTogetUp/togetUp/api/notification/NotificationService.java +++ b/src/main/java/com/wakeUpTogetUp/togetUp/api/notification/NotificationService.java @@ -9,7 +9,7 @@ import com.wakeUpTogetUp.togetUp.api.users.UserRepository; import com.wakeUpTogetUp.togetUp.api.users.UserService; import com.wakeUpTogetUp.togetUp.api.users.fcmToken.FcmTokenRepository; -import com.wakeUpTogetUp.togetUp.api.users.model.User; +import com.wakeUpTogetUp.togetUp.api.users.domain.User; import com.wakeUpTogetUp.togetUp.common.Status; import com.wakeUpTogetUp.togetUp.exception.BaseException; import com.wakeUpTogetUp.togetUp.infra.fcm.FcmService; diff --git a/src/main/java/com/wakeUpTogetUp/togetUp/api/notification/vo/RoomMissionLogNotificationEvent.java b/src/main/java/com/wakeUpTogetUp/togetUp/api/notification/vo/RoomMissionLogNotificationEvent.java index d8e7711a..f56b327c 100644 --- a/src/main/java/com/wakeUpTogetUp/togetUp/api/notification/vo/RoomMissionLogNotificationEvent.java +++ b/src/main/java/com/wakeUpTogetUp/togetUp/api/notification/vo/RoomMissionLogNotificationEvent.java @@ -5,7 +5,7 @@ import com.wakeUpTogetUp.togetUp.api.notification.DataValueType; import com.wakeUpTogetUp.togetUp.api.room.model.Room; import com.wakeUpTogetUp.togetUp.api.users.fcmToken.FcmToken; -import com.wakeUpTogetUp.togetUp.api.users.model.User; +import com.wakeUpTogetUp.togetUp.api.users.domain.User; import java.util.HashMap; import java.util.List; diff --git a/src/main/java/com/wakeUpTogetUp/togetUp/api/room/RoomService.java b/src/main/java/com/wakeUpTogetUp/togetUp/api/room/RoomService.java index b99d5350..f6114e15 100644 --- a/src/main/java/com/wakeUpTogetUp/togetUp/api/room/RoomService.java +++ b/src/main/java/com/wakeUpTogetUp/togetUp/api/room/RoomService.java @@ -11,7 +11,7 @@ import com.wakeUpTogetUp.togetUp.api.room.model.Room; import com.wakeUpTogetUp.togetUp.api.room.model.RoomUser; import com.wakeUpTogetUp.togetUp.api.users.UserRepository; -import com.wakeUpTogetUp.togetUp.api.users.model.User; +import com.wakeUpTogetUp.togetUp.api.users.domain.User; import com.wakeUpTogetUp.togetUp.common.Status; import com.wakeUpTogetUp.togetUp.exception.BaseException; import com.wakeUpTogetUp.togetUp.utils.mapper.AlarmMigrationMapper; diff --git a/src/main/java/com/wakeUpTogetUp/togetUp/api/room/model/RoomUser.java b/src/main/java/com/wakeUpTogetUp/togetUp/api/room/model/RoomUser.java index d52ab4a3..43c6c454 100644 --- a/src/main/java/com/wakeUpTogetUp/togetUp/api/room/model/RoomUser.java +++ b/src/main/java/com/wakeUpTogetUp/togetUp/api/room/model/RoomUser.java @@ -1,8 +1,7 @@ package com.wakeUpTogetUp.togetUp.api.room.model; -import com.wakeUpTogetUp.togetUp.api.users.model.User; +import com.wakeUpTogetUp.togetUp.api.users.domain.User; import lombok.*; -import org.hibernate.annotations.Where; import javax.persistence.*; import java.sql.Timestamp; diff --git a/src/main/java/com/wakeUpTogetUp/togetUp/api/users/UserController.java b/src/main/java/com/wakeUpTogetUp/togetUp/api/users/UserController.java index daa645fe..d8097880 100644 --- a/src/main/java/com/wakeUpTogetUp/togetUp/api/users/UserController.java +++ b/src/main/java/com/wakeUpTogetUp/togetUp/api/users/UserController.java @@ -2,6 +2,7 @@ import com.wakeUpTogetUp.togetUp.api.auth.AuthUser; import com.wakeUpTogetUp.togetUp.api.auth.service.AuthService; +import com.wakeUpTogetUp.togetUp.api.avatar.application.UserAvatarQueryService; import com.wakeUpTogetUp.togetUp.api.avatar.application.UserAvatarService; import com.wakeUpTogetUp.togetUp.api.avatar.dto.response.UserAvatarResponse; import com.wakeUpTogetUp.togetUp.api.users.dto.request.AppleUserDeleteReq; @@ -34,6 +35,7 @@ public class UserController { private final UserService userService; private final UserAvatarService userAvatarService; + private final UserAvatarQueryService userAvatarQueryService; private final AuthService authService; @Operation(summary = "fcmToken 등록") @@ -41,7 +43,9 @@ public class UserController { @ApiResponse(responseCode = "200", description = "요청에 성공하였습니다."), @ApiResponse(responseCode = "404", description = "존재하지 않는 유저 입니다.")}) @PostMapping("/fcm-token") - public BaseResponse updateFcmToken(@Parameter(hidden = true) @AuthUser Integer userId, @Parameter(description = "토큰값", required = true) @RequestParam String fcmToken) { + public BaseResponse updateFcmToken( + @Parameter(hidden = true) @AuthUser Integer userId, + @Parameter(description = "토큰값", required = true) @RequestParam String fcmToken) { userService.registerFcmToken(userId, fcmToken); return new BaseResponse<>(Status.SUCCESS_CREATED); } @@ -52,8 +56,9 @@ public BaseResponse updateFcmToken(@Parameter(hidden = true) @AuthUser @ApiResponse(responseCode = "200", description = "요청에 성공하였습니다."), @ApiResponse(responseCode = "404", description = "존재하지 않는 유저 입니다.")}) @PatchMapping("/push") - public BaseResponse updateAgreePush(@Parameter(hidden = true) @AuthUser Integer userId, - @Parameter(description = "알람동의 값", example = "true") @RequestParam() boolean agreePush) { + public BaseResponse updateAgreePush( + @Parameter(hidden = true) @AuthUser Integer userId, + @Parameter(description = "알람동의 값", example = "true") @RequestParam boolean agreePush) { userService.updateAgreePush(userId, agreePush); return new BaseResponse<>(Status.SUCCESS); } @@ -76,9 +81,9 @@ public BaseResponse deleteKaKaoUser( @ApiResponse(responseCode = "404", description = "존재하지 않는 유저 입니다."), }) @DeleteMapping("apple") - public BaseResponse deleteAppleUser(@Parameter(hidden = true) @AuthUser Integer userId, - @RequestBody @Valid AppleUserDeleteReq appleUserDeleteReq) - throws IOException { + public BaseResponse deleteAppleUser( + @Parameter(hidden = true) @AuthUser Integer userId, + @RequestBody @Valid AppleUserDeleteReq appleUserDeleteReq) throws IOException { authService.disconnectApple(appleUserDeleteReq.getAuthorizationCode()); userService.deleteById(userId); return new BaseResponse<>(Status.SUCCESS); @@ -86,18 +91,16 @@ public BaseResponse deleteAppleUser(@Parameter(hidden = true) @AuthUser @Operation(summary = "아바타 목록 가져오기") @GetMapping("/avatars") - public BaseResponse> getUserAvatarList( @Parameter(hidden = true) @AuthUser Integer userId ) { - return new BaseResponse<>(Status.SUCCESS, userAvatarService.findUserAvatarList(userId)); + return new BaseResponse<>(Status.SUCCESS, userAvatarQueryService.findUserAvatarList(userId)); } @Operation(summary = "아바타 변경") @PatchMapping("/avatars/{avatarId}/change") @ApiResponses(value = { - @ApiResponse( - responseCode = "200", description = "요청에 성공하였습니다."), + @ApiResponse(responseCode = "200", description = "요청에 성공하였습니다."), @ApiResponse(responseCode = "403", description = "유저가 보유하지 않은 아바타 ID 입니다.")}) public BaseResponse updateUserAvatar( @Parameter(hidden = true) @AuthUser Integer userId, diff --git a/src/main/java/com/wakeUpTogetUp/togetUp/api/users/UserProgressService.java b/src/main/java/com/wakeUpTogetUp/togetUp/api/users/UserProgressService.java new file mode 100644 index 00000000..e5400fd1 --- /dev/null +++ b/src/main/java/com/wakeUpTogetUp/togetUp/api/users/UserProgressService.java @@ -0,0 +1,16 @@ +package com.wakeUpTogetUp.togetUp.api.users; + +import org.springframework.stereotype.Service; + +import com.wakeUpTogetUp.togetUp.api.users.domain.User; +import com.wakeUpTogetUp.togetUp.api.users.domain.model.UserProgressResult; + +@Service +public class UserProgressService { + + private static final int EXP_GAINED_PER_MISSION = 10; + + public UserProgressResult progress(User user) { + return user.progress(EXP_GAINED_PER_MISSION); + } +} diff --git a/src/main/java/com/wakeUpTogetUp/togetUp/api/users/UserRepository.java b/src/main/java/com/wakeUpTogetUp/togetUp/api/users/UserRepository.java index 7dbda18f..600ca4b4 100644 --- a/src/main/java/com/wakeUpTogetUp/togetUp/api/users/UserRepository.java +++ b/src/main/java/com/wakeUpTogetUp/togetUp/api/users/UserRepository.java @@ -1,7 +1,7 @@ package com.wakeUpTogetUp.togetUp.api.users; import com.wakeUpTogetUp.togetUp.api.room.dto.response.UserProfileData; -import com.wakeUpTogetUp.togetUp.api.users.model.User; +import com.wakeUpTogetUp.togetUp.api.users.domain.User; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.query.Param; diff --git a/src/main/java/com/wakeUpTogetUp/togetUp/api/users/UserService.java b/src/main/java/com/wakeUpTogetUp/togetUp/api/users/UserService.java index 95c44d19..46996472 100644 --- a/src/main/java/com/wakeUpTogetUp/togetUp/api/users/UserService.java +++ b/src/main/java/com/wakeUpTogetUp/togetUp/api/users/UserService.java @@ -8,8 +8,7 @@ import com.wakeUpTogetUp.togetUp.api.room.RoomUserRepository; import com.wakeUpTogetUp.togetUp.api.users.fcmToken.FcmToken; import com.wakeUpTogetUp.togetUp.api.users.fcmToken.FcmTokenRepository; -import com.wakeUpTogetUp.togetUp.api.users.model.User; -import com.wakeUpTogetUp.togetUp.api.users.vo.UserProgressResult; +import com.wakeUpTogetUp.togetUp.api.users.domain.User; import com.wakeUpTogetUp.togetUp.common.Status; import com.wakeUpTogetUp.togetUp.exception.BaseException; @@ -59,7 +58,6 @@ public void registerFcmToken(Integer userId, String fcmTokenValue) { if (!fcmTokenRepository.existsByValue(fcmTokenValue)) { saveFcmToken(user, fcmTokenValue); } - } private void saveFcmToken(User user, String fcmTokenValue) { @@ -93,13 +91,6 @@ public void deleteById(Integer userId) { } } - public UserProgressResult userProgress(User user) { - UserProgressResult result = user.progress(); - userRepository.save(user); - - return result; - } - public List getAgreedNotiUsersIds() { return userRepository.findAllByAgreePushIsTrue().stream() .map(User::getId) diff --git a/src/main/java/com/wakeUpTogetUp/togetUp/api/users/UserServiceHelper.java b/src/main/java/com/wakeUpTogetUp/togetUp/api/users/UserServiceHelper.java index dcf44b72..53f89c51 100644 --- a/src/main/java/com/wakeUpTogetUp/togetUp/api/users/UserServiceHelper.java +++ b/src/main/java/com/wakeUpTogetUp/togetUp/api/users/UserServiceHelper.java @@ -1,6 +1,6 @@ package com.wakeUpTogetUp.togetUp.api.users; -import com.wakeUpTogetUp.togetUp.api.users.model.User; +import com.wakeUpTogetUp.togetUp.api.users.domain.User; import com.wakeUpTogetUp.togetUp.common.Status; import com.wakeUpTogetUp.togetUp.exception.BaseException; diff --git a/src/main/java/com/wakeUpTogetUp/togetUp/api/users/model/User.java b/src/main/java/com/wakeUpTogetUp/togetUp/api/users/domain/User.java similarity index 64% rename from src/main/java/com/wakeUpTogetUp/togetUp/api/users/model/User.java rename to src/main/java/com/wakeUpTogetUp/togetUp/api/users/domain/User.java index 34a26f0f..eb29d072 100644 --- a/src/main/java/com/wakeUpTogetUp/togetUp/api/users/model/User.java +++ b/src/main/java/com/wakeUpTogetUp/togetUp/api/users/domain/User.java @@ -1,11 +1,11 @@ -package com.wakeUpTogetUp.togetUp.api.users.model; +package com.wakeUpTogetUp.togetUp.api.users.domain; import com.fasterxml.jackson.annotation.JsonIgnore; import com.wakeUpTogetUp.togetUp.api.auth.LoginType; import com.wakeUpTogetUp.togetUp.api.avatar.model.UserAvatar; import com.wakeUpTogetUp.togetUp.api.room.model.RoomUser; +import com.wakeUpTogetUp.togetUp.api.users.domain.model.UserProgressResult; import com.wakeUpTogetUp.togetUp.api.users.fcmToken.FcmToken; -import com.wakeUpTogetUp.togetUp.api.users.vo.UserProgressResult; import java.sql.Timestamp; import java.time.Instant; import java.util.ArrayList; @@ -18,9 +18,11 @@ import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.OneToMany; +import javax.persistence.PostLoad; import javax.persistence.PrePersist; import javax.persistence.PreUpdate; import javax.persistence.Table; +import javax.persistence.Transient; import javax.validation.constraints.NotNull; import lombok.Builder; import lombok.Getter; @@ -40,9 +42,6 @@ @DynamicInsert public class User { - private static final int EXP_GAIN_PER_MISSION_COMPLETE = 10; - private static final int DEFAULT_LEVEL_INCREMENT = 1; - @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(columnDefinition = "INT UNSIGNED") @@ -71,6 +70,9 @@ public class User { @Column(name = "exp_point") private Integer expPoint; + @Transient + private double expPercentage; + @Column(name = "created_at") private Timestamp createdAt; @@ -103,9 +105,7 @@ void updatedAt() { } @Builder - public User(Integer id, String socialId, String name, String email, LoginType loginType, - int level, int expPoint - ) { + private User(Integer id, String socialId, String name, String email, LoginType loginType, int level, int expPoint) { this.id = id; this.socialId = socialId; this.name = name; @@ -113,46 +113,56 @@ public User(Integer id, String socialId, String name, String email, LoginType lo this.loginType = loginType; this.level = level; this.expPoint = expPoint; + initExpPercentage(); + } + + @PostLoad + private void initExpPercentage() { + updateExpPercentage(UserProgressCalculator.calculateLevelUpThreshold(this)); } - public UserProgressResult progress() { - gainExpPoint(EXP_GAIN_PER_MISSION_COMPLETE); + public UserProgressResult progress(int gainedExpPoint) { + gainExpPoint(gainedExpPoint); - int threshold = calculateLevelUpThreshold(); - boolean isUserLevelUpAvailable = isUserLevelUpAvailable(threshold); + int currentThreshold = UserProgressCalculator.calculateLevelUpThreshold(this); + int currentLevel = this.level; - if (isUserLevelUpAvailable) { - levelUp(threshold); + if (isUserLevelUpAvailable(currentThreshold)) { + levelUp(currentThreshold); + + int newThreshold = UserProgressCalculator.calculateLevelUpThreshold(this); + updateExpPercentage(newThreshold); + } else { + updateExpPercentage(currentThreshold); } - return new UserProgressResult(isUserLevelUpAvailable); + boolean isUserLevelUp = getLevel() > currentLevel; + return new UserProgressResult(isUserLevelUp); } - private void gainExpPoint(int expPoint) { - this.setExpPoint(this.getExpPoint() + expPoint); + private void gainExpPoint(int point) { + this.expPoint = this.expPoint + point; } - public boolean isUserLevelUpAvailable(int threshold) { - return this.getExpPoint() >= threshold; + private boolean isUserLevelUpAvailable(int threshold) { + return this.expPoint >= threshold; } - public void levelUp(int threshold) { - this.setLevel(this.getLevel() + DEFAULT_LEVEL_INCREMENT); - this.setExpPoint(this.getExpPoint() - threshold); + private void levelUp(int threshold) { + this.level++; + this.expPoint -= threshold; } - public int calculateLevelUpThreshold() { - return 10 + 16 * (level - 1); + private void updateExpPercentage(int threshold) { + this.expPercentage = calculateExpPercentage(threshold); } - public double calculateExpPercentage() { - double expPercentage = ((double) expPoint / calculateLevelUpThreshold()) * 100.0; + private double calculateExpPercentage(int threshold) { + double expPercentage = ((double) this.expPoint / threshold) * 100.0; return Math.round(expPercentage * 100.0) / 100.0; } - public void changeAgreePush(boolean agreePush) { this.setAgreePush(agreePush); } - } diff --git a/src/main/java/com/wakeUpTogetUp/togetUp/api/users/domain/UserProgressCalculator.java b/src/main/java/com/wakeUpTogetUp/togetUp/api/users/domain/UserProgressCalculator.java new file mode 100644 index 00000000..e7996eef --- /dev/null +++ b/src/main/java/com/wakeUpTogetUp/togetUp/api/users/domain/UserProgressCalculator.java @@ -0,0 +1,15 @@ +package com.wakeUpTogetUp.togetUp.api.users.domain; + +import java.util.function.Function; + +import com.wakeUpTogetUp.togetUp.api.users.domain.User; + +public class UserProgressCalculator { + + private static final Function LEVEL_UP_THRESHOLD_CALCULATOR = + user -> 10 + 16 * (user.getLevel() - 1); + + public static int calculateLevelUpThreshold(User user) { + return LEVEL_UP_THRESHOLD_CALCULATOR.apply(user); + } +} \ No newline at end of file diff --git a/src/main/java/com/wakeUpTogetUp/togetUp/api/users/vo/UserProgressResult.java b/src/main/java/com/wakeUpTogetUp/togetUp/api/users/domain/model/UserProgressResult.java similarity index 56% rename from src/main/java/com/wakeUpTogetUp/togetUp/api/users/vo/UserProgressResult.java rename to src/main/java/com/wakeUpTogetUp/togetUp/api/users/domain/model/UserProgressResult.java index fc661a02..30f1c08e 100644 --- a/src/main/java/com/wakeUpTogetUp/togetUp/api/users/vo/UserProgressResult.java +++ b/src/main/java/com/wakeUpTogetUp/togetUp/api/users/domain/model/UserProgressResult.java @@ -1,4 +1,4 @@ -package com.wakeUpTogetUp.togetUp.api.users.vo; +package com.wakeUpTogetUp.togetUp.api.users.domain.model; import lombok.Getter; import lombok.RequiredArgsConstructor; @@ -6,5 +6,6 @@ @Getter @RequiredArgsConstructor public class UserProgressResult { - private final boolean isUserLevelUp; + + private final boolean isUserLevelUp; } diff --git a/src/main/java/com/wakeUpTogetUp/togetUp/api/users/vo/UserStat.java b/src/main/java/com/wakeUpTogetUp/togetUp/api/users/domain/vo/UserStat.java similarity index 72% rename from src/main/java/com/wakeUpTogetUp/togetUp/api/users/vo/UserStat.java rename to src/main/java/com/wakeUpTogetUp/togetUp/api/users/domain/vo/UserStat.java index 7dd632a3..93e87f40 100644 --- a/src/main/java/com/wakeUpTogetUp/togetUp/api/users/vo/UserStat.java +++ b/src/main/java/com/wakeUpTogetUp/togetUp/api/users/domain/vo/UserStat.java @@ -1,12 +1,11 @@ -package com.wakeUpTogetUp.togetUp.api.users.vo; +package com.wakeUpTogetUp.togetUp.api.users.domain.vo; -import com.wakeUpTogetUp.togetUp.api.users.model.User; +import com.wakeUpTogetUp.togetUp.api.users.domain.User; import io.swagger.v3.oas.annotations.media.Schema; import lombok.Getter; @Getter public class UserStat { - @Schema(description = "현재 레벨") private final int level; @@ -15,7 +14,7 @@ public class UserStat { private UserStat(User user) { this.level = user.getLevel(); - this.expPercentage = user.calculateExpPercentage(); + this.expPercentage = user.getExpPercentage(); } public static UserStat from(User user) { diff --git a/src/main/java/com/wakeUpTogetUp/togetUp/api/users/fcmToken/FcmToken.java b/src/main/java/com/wakeUpTogetUp/togetUp/api/users/fcmToken/FcmToken.java index 9092e932..69af7c1e 100644 --- a/src/main/java/com/wakeUpTogetUp/togetUp/api/users/fcmToken/FcmToken.java +++ b/src/main/java/com/wakeUpTogetUp/togetUp/api/users/fcmToken/FcmToken.java @@ -1,6 +1,6 @@ package com.wakeUpTogetUp.togetUp.api.users.fcmToken; -import com.wakeUpTogetUp.togetUp.api.users.model.User; +import com.wakeUpTogetUp.togetUp.api.users.domain.User; import lombok.*; import org.hibernate.annotations.DynamicInsert;