Skip to content

Commit

Permalink
[FIX] 아바타 테마가 존재하지 않아 발생하는 에러 예외 처리 (#156)
Browse files Browse the repository at this point in the history
* [refactor] 네이밍 변경: SpeechStrategy -> MakeSpeechStrategy

* [fix] AvatarTheme 클래스에 정의되지 않은 테마로 요청했을때 발생하는 예외 방지

추후 아바타가 추가될때마다 AvatarTheme에 추가하고 서버를 재배포하는 것이 번거롭다고 생각하여 특정 조건으로 대사를 가져오는 아바타 테마만 AvatarTheme에 구현했습니다.

해당 이름의 아바타 테마가 존재하는지 확인하고, 존재하지 않으면 랜덤 대사를 가져오고, 존재하면 해당 테마에 맞는 방식으로 대사를 가져오도록 구현했습니다.

* [refactor] 네이밍 변경: 아바타 대사 조회 메서드를 선택한다는 의미를 강조
  • Loading branch information
05AM authored Aug 24, 2024
1 parent 9f47e4f commit 2bcc6c4
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 23 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.wakeUpTogetUp.togetUp.api.avatar.application;

import com.wakeUpTogetUp.togetUp.api.avatar.application.strategy.SpeechStrategy;
import com.wakeUpTogetUp.togetUp.api.avatar.application.strategy.MakeSpeechStrategy;
import com.wakeUpTogetUp.togetUp.api.avatar.domain.Avatar;
import com.wakeUpTogetUp.togetUp.api.avatar.domain.AvatarSpeech;
import com.wakeUpTogetUp.togetUp.api.avatar.domain.AvatarSpeechCondition;
Expand Down Expand Up @@ -30,28 +30,31 @@ public class AvatarSpeechProvider {
private final AvatarRepository avatarRepository;
private final AvatarSpeechRepository avatarSpeechRepository;

private final SpeechStrategyFactory speechStrategyFactory;
private final MakeSpeechStrategyFactory makeSpeechStrategyFactory;

private final Random random = new Random();

public AvatarSpeechResponse getUserAvatarSpeech(int userId, int avatarId) {
userAvatarValidationService.validateUserAvatarActive(userId, avatarId);
String speech = makeSpeechByCondition(getSpeechByAvatar(avatarId));
String speech = makeSpeechByCondition(selectSpeechByAvatar(avatarId));

return EntityDtoMapper.INSTANCE.toAvatarSpeechResponse(speech);
}

private String makeSpeechByCondition(AvatarSpeech avatarSpeech) {
SpeechStrategy strategy = speechStrategyFactory.getStrategy(avatarSpeech.getCondition());
MakeSpeechStrategy strategy = makeSpeechStrategyFactory.getStrategy(avatarSpeech.getCondition());
return strategy.makeSpeech(avatarSpeech);
}

private AvatarSpeech getSpeechByAvatar(int avatarId) {
private AvatarSpeech selectSpeechByAvatar(int avatarId) {
Avatar avatar = avatarRepository.findById(avatarId)
.orElseThrow(() -> new BaseException(Status.AVATAR_NOT_FOUND));
AvatarTheme avatarTheme = AvatarTheme.valueOf(avatar.getTheme());

switch (avatarTheme) {
if (!AvatarTheme.isExist(avatar.getTheme())) {
return getRandomOneByAvatar(avatarId);
}

switch (AvatarTheme.valueOf(avatar.getTheme())) {
case ASTRONAUT_BEAR:
LocalDate today = LocalDate.now();
return getByAvatarAndLunarCondition(avatarId, today);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.wakeUpTogetUp.togetUp.api.avatar.application;

import com.wakeUpTogetUp.togetUp.api.avatar.application.strategy.BasicSpeechStrategy;
import com.wakeUpTogetUp.togetUp.api.avatar.application.strategy.FoodSpeechStrategy;
import com.wakeUpTogetUp.togetUp.api.avatar.application.strategy.SpeechStrategy;
import com.wakeUpTogetUp.togetUp.api.avatar.application.strategy.TimeSpeechStrategy;
import com.wakeUpTogetUp.togetUp.api.avatar.application.strategy.BasicMakeSpeechStrategy;
import com.wakeUpTogetUp.togetUp.api.avatar.application.strategy.FoodMakeSpeechStrategy;
import com.wakeUpTogetUp.togetUp.api.avatar.application.strategy.MakeSpeechStrategy;
import com.wakeUpTogetUp.togetUp.api.avatar.application.strategy.TimeMakeSpeechStrategy;
import com.wakeUpTogetUp.togetUp.api.avatar.domain.AvatarSpeechCondition;
import java.util.EnumMap;
import java.util.Map;
Expand All @@ -13,11 +13,11 @@

@Component
@RequiredArgsConstructor
public class SpeechStrategyFactory {
private static final Map<AvatarSpeechCondition, SpeechStrategy> strategies = new EnumMap<>(AvatarSpeechCondition.class);
private final BasicSpeechStrategy basicSpeechStrategy;
private final FoodSpeechStrategy foodSpeechStrategy;
private final TimeSpeechStrategy timeSpeechStrategy;
public class MakeSpeechStrategyFactory {
private static final Map<AvatarSpeechCondition, MakeSpeechStrategy> strategies = new EnumMap<>(AvatarSpeechCondition.class);
private final BasicMakeSpeechStrategy basicSpeechStrategy;
private final FoodMakeSpeechStrategy foodSpeechStrategy;
private final TimeMakeSpeechStrategy timeSpeechStrategy;

@PostConstruct
public void init() {
Expand All @@ -27,7 +27,7 @@ public void init() {
strategies.put(AvatarSpeechCondition.TIME, timeSpeechStrategy);
}

public SpeechStrategy getStrategy(AvatarSpeechCondition condition) {
return strategies.getOrDefault(condition, new BasicSpeechStrategy());
public MakeSpeechStrategy getStrategy(AvatarSpeechCondition condition) {
return strategies.getOrDefault(condition, new BasicMakeSpeechStrategy());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import org.springframework.stereotype.Component;

@Component
public class BasicSpeechStrategy implements SpeechStrategy{
public class BasicMakeSpeechStrategy implements MakeSpeechStrategy {

@Override
public String makeSpeech(AvatarSpeech avatarSpeech) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

@Component
@RequiredArgsConstructor
public class FoodSpeechStrategy implements SpeechStrategy{
public class FoodMakeSpeechStrategy implements MakeSpeechStrategy {

private final RandomFoodProvider randomFoodProvider;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.wakeUpTogetUp.togetUp.api.avatar.domain.AvatarSpeech;

public interface SpeechStrategy {
public interface MakeSpeechStrategy {

String makeSpeech(AvatarSpeech avatarSpeech);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import org.springframework.stereotype.Component;

@Component
public class TimeSpeechStrategy implements SpeechStrategy{
public class TimeMakeSpeechStrategy implements MakeSpeechStrategy {

@Override
public String makeSpeech(AvatarSpeech avatarSpeech) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.wakeUpTogetUp.togetUp.api.avatar.domain;

import java.util.Arrays;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
Expand All @@ -8,5 +9,11 @@
@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
public enum AvatarTheme {

ASTRONAUT_BEAR,
ASTRONAUT_BEAR
;

public static boolean isExist(String name) {
return Arrays.stream(values())
.anyMatch(v -> v.name().equals(name));
}
}

0 comments on commit 2bcc6c4

Please sign in to comment.