Skip to content

Commit

Permalink
[REFACTOR] user 레벨 업 및 기타 작업 리팩터링 (#134)
Browse files Browse the repository at this point in the history
## ☀️ 작업 사항

- user progress 기능 user 도메인 내부로 이동
- avatar unlock 기능 명시적으로 리팩터링
- user에서 레벨 업 관련 계산 로직 분리
- user에서 exp percentage를 계산할 수 있도록 필드 및 메서드 추가
- user 애그리거트 리패키징
  - domian 패키지 생성 및 내부에 도메인 계층의 클래스 리패키징

## ☀️ 관련 이슈

related: #133 

## ☀️ 참고사항

논의된 사항이지만 패키징 구조에 대해 이견이 있다면 자유롭게 말씀해주시면 감사하겠습니다!
  • Loading branch information
05AM authored May 14, 2024
2 parents 6e48892 + 092d4b7 commit d719233
Show file tree
Hide file tree
Showing 27 changed files with 206 additions and 134 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -27,7 +27,7 @@ public class AuthService {
private final List<SocialLoginService> loginServices;
private final JwtService jwtService;
private final UserService userService;
private final UserAvatarService userAvatarService;
private final UserAvatarQueryService userAvatarQueryService;
private final AppleLoginServiceImpl appleLoginService;

@Transactional
Expand All @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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<UserAvatarResponse> findUserAvatarList(int userId) {
List<Avatar> avatarList = avatarRepository.findAll();
List<UserAvatar> 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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<UserAvatarResponse> findUserAvatarList(int userId) {
List<Avatar> avatarList = avatarRepository.findAll();
List<UserAvatar> userAvatarList = userAvatarRepository.findAllByUser_Id(userId);

return EntityDtoMapper.INSTANCE.toUserAvatarDataList(avatarList, userAvatarList);
}

@Transactional
public boolean unlockAvatarIfAvailableExist(User user) {
Optional<Avatar> 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)
Expand All @@ -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));
Expand All @@ -65,10 +42,8 @@ public void setUserDefaultAvatar(User user) {
changeUserAvatar(user.getId(), DEFAULT_AVATAR_ID);
}

@Transactional
public void changeUserAvatar(int userId, int avatarId) {
List<UserAvatar> userAvatarList =
userAvatarRepository.findAllByUser_Id(userId);
List<UserAvatar> userAvatarList = userAvatarRepository.findAllByUser_Id(userId);

userAvatarList.stream()
.filter(i -> i.getAvatar().getId() == avatarId)
Expand All @@ -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<Avatar> 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);
}
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
20 changes: 11 additions & 9 deletions src/main/java/com/wakeUpTogetUp/togetUp/api/dev/DevController.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -68,13 +68,15 @@ public BaseResponse<MissionCompleteRes> processMissionCompletion(
@Parameter(hidden = true) @AuthUser Integer userId,
@RequestBody @Valid MissionCompleteReq missionCompleteReq
) {
User user = new User(userId,
"1234",
"이예원",
"[email protected]",
LoginType.KAKAO,
30,
3);
User user = User.builder()
.id(userId)
.socialId("1234")
.name("이예원")
.email("[email protected]")
.loginType(LoginType.KAKAO)
.expPoint(30)
.level(3)
.build();

MissionCompleteRes response = MissionCompleteRes.builder()
.userStat(UserStat.from(user))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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<CustomAnalysisEntity> getMissionResult(MissionType type, String object, MultipartFile missionImage) {
VisionAnalysisResult result = visionServiceFactory
Expand All @@ -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();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Loading

0 comments on commit d719233

Please sign in to comment.