Skip to content

Commit 8d44971

Browse files
committed
refactor : ArtistType -> ENUM 으로 변경
1 parent 7f47452 commit 8d44971

9 files changed

Lines changed: 59 additions & 35 deletions

File tree

src/main/java/com/back/web7_9_codecrete_be/domain/artists/controller/ArtistsController.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public RsData<Integer> enrich(
4343
public RsData<Void> create(
4444
@RequestBody CreateRequest reqBody
4545
) {
46-
artistService.createArtist(reqBody.artistName(), reqBody.artistGroup(), reqBody.artistGroup(), reqBody.genreName());
46+
artistService.createArtist(reqBody.artistName(), reqBody.artistGroup(), reqBody.artistType(), reqBody.genreName());
4747
return RsData.success("아티스트 생성이 완료되었습니다.", null);
4848
}
4949

src/main/java/com/back/web7_9_codecrete_be/domain/artists/dto/request/CreateRequest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.back.web7_9_codecrete_be.domain.artists.dto.request;
22

3+
import com.back.web7_9_codecrete_be.domain.artists.entity.ArtistType;
34
import jakarta.validation.constraints.NotNull;
45
import jakarta.validation.constraints.Size;
56

@@ -12,7 +13,7 @@ public record CreateRequest(
1213
String artistGroup,
1314

1415
@NotNull(message = "아티스트 타입은 필수로 입력해야합니다(SOLO or GROUP)")
15-
String artistType,
16+
ArtistType artistType,
1617

1718
@NotNull(message = "장르는 필수로 입력해야합니다.")
1819
@Size(max = 30, message = "장르는 30자를 넘길 수 없습니다.")

src/main/java/com/back/web7_9_codecrete_be/domain/artists/dto/request/UpdateRequest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.back.web7_9_codecrete_be.domain.artists.dto.request;
22

3+
import com.back.web7_9_codecrete_be.domain.artists.entity.ArtistType;
34
import jakarta.validation.constraints.Size;
45

56
public record UpdateRequest(
@@ -9,7 +10,7 @@ public record UpdateRequest(
910
@Size(max = 150, message = "아티스트 그룹 이름은 150자를 넘길 수 없습니다.")
1011
String artistGroup,
1112

12-
String artistType,
13+
ArtistType artistType,
1314

1415
@Size(max = 30, message = "장르 이름은 30자를 넘길 수 없습니다.")
1516
String genreName

src/main/java/com/back/web7_9_codecrete_be/domain/artists/dto/response/ArtistDetailResponse.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
package com.back.web7_9_codecrete_be.domain.artists.dto.response;
22

3+
import com.back.web7_9_codecrete_be.domain.artists.entity.ArtistType;
4+
35
import java.util.List;
46

57
public record ArtistDetailResponse(
68
String artistName,
79
String artistGroup,
8-
String artistType,
10+
ArtistType artistType,
911
String profileImageUrl,
1012
long likeCount,
1113
int totalAlbums,

src/main/java/com/back/web7_9_codecrete_be/domain/artists/entity/Artist.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@ public class Artist {
2424
@Column(name = "artist_group")
2525
private String artistGroup;
2626

27+
@Enumerated(EnumType.STRING)
2728
@Column(name = "artist_type")
28-
private String artistType;
29+
private ArtistType artistType;
2930

3031
@ManyToOne(fetch = FetchType.LAZY)
3132
private Genre genre;
@@ -36,22 +37,22 @@ public class Artist {
3637
@Column(name = "name_ko", length = 200)
3738
private String nameKo;
3839

39-
public Artist(String spotifyArtistId, String artistName, String artistGroup, String artistType, Genre genre) {
40+
public Artist(String spotifyArtistId, String artistName, String artistGroup, ArtistType artistType, Genre genre) {
4041
this.spotifyArtistId = spotifyArtistId;
4142
this.artistName = artistName;
4243
this.artistGroup = artistGroup; // 옵션 B: seed에서는 null
4344
this.artistType = artistType; // 옵션 B: seed에서는 "SINGER"
4445
this.genre = genre;
4546
}
4647

47-
public Artist(String artistName, String artistGroup, String artistType, Genre genre) {
48+
public Artist(String artistName, String artistGroup, ArtistType artistType, Genre genre) {
4849
this.artistName = artistName;
4950
this.artistGroup = artistGroup;
5051
this.artistType = artistType;
5152
this.genre = genre;
5253
}
5354

54-
public void updateProfile(String nameKo, String artistGroup, String artistType) {
55+
public void updateProfile(String nameKo, String artistGroup, ArtistType artistType) {
5556
this.nameKo = nameKo;
5657
this.artistGroup = artistGroup; // nullable
5758
this.artistType = artistType; // "SOLO" / "GROUP"
@@ -65,7 +66,7 @@ public void changeGroup(String group) {
6566
this.artistGroup = group;
6667
}
6768

68-
public void changeType(String type) {
69+
public void changeType(ArtistType type) {
6970
this.artistType = type;
7071
}
7172

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.back.web7_9_codecrete_be.domain.artists.entity;
2+
3+
public enum ArtistType {
4+
SOLO,
5+
GROUP
6+
}

src/main/java/com/back/web7_9_codecrete_be/domain/artists/service/ArtistEnrichService.java

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.back.web7_9_codecrete_be.domain.artists.service;
22

33
import com.back.web7_9_codecrete_be.domain.artists.entity.Artist;
4+
import com.back.web7_9_codecrete_be.domain.artists.entity.ArtistType;
45
import com.back.web7_9_codecrete_be.domain.artists.repository.ArtistRepository;
56
import com.back.web7_9_codecrete_be.global.musicbrainz.MusicBrainzClient;
67
import com.back.web7_9_codecrete_be.global.wikidata.WikidataClient;
@@ -95,13 +96,28 @@ protected void enrichSingleArtist(Artist artist) {
9596
}
9697

9798
// 기존 artistType이 있으면 유지, 없으면 가져온 값 사용
98-
String artistType = result.artistType != null ? result.artistType : artist.getArtistType();
99+
String artistTypeStr = result.artistType != null ? result.artistType :
100+
(artist.getArtistType() != null ? artist.getArtistType().name() : null);
101+
102+
// String을 ArtistType enum으로 변환
103+
ArtistType artistType;
104+
if (artistTypeStr != null) {
105+
try {
106+
artistType = ArtistType.valueOf(artistTypeStr);
107+
} catch (IllegalArgumentException e) {
108+
log.warn("잘못된 artistType 값: {}, 기본값 SOLO 사용", artistTypeStr);
109+
artistType = ArtistType.SOLO;
110+
}
111+
} else {
112+
// 기존 값이 없고 새 값도 없으면 기본값 사용
113+
artistType = artist.getArtistType() != null ? artist.getArtistType() : ArtistType.SOLO;
114+
}
99115

100-
// 기존 row를 "보강"
116+
// 기존 row를 "보강"
101117
artist.updateProfile(result.nameKo, result.artistGroup, artistType);
102118
// 명시적으로 save하여 변경사항을 DB에 즉시 반영
103119
artistRepository.save(artist);
104-
log.info("Enrich 성공: artistId={}, name={}, nameKo={}, group={}, type={}, source={}",
120+
log.info("Enrich 성공: artistId={}, name={}, nameKo={}, group={}, type={}, source={}",
105121
artist.getId(), artist.getArtistName(), result.nameKo,
106122
result.artistGroup, artistType, result.source);
107123
}

src/main/java/com/back/web7_9_codecrete_be/domain/artists/service/ArtistService.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import com.back.web7_9_codecrete_be.domain.artists.dto.response.ArtistListResponse;
55
import com.back.web7_9_codecrete_be.domain.artists.dto.response.ArtistDetailResponse;
66
import com.back.web7_9_codecrete_be.domain.artists.entity.Artist;
7+
import com.back.web7_9_codecrete_be.domain.artists.entity.ArtistType;
78
import com.back.web7_9_codecrete_be.domain.artists.entity.Genre;
89
import com.back.web7_9_codecrete_be.domain.artists.repository.ArtistRepository;
910
import com.back.web7_9_codecrete_be.domain.artists.repository.ArtistLikeRepository;
@@ -30,7 +31,7 @@ public int setArtist() {
3031
}
3132

3233
@Transactional
33-
public Artist createArtist(String artistName, String artistGroup, String artistType, String genreName) {
34+
public Artist createArtist(String artistName, String artistGroup, ArtistType artistType, String genreName) {
3435
Genre genre = genreService.findByGenreName(genreName);
3536
if(artistRepository.existsByArtistName(artistName) || artistRepository.existsByNameKo(artistName)) {
3637
throw new BusinessException(ArtistErrorCode.ARTIST_ALREADY_EXISTS);
@@ -85,8 +86,8 @@ public void updateArtist(Long id, UpdateRequest req) {
8586
changed = true;
8687
}
8788

88-
if (req.artistType() != null && !req.artistType().isBlank()) {
89-
artist.changeType(req.artistType().trim());
89+
if (req.artistType() != null) {
90+
artist.changeType(req.artistType());
9091
changed = true;
9192
}
9293

src/main/java/com/back/web7_9_codecrete_be/domain/artists/service/SpotifyService.java

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import com.back.web7_9_codecrete_be.domain.artists.dto.response.ArtistDetailResponse;
55
import com.back.web7_9_codecrete_be.domain.artists.dto.response.RelatedArtistResponse;
66
import com.back.web7_9_codecrete_be.domain.artists.dto.response.TopTrackResponse;
7+
import com.back.web7_9_codecrete_be.domain.artists.entity.Artist;
8+
import com.back.web7_9_codecrete_be.domain.artists.entity.ArtistType;
79
import com.back.web7_9_codecrete_be.domain.artists.entity.Genre;
810
import com.back.web7_9_codecrete_be.domain.artists.repository.ArtistRepository;
911
import com.back.web7_9_codecrete_be.domain.artists.repository.GenreRepository;
@@ -19,7 +21,6 @@
1921
import se.michaelthelin.spotify.enums.AlbumType;
2022
import se.michaelthelin.spotify.exceptions.detailed.NotFoundException;
2123
import se.michaelthelin.spotify.model_objects.specification.AlbumSimplified;
22-
import se.michaelthelin.spotify.model_objects.specification.Artist;
2324
import se.michaelthelin.spotify.model_objects.specification.Image;
2425
import se.michaelthelin.spotify.model_objects.specification.Paging;
2526
import se.michaelthelin.spotify.model_objects.specification.Track;
@@ -63,7 +64,7 @@ public int seedKoreanArtists300() {
6364
int offset = 0;
6465

6566
while (totalSaved < targetCount) {
66-
Paging<Artist> paging = api.searchArtists(q)
67+
Paging<se.michaelthelin.spotify.model_objects.specification.Artist> paging = api.searchArtists(q)
6768
.limit(limit)
6869
.offset(offset)
6970
.build()
@@ -85,10 +86,10 @@ public int seedKoreanArtists300() {
8586
String mainGenreName = pickMainGenreName(spotifyArtist);
8687
Genre genre = findOrCreateGenreByName(mainGenreName, null);
8788

88-
String artistType = inferArtistType(spotifyArtist);
89+
String artistTypeStr = inferArtistType(spotifyArtist);
90+
ArtistType artistType = ArtistType.valueOf(artistTypeStr);
8991

90-
com.back.web7_9_codecrete_be.domain.artists.entity.Artist artistEntity =
91-
new com.back.web7_9_codecrete_be.domain.artists.entity.Artist(
92+
Artist artistEntity = new Artist(
9293
spotifyId,
9394
name.trim(),
9495
null, // artistGroup
@@ -127,7 +128,7 @@ public int seedKoreanArtists300() {
127128
"korean indie", "korean rock"
128129
);
129130

130-
private boolean isLikelyKoreanMusic(Artist a) {
131+
private boolean isLikelyKoreanMusic(se.michaelthelin.spotify.model_objects.specification.Artist a) {
131132
String[] genres = a.getGenres();
132133
if (genres != null) {
133134
for (String g : genres) {
@@ -142,7 +143,7 @@ private boolean isLikelyKoreanMusic(Artist a) {
142143
return name != null && name.matches(".*[가-힣].*");
143144
}
144145

145-
private String pickMainGenreName(Artist a) {
146+
private String pickMainGenreName(se.michaelthelin.spotify.model_objects.specification.Artist a) {
146147
String[] genres = a.getGenres();
147148
if (genres == null || genres.length == 0) return "k-pop";
148149

@@ -161,7 +162,7 @@ private String pickMainGenreName(Artist a) {
161162
return "k-pop";
162163
}
163164

164-
private String inferArtistType(Artist a) {
165+
private String inferArtistType(se.michaelthelin.spotify.model_objects.specification.Artist a) {
165166
String[] genres = a.getGenres();
166167
if (genres != null) {
167168
for (String g : genres) {
@@ -182,15 +183,15 @@ private Genre findOrCreateGenreByName(String genreName, String genreGroup) {
182183
public ArtistDetailResponse getArtistDetail(
183184
String spotifyArtistId,
184185
String artistGroup,
185-
String artistType,
186+
ArtistType artistType,
186187
long likeCount,
187188
long artistId,
188189
Long genreId
189190
) {
190191
try {
191192
SpotifyApi api = spotifyClient.getAuthorizedApi();
192193

193-
Artist artist = api.getArtist(spotifyArtistId).build().execute(); // 메인 정보는 실패 시 예외 발생
194+
se.michaelthelin.spotify.model_objects.specification.Artist artist = api.getArtist(spotifyArtistId).build().execute(); // 메인 정보는 실패 시 예외 발생
194195

195196
Track[] topTracks = safeGetTopTracks(api, spotifyArtistId);
196197
Paging<AlbumSimplified> albums = safeGetAlbums(api, spotifyArtistId);
@@ -210,8 +211,8 @@ public ArtistDetailResponse getArtistDetail(
210211
pickImageUrl(artist.getImages()),
211212
likeCount,
212213
albums != null ? albums.getTotal() : 0,
213-
artist.getPopularity(), // (너가 별점으로 바꾸고 싶으면 여기 가공)
214-
"", // Spotify에서는 설명을 제공하지 않아 공란 처리
214+
artist.getPopularity(), // 별점으로 수정
215+
"", // 설명
215216
toAlbumResponses(albums != null ? albums.getItems() : null, spotifyArtistId),
216217
toTopTrackResponses(topTracks),
217218
relatedResponses
@@ -252,14 +253,9 @@ private Paging<AlbumSimplified> safeGetAlbums(SpotifyApi api, String artistId) {
252253
}
253254
}
254255

255-
/**
256-
* ✅ related artists
257-
* - 정상 호출
258-
* - related가 비거나 404면 fallback(장르 기반 검색)으로 대체
259-
*/
260256
private List<RelatedArtistResponse> safeGetRelated(
261257
SpotifyApi api,
262-
Artist me,
258+
se.michaelthelin.spotify.model_objects.specification.Artist me,
263259
String artistGroup,
264260
long artistId,
265261
Long genreId
@@ -271,7 +267,7 @@ private List<RelatedArtistResponse> safeGetRelated(
271267
}
272268

273269
try {
274-
Artist[] related = api.getArtistsRelatedArtists(id).build().execute();
270+
se.michaelthelin.spotify.model_objects.specification.Artist[] related = api.getArtistsRelatedArtists(id).build().execute();
275271
if (related != null && related.length > 0) {
276272
log.info("Spotify related artists fetched: size={} spotifyArtistId={}", related.length, id);
277273
return toRelatedArtistResponses(related);
@@ -365,7 +361,7 @@ private List<TopTrackResponse> toTopTrackResponses(Track[] tracks) {
365361
.collect(toList());
366362
}
367363

368-
private List<RelatedArtistResponse> toRelatedArtistResponses(Artist[] artists) {
364+
private List<RelatedArtistResponse> toRelatedArtistResponses(se.michaelthelin.spotify.model_objects.specification.Artist[] artists) {
369365
if (artists == null) return List.of();
370366
return Stream.of(artists)
371367
.filter(Objects::nonNull)

0 commit comments

Comments
 (0)