Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,18 @@
import com.example.cp_main_be.domain.avatar.avatar.domain.AvatarMaster;
import com.example.cp_main_be.domain.avatar.avatar.domain.repository.AvatarMasterRepository;
import com.example.cp_main_be.domain.avatar.avatar.domain.repository.AvatarRepository;
import com.example.cp_main_be.domain.garden.garden.domain.Garden;
import com.example.cp_main_be.domain.garden.garden.domain.repository.GardenRepository;
import com.example.cp_main_be.domain.member.notification.domain.NotificationType;
import com.example.cp_main_be.domain.member.notification.service.NotificationService;
import com.example.cp_main_be.domain.member.user.domain.User;
import com.example.cp_main_be.domain.member.user.domain.repository.UserRepository;
import com.example.cp_main_be.domain.mission.wishTree.WishTree;
import com.example.cp_main_be.domain.mission.wishTree.WishTreeRepository;
import com.example.cp_main_be.domain.mission.wishTree.WishTreeStage;
import com.example.cp_main_be.global.common.CustomApiException;
import com.example.cp_main_be.global.common.ErrorCode;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand All @@ -25,6 +31,8 @@ public class AvatarService {
private final NotificationService notificationService;

private static final Long AI_AVATAR_MASTER_ID = 9999L;
private final WishTreeRepository wishTreeRepository;
private final GardenRepository gardenRepository;

// [수정] 새로운 아바타 생성 로직 구현
public void createAvatar(Long userId, String nickname, String imageUrl, Long masterId) {
Expand Down Expand Up @@ -57,6 +65,23 @@ public void createAvatar(Long userId, String nickname, String imageUrl, Long mas
.build();

avatarRepository.save(newAvatar);

WishTree wishTree =
wishTreeRepository
.findByUserId(userId)
.orElseThrow(() -> new CustomApiException(ErrorCode.NOT_FOUND));

WishTreeStage stage = wishTree.getStage();
int maxGardens = stage.getMaxGardens();

List<Garden> userGardens = user.getGardens();
if (userGardens.size() < maxGardens) {
Garden newGarden =
Garden.builder().user(user).slotNumber(userGardens.size() + 1).avatar(newAvatar).build();
gardenRepository.save(newGarden);
} else {
throw new CustomApiException(ErrorCode.MAX_GARDENS_REACHED);
}
}

@Transactional(readOnly = true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@
@RequiredArgsConstructor
public enum WishTreeStage {
// 다음 단계로 가기 위해 필요한 '누적' 포인트와 이름 정의
SPROUT(1000, "새싹"),
FLOWER(2150, "꽃"), // 1000 + 1150
FRUIT(3450, "열매"), // 2150 + 1300
TREE(4900, "나무"), // 3450 + 1450
FINAL(Integer.MAX_VALUE, "최종 나무"); // 더 이상 성장 안함
SPROUT(1000, "새싹", 1),
FLOWER(2150, "꽃", 2), // 1000 + 1150
FRUIT(3450, "열매", 3), // 2150 + 1300
TREE(4900, "나무", 4), // 3450 + 1450
FINAL(Integer.MAX_VALUE, "최종 나무", 5); // 더 이상 성장 안함

private final int requiredPointsForNextStage;
private final String koreanName;
private final int maxGardens;

public static WishTreeStage getStageForPoints(int points) {
if (points < SPROUT.requiredPointsForNextStage) return SPROUT;
Expand All @@ -23,4 +24,8 @@ public static WishTreeStage getStageForPoints(int points) {
if (points < TREE.requiredPointsForNextStage) return TREE;
return FINAL;
}

public int getMaxGardens(WishTreeStage stage) {
return stage.maxGardens;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ public enum ErrorCode {
INVALID_TOKEN(HttpStatus.BAD_REQUEST, "E-50004", "부적절한 토큰입니다."),
FILE_SIZE_EXCEEDED(HttpStatus.BAD_REQUEST, "E-50005", "파일 크기 제한을 넘었습니다."),
NOT_FOUND(HttpStatus.NOT_FOUND, "E-50006", "Resource를 찾을 수 없습니다."),
UPLOAD_FAILED(HttpStatus.EXPECTATION_FAILED, "E-50007", "파일 업로드에 실패했습니다.");
UPLOAD_FAILED(HttpStatus.EXPECTATION_FAILED, "E-50007", "파일 업로드에 실패했습니다."),
MAX_GARDENS_REACHED(HttpStatus.BAD_REQUEST, "E-50008", "현재 레벨의 최대 정원수에 도달했습니다.");

private final HttpStatus status;
private final String code;
Expand Down