From e1e9d56c779f306b06a0feca757d5946006058b6 Mon Sep 17 00:00:00 2001 From: parkjuyeong0312 Date: Tue, 12 Aug 2025 09:37:24 +0900 Subject: [PATCH 1/2] refactor : ProfileBackgroundColor service refactoring --- .../aloc/global/init/DevDataInitializer.java | 45 ++++++------- .../enums/ColorType.java | 23 +++++++ .../ProfileBackgroundColorService.java | 64 +++++++++++++------ 3 files changed, 92 insertions(+), 40 deletions(-) create mode 100644 src/main/java/com/aloc/aloc/profilebackgroundcolor/enums/ColorType.java diff --git a/src/main/java/com/aloc/aloc/global/init/DevDataInitializer.java b/src/main/java/com/aloc/aloc/global/init/DevDataInitializer.java index 4bb06200..3278f95a 100644 --- a/src/main/java/com/aloc/aloc/global/init/DevDataInitializer.java +++ b/src/main/java/com/aloc/aloc/global/init/DevDataInitializer.java @@ -1,6 +1,7 @@ package com.aloc.aloc.global.init; import com.aloc.aloc.profilebackgroundcolor.entity.ProfileBackgroundColor; +import com.aloc.aloc.profilebackgroundcolor.enums.ColorType; import com.aloc.aloc.profilebackgroundcolor.repository.ProfileBackgroundColorRepository; import java.util.List; import lombok.RequiredArgsConstructor; @@ -29,41 +30,41 @@ private void initProfileBackgroundColor() { List colors = List.of( - new ProfileBackgroundColor("Red", "#FF5A5A", null, null, null, null, "common", null), - new ProfileBackgroundColor("Yellow", "#FFB800", null, null, null, null, "common", null), - new ProfileBackgroundColor("Orange", "#FF9635", null, null, null, null, "common", null), - new ProfileBackgroundColor("Green", "#2ADC0D", null, null, null, null, "common", null), + new ProfileBackgroundColor("Red", "#FF5A5A", null, null, null, null, ColorType.COMMON.getValue(), null), + new ProfileBackgroundColor("Yellow", "#FFB800", null, null, null, null, ColorType.COMMON.getValue(), null), + new ProfileBackgroundColor("Orange", "#FF9635", null, null, null, null, ColorType.COMMON.getValue(), null), + new ProfileBackgroundColor("Green", "#2ADC0D", null, null, null, null, ColorType.COMMON.getValue(), null), new ProfileBackgroundColor( - "Emerald", "#00DC9A", null, null, null, null, "common", null), + "Emerald", "#00DC9A", null, null, null, null, ColorType.COMMON.getValue(), null), new ProfileBackgroundColor( - "Skyblue", "#00C2FF", null, null, null, null, "common", null), - new ProfileBackgroundColor("Blue", "#408CFF", null, null, null, null, "common", null), - new ProfileBackgroundColor("Indigo", "#4440FF", null, null, null, null, "common", null), - new ProfileBackgroundColor("Purple", "#BA63FF", null, null, null, null, "common", null), - new ProfileBackgroundColor("Pink", "#FF5AB3", null, null, null, null, "common", null), + "Skyblue", "#00C2FF", null, null, null, null, ColorType.COMMON.getValue(), null), + new ProfileBackgroundColor("Blue", "#408CFF", null, null, null, null, ColorType.COMMON.getValue(), null), + new ProfileBackgroundColor("Indigo", "#4440FF", null, null, null, null, ColorType.COMMON.getValue(), null), + new ProfileBackgroundColor("Purple", "#BA63FF", null, null, null, null, ColorType.COMMON.getValue(), null), + new ProfileBackgroundColor("Pink", "#FF5AB3", null, null, null, null, ColorType.COMMON.getValue(), null), new ProfileBackgroundColor( - "PurpleRed", "#BA63FF", "#FF5A5A", null, null, null, "rare", 135), + "PurpleRed", "#BA63FF", "#FF5A5A", null, null, null, ColorType.RARE.getValue(), 135), new ProfileBackgroundColor( - "BluePurple", "#408CFF", "#BA63FF", null, null, null, "rare", 135), + "BluePurple", "#408CFF", "#BA63FF", null, null, null, ColorType.RARE.getValue(), 135), new ProfileBackgroundColor( - "IndigoPink", "#4440FF", "#FF5AB3", null, null, null, "rare", 135), + "IndigoPink", "#4440FF", "#FF5AB3", null, null, null, ColorType.RARE.getValue(), 135), new ProfileBackgroundColor( - "SkyblueIndigo", "#00C2FF", "#4440FF", null, null, null, "rare", 135), + "SkyblueIndigo", "#00C2FF", "#4440FF", null, null, null, ColorType.RARE.getValue(), 135), new ProfileBackgroundColor( - "GreenSkyblue", "#2ADC0D", "#00C2FF", null, null, null, "rare", 135), + "GreenSkyblue", "#2ADC0D", "#00C2FF", null, null, null, ColorType.RARE.getValue(), 135), new ProfileBackgroundColor( - "RedYellow", "#FF5A5A", "#FFB800", null, null, null, "rare", 135), + "RedYellow", "#FF5A5A", "#FFB800", null, null, null, ColorType.RARE.getValue(), 135), new ProfileBackgroundColor( - "BeautifulYPB", "#FFB800", "#FF69F0", "#408CFF", "", "", "special", 135), + "BeautifulYPB", "#FFB800", "#FF69F0", "#408CFF", "", "", ColorType.SPECIAL.getValue(), 135), new ProfileBackgroundColor( - "BeautifulBPR", "#408CFF", "#E95FFF", "#FF5A5A", "", "", "special", 135), + "BeautifulBPR", "#408CFF", "#E95FFF", "#FF5A5A", "", "", ColorType.SPECIAL.getValue(), 135), new ProfileBackgroundColor( - "GreenTea", "#CED690", "#49985D", null, null, null, "rare", 180), + "GreenTea", "#CED690", "#49985D", null, null, null, ColorType.RARE.getValue(), 180), new ProfileBackgroundColor( - "TequilaSunrise", "#F2F1AF", "#EF3529", null, null, null, "rare", 180), - new ProfileBackgroundColor("10AM", "#759CFF", "#B2E3FF", null, null, null, "rare", 180), + "TequilaSunrise", "#F2F1AF", "#EF3529", null, null, null, ColorType.RARE.getValue(), 180), + new ProfileBackgroundColor("10AM", "#759CFF", "#B2E3FF", null, null, null, ColorType.RARE.getValue(), 180), new ProfileBackgroundColor( - "10PM", "#111579", "#0071C3", null, null, null, "rare", 180)); + "10PM", "#111579", "#0071C3", null, null, null, ColorType.RARE.getValue(), 180)); profileBackgroundColorRepository.saveAll(colors); log.info("✅ profile_background_color 초기 데이터 삽입 완료: {}개", colors.size()); diff --git a/src/main/java/com/aloc/aloc/profilebackgroundcolor/enums/ColorType.java b/src/main/java/com/aloc/aloc/profilebackgroundcolor/enums/ColorType.java new file mode 100644 index 00000000..746b5801 --- /dev/null +++ b/src/main/java/com/aloc/aloc/profilebackgroundcolor/enums/ColorType.java @@ -0,0 +1,23 @@ +package com.aloc.aloc.profilebackgroundcolor.enums; + +/** + * 프로필 배경 색상 타입을 정의하는 Enum + * - COMMON: 일반 색상 (85% 확률) + * - RARE: 희귀 색상 (10% 확률, 86~95%) + * - SPECIAL: 특별 색상 (5% 확률, 96~100%) + */ +public enum ColorType { + COMMON("common"), + RARE("rare"), + SPECIAL("special"); + + private final String value; + + ColorType(String value) { + this.value = value; + } + + public String getValue() { + return value; + } +} \ No newline at end of file diff --git a/src/main/java/com/aloc/aloc/profilebackgroundcolor/service/ProfileBackgroundColorService.java b/src/main/java/com/aloc/aloc/profilebackgroundcolor/service/ProfileBackgroundColorService.java index 7c84c3f0..af1527fc 100644 --- a/src/main/java/com/aloc/aloc/profilebackgroundcolor/service/ProfileBackgroundColorService.java +++ b/src/main/java/com/aloc/aloc/profilebackgroundcolor/service/ProfileBackgroundColorService.java @@ -2,13 +2,14 @@ import com.aloc.aloc.profilebackgroundcolor.dto.response.ColorResponseDto; import com.aloc.aloc.profilebackgroundcolor.entity.ProfileBackgroundColor; +import com.aloc.aloc.profilebackgroundcolor.enums.ColorType; import com.aloc.aloc.profilebackgroundcolor.repository.ProfileBackgroundColorRepository; -import com.aloc.aloc.user.repository.UserRepository; import java.util.List; import java.util.Random; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; + // 프로필 배경 색상 서비스 // 1. getColorByName 이름으로 조회 // 2. pickColor 랜덤 컬러 선택 @@ -17,38 +18,65 @@ @RequiredArgsConstructor public class ProfileBackgroundColorService { private final ProfileBackgroundColorRepository profileBackgroundColorRepository; - private final UserRepository userRepository; + private final Random randomNumberGenerator = new Random(); + + // 랜덤 컬러 선택 확률 경계 설정 + private static final int MAX_PROBABILITY = 100; + private static final int COMMON_THRESHOLD = 85; + private static final int RARE_THRESHOLD = 95; - private static final int COLOR_CHANGE_MONEY = 100; // 1. getColorByName 이름으로 조회 // input : 컬러 이름 // output : 컬러 이름에 해당하는 컬러 - public ProfileBackgroundColor getColorByName(String name) { + public ProfileBackgroundColor getColorByName(String colorName) { return profileBackgroundColorRepository - .findByName(name) - .orElseThrow(() -> new IllegalArgumentException("해당 컬러가 없습니다. " + name)); + .findByName(colorName) + .orElseThrow(() -> new IllegalArgumentException("해당 컬러가 없습니다. " + colorName)); } // 2. pickColor 랜덤 컬러 선택 // input : 없음 // output : 랜덤 컬러 이름 public String pickColor() { - Random random = new Random(); - int draw = random.nextInt(100) + 1; - - List profileBackgroundColorList; - if (draw <= 85) { - profileBackgroundColorList = profileBackgroundColorRepository.findByType("common"); - } else if (draw <= 95) { - profileBackgroundColorList = profileBackgroundColorRepository.findByType("rare"); + ColorType selectedType = determineColorTypeByProbability(); + List colorsOfSelectedType = getColorsByType(selectedType); + return selectRandomColor(colorsOfSelectedType); + } + + // 확률 기반으로 ColorType 결정 + // input : 없음 + // output : 선택된 ColorType + private ColorType determineColorTypeByProbability() { + int randomNumber = randomNumberGenerator.nextInt(MAX_PROBABILITY) + 1; + + if (randomNumber <= COMMON_THRESHOLD) { + return ColorType.COMMON; + } else if (randomNumber <= RARE_THRESHOLD) { + return ColorType.RARE; } else { - profileBackgroundColorList = profileBackgroundColorRepository.findByType("special"); + return ColorType.SPECIAL; } + } + + // 특정 타입의 색상 리스트 조회 + // input : ColorType + // output : 해당 타입의 색상 리스트 + private List getColorsByType(ColorType colorType) { + List colors = profileBackgroundColorRepository.findByType(colorType.getValue()); + + if (colors.isEmpty()) { + throw new IllegalStateException("선택된 타입에 해당하는 색상이 없습니다: " + colorType.getValue()); + } + + return colors; + } - return profileBackgroundColorList - .get(random.nextInt(profileBackgroundColorList.size())) - .getName(); + // 색상 리스트에서 랜덤하게 하나 선택 + // input : 색상 리스트 + // output : 선택된 색상 이름 + private String selectRandomColor(List colors) { + return colors.get(randomNumberGenerator.nextInt(colors.size())).getName(); } // 3. getAllColors 모든 컬러 조회 From 700acde362c6ceebcd6c7f2fa6cc1bc02733ea57 Mon Sep 17 00:00:00 2001 From: parkjuyeong0312 Date: Sun, 17 Aug 2025 03:38:45 +0900 Subject: [PATCH 2/2] =?UTF-8?q?refactor:=20ColorType=20enum=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80=20=EB=B0=8F=20=ED=99=95=EB=A5=A0=20=EA=B8=B0=EB=B0=98?= =?UTF-8?q?=20=EC=83=89=EC=83=81=20=EC=84=A0=ED=83=9D=20=EB=A1=9C=EC=A7=81?= =?UTF-8?q?=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../aloc/global/init/DevDataInitializer.java | 108 +++++++++++++++--- .../enums/ColorType.java | 32 +++--- .../ProfileBackgroundColorService.java | 11 +- 3 files changed, 110 insertions(+), 41 deletions(-) diff --git a/src/main/java/com/aloc/aloc/global/init/DevDataInitializer.java b/src/main/java/com/aloc/aloc/global/init/DevDataInitializer.java index 3278f95a..9e51b08f 100644 --- a/src/main/java/com/aloc/aloc/global/init/DevDataInitializer.java +++ b/src/main/java/com/aloc/aloc/global/init/DevDataInitializer.java @@ -30,39 +30,111 @@ private void initProfileBackgroundColor() { List colors = List.of( - new ProfileBackgroundColor("Red", "#FF5A5A", null, null, null, null, ColorType.COMMON.getValue(), null), - new ProfileBackgroundColor("Yellow", "#FFB800", null, null, null, null, ColorType.COMMON.getValue(), null), - new ProfileBackgroundColor("Orange", "#FF9635", null, null, null, null, ColorType.COMMON.getValue(), null), - new ProfileBackgroundColor("Green", "#2ADC0D", null, null, null, null, ColorType.COMMON.getValue(), null), + new ProfileBackgroundColor( + "Red", "#FF5A5A", null, null, null, null, ColorType.COMMON.getValue(), null), + new ProfileBackgroundColor( + "Yellow", "#FFB800", null, null, null, null, ColorType.COMMON.getValue(), null), + new ProfileBackgroundColor( + "Orange", "#FF9635", null, null, null, null, ColorType.COMMON.getValue(), null), + new ProfileBackgroundColor( + "Green", "#2ADC0D", null, null, null, null, ColorType.COMMON.getValue(), null), new ProfileBackgroundColor( "Emerald", "#00DC9A", null, null, null, null, ColorType.COMMON.getValue(), null), new ProfileBackgroundColor( "Skyblue", "#00C2FF", null, null, null, null, ColorType.COMMON.getValue(), null), - new ProfileBackgroundColor("Blue", "#408CFF", null, null, null, null, ColorType.COMMON.getValue(), null), - new ProfileBackgroundColor("Indigo", "#4440FF", null, null, null, null, ColorType.COMMON.getValue(), null), - new ProfileBackgroundColor("Purple", "#BA63FF", null, null, null, null, ColorType.COMMON.getValue(), null), - new ProfileBackgroundColor("Pink", "#FF5AB3", null, null, null, null, ColorType.COMMON.getValue(), null), new ProfileBackgroundColor( - "PurpleRed", "#BA63FF", "#FF5A5A", null, null, null, ColorType.RARE.getValue(), 135), + "Blue", "#408CFF", null, null, null, null, ColorType.COMMON.getValue(), null), + new ProfileBackgroundColor( + "Indigo", "#4440FF", null, null, null, null, ColorType.COMMON.getValue(), null), new ProfileBackgroundColor( - "BluePurple", "#408CFF", "#BA63FF", null, null, null, ColorType.RARE.getValue(), 135), + "Purple", "#BA63FF", null, null, null, null, ColorType.COMMON.getValue(), null), new ProfileBackgroundColor( - "IndigoPink", "#4440FF", "#FF5AB3", null, null, null, ColorType.RARE.getValue(), 135), + "Pink", "#FF5AB3", null, null, null, null, ColorType.COMMON.getValue(), null), new ProfileBackgroundColor( - "SkyblueIndigo", "#00C2FF", "#4440FF", null, null, null, ColorType.RARE.getValue(), 135), + "PurpleRed", + "#BA63FF", + "#FF5A5A", + null, + null, + null, + ColorType.RARE.getValue(), + 135), new ProfileBackgroundColor( - "GreenSkyblue", "#2ADC0D", "#00C2FF", null, null, null, ColorType.RARE.getValue(), 135), + "BluePurple", + "#408CFF", + "#BA63FF", + null, + null, + null, + ColorType.RARE.getValue(), + 135), new ProfileBackgroundColor( - "RedYellow", "#FF5A5A", "#FFB800", null, null, null, ColorType.RARE.getValue(), 135), + "IndigoPink", + "#4440FF", + "#FF5AB3", + null, + null, + null, + ColorType.RARE.getValue(), + 135), new ProfileBackgroundColor( - "BeautifulYPB", "#FFB800", "#FF69F0", "#408CFF", "", "", ColorType.SPECIAL.getValue(), 135), + "SkyblueIndigo", + "#00C2FF", + "#4440FF", + null, + null, + null, + ColorType.RARE.getValue(), + 135), new ProfileBackgroundColor( - "BeautifulBPR", "#408CFF", "#E95FFF", "#FF5A5A", "", "", ColorType.SPECIAL.getValue(), 135), + "GreenSkyblue", + "#2ADC0D", + "#00C2FF", + null, + null, + null, + ColorType.RARE.getValue(), + 135), + new ProfileBackgroundColor( + "RedYellow", + "#FF5A5A", + "#FFB800", + null, + null, + null, + ColorType.RARE.getValue(), + 135), + new ProfileBackgroundColor( + "BeautifulYPB", + "#FFB800", + "#FF69F0", + "#408CFF", + "", + "", + ColorType.SPECIAL.getValue(), + 135), + new ProfileBackgroundColor( + "BeautifulBPR", + "#408CFF", + "#E95FFF", + "#FF5A5A", + "", + "", + ColorType.SPECIAL.getValue(), + 135), new ProfileBackgroundColor( "GreenTea", "#CED690", "#49985D", null, null, null, ColorType.RARE.getValue(), 180), new ProfileBackgroundColor( - "TequilaSunrise", "#F2F1AF", "#EF3529", null, null, null, ColorType.RARE.getValue(), 180), - new ProfileBackgroundColor("10AM", "#759CFF", "#B2E3FF", null, null, null, ColorType.RARE.getValue(), 180), + "TequilaSunrise", + "#F2F1AF", + "#EF3529", + null, + null, + null, + ColorType.RARE.getValue(), + 180), + new ProfileBackgroundColor( + "10AM", "#759CFF", "#B2E3FF", null, null, null, ColorType.RARE.getValue(), 180), new ProfileBackgroundColor( "10PM", "#111579", "#0071C3", null, null, null, ColorType.RARE.getValue(), 180)); diff --git a/src/main/java/com/aloc/aloc/profilebackgroundcolor/enums/ColorType.java b/src/main/java/com/aloc/aloc/profilebackgroundcolor/enums/ColorType.java index 746b5801..565ed42d 100644 --- a/src/main/java/com/aloc/aloc/profilebackgroundcolor/enums/ColorType.java +++ b/src/main/java/com/aloc/aloc/profilebackgroundcolor/enums/ColorType.java @@ -1,23 +1,21 @@ package com.aloc.aloc.profilebackgroundcolor.enums; /** - * 프로필 배경 색상 타입을 정의하는 Enum - * - COMMON: 일반 색상 (85% 확률) - * - RARE: 희귀 색상 (10% 확률, 86~95%) - * - SPECIAL: 특별 색상 (5% 확률, 96~100%) + * 프로필 배경 색상 타입을 정의하는 Enum - COMMON: 일반 색상 (85% 확률) - RARE: 희귀 색상 (10% 확률, 86~95%) - SPECIAL: 특별 색상 + * (5% 확률, 96~100%) */ public enum ColorType { - COMMON("common"), - RARE("rare"), - SPECIAL("special"); - - private final String value; - - ColorType(String value) { - this.value = value; - } - - public String getValue() { - return value; - } + COMMON("common"), + RARE("rare"), + SPECIAL("special"); + + private final String value; + + ColorType(String value) { + this.value = value; + } + + public String getValue() { + return value; + } } \ No newline at end of file diff --git a/src/main/java/com/aloc/aloc/profilebackgroundcolor/service/ProfileBackgroundColorService.java b/src/main/java/com/aloc/aloc/profilebackgroundcolor/service/ProfileBackgroundColorService.java index af1527fc..779d9d4b 100644 --- a/src/main/java/com/aloc/aloc/profilebackgroundcolor/service/ProfileBackgroundColorService.java +++ b/src/main/java/com/aloc/aloc/profilebackgroundcolor/service/ProfileBackgroundColorService.java @@ -9,7 +9,6 @@ import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; - // 프로필 배경 색상 서비스 // 1. getColorByName 이름으로 조회 // 2. pickColor 랜덤 컬러 선택 @@ -25,7 +24,6 @@ public class ProfileBackgroundColorService { private static final int COMMON_THRESHOLD = 85; private static final int RARE_THRESHOLD = 95; - // 1. getColorByName 이름으로 조회 // input : 컬러 이름 // output : 컬러 이름에 해당하는 컬러 @@ -49,7 +47,7 @@ public String pickColor() { // output : 선택된 ColorType private ColorType determineColorTypeByProbability() { int randomNumber = randomNumberGenerator.nextInt(MAX_PROBABILITY) + 1; - + if (randomNumber <= COMMON_THRESHOLD) { return ColorType.COMMON; } else if (randomNumber <= RARE_THRESHOLD) { @@ -63,12 +61,13 @@ private ColorType determineColorTypeByProbability() { // input : ColorType // output : 해당 타입의 색상 리스트 private List getColorsByType(ColorType colorType) { - List colors = profileBackgroundColorRepository.findByType(colorType.getValue()); - + List colors = + profileBackgroundColorRepository.findByType(colorType.getValue()); + if (colors.isEmpty()) { throw new IllegalStateException("선택된 타입에 해당하는 색상이 없습니다: " + colorType.getValue()); } - + return colors; }