-
Notifications
You must be signed in to change notification settings - Fork 2
[Artist] CRUD 구현 #80
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
[Artist] CRUD 구현 #80
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
ecbb340
Merge branch 'main' of https://github.com/prgrms-web-devcourse-final-…
ys0221 387571f
feat: 아티스트 생성
ys0221 64d8b1a
feat: 아티스트 조회, 수정, 삭제
ys0221 857116b
Merge branch 'main' into feat/#66
ys0221 69a5e08
fix : 아티스트 생성 예외 처리 오류 수정
ys0221 6aa1fb7
Merge branch 'feat/#66' of https://github.com/prgrms-web-devcourse-fi…
ys0221 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 1 addition & 4 deletions
5
src/main/java/com/back/web7_9_codecrete_be/domain/artists/dto/request/CreateRequest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,9 @@ | ||
| package com.back.web7_9_codecrete_be.domain.artists.dto.request; | ||
|
|
||
| import com.back.web7_9_codecrete_be.domain.artists.entity.Genre; | ||
|
|
||
|
|
||
| public record CreateRequest( | ||
| String artistName, | ||
| String artistGroup, | ||
| String artistType, | ||
| Genre genre | ||
| String genreName | ||
| ) { | ||
| } |
5 changes: 1 addition & 4 deletions
5
src/main/java/com/back/web7_9_codecrete_be/domain/artists/dto/request/UpdateRequest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,9 @@ | ||
| package com.back.web7_9_codecrete_be.domain.artists.dto.request; | ||
|
|
||
| import com.back.web7_9_codecrete_be.domain.artists.entity.Genre; | ||
|
|
||
|
|
||
| public record UpdateRequest( | ||
| String artistName, | ||
| String artistGroup, | ||
| String artistType, | ||
| Genre genre | ||
| String genreName | ||
| ) { | ||
| } |
10 changes: 10 additions & 0 deletions
10
src/main/java/com/back/web7_9_codecrete_be/domain/artists/dto/response/AlbumResponse.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| package com.back.web7_9_codecrete_be.domain.artists.dto.response; | ||
|
|
||
| public record AlbumResponse( | ||
| String albumName, | ||
| String releaseDate, | ||
| String albumType, // album / single / ep | ||
| String imageUrl, | ||
| String spotifyUrl | ||
| ) { | ||
| } | ||
18 changes: 18 additions & 0 deletions
18
...n/java/com/back/web7_9_codecrete_be/domain/artists/dto/response/ArtistDetailResponse.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| package com.back.web7_9_codecrete_be.domain.artists.dto.response; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| public record ArtistDetailResponse( | ||
| String artistName, | ||
| String artistGroup, | ||
| String artistType, | ||
| String profileImageUrl, | ||
| long likeCount, | ||
| int totalAlbums, | ||
| double popularityRating, | ||
| String description, | ||
| List<AlbumResponse> albums, | ||
| List<TopTrackResponse> topTracks, | ||
| List<RelatedArtistResponse> relatedArtists | ||
| ) { | ||
| } |
17 changes: 17 additions & 0 deletions
17
...ain/java/com/back/web7_9_codecrete_be/domain/artists/dto/response/ArtistListResponse.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| package com.back.web7_9_codecrete_be.domain.artists.dto.response; | ||
|
|
||
| import com.back.web7_9_codecrete_be.domain.artists.entity.Artist; | ||
|
|
||
| public record ArtistListResponse( | ||
| String artistName, | ||
| String artistGroup, | ||
| String genreName | ||
| ) { | ||
| public static ArtistListResponse from(Artist artist) { | ||
| return new ArtistListResponse( | ||
| artist.getArtistName(), | ||
| artist.getArtistGroup(), | ||
| artist.getGenre().getGenreName() | ||
| ); | ||
| } | ||
| } |
8 changes: 8 additions & 0 deletions
8
.../java/com/back/web7_9_codecrete_be/domain/artists/dto/response/RelatedArtistResponse.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| package com.back.web7_9_codecrete_be.domain.artists.dto.response; | ||
|
|
||
| public record RelatedArtistResponse( | ||
| String artistName, | ||
| String imageUrl, | ||
| String spotifyArtistId | ||
| ) { | ||
| } |
7 changes: 7 additions & 0 deletions
7
src/main/java/com/back/web7_9_codecrete_be/domain/artists/dto/response/TopTrackResponse.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| package com.back.web7_9_codecrete_be.domain.artists.dto.response; | ||
|
|
||
| public record TopTrackResponse( | ||
| String trackName, | ||
| String spotifyUrl | ||
| ) { | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 94 additions & 0 deletions
94
src/main/java/com/back/web7_9_codecrete_be/domain/artists/service/ArtistService.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,17 +1,111 @@ | ||
| package com.back.web7_9_codecrete_be.domain.artists.service; | ||
|
|
||
| import com.back.web7_9_codecrete_be.domain.artists.dto.request.UpdateRequest; | ||
| import com.back.web7_9_codecrete_be.domain.artists.dto.response.ArtistListResponse; | ||
| import com.back.web7_9_codecrete_be.domain.artists.dto.response.ArtistDetailResponse; | ||
| import com.back.web7_9_codecrete_be.domain.artists.entity.Artist; | ||
| import com.back.web7_9_codecrete_be.domain.artists.entity.Genre; | ||
| import com.back.web7_9_codecrete_be.domain.artists.repository.ArtistRepository; | ||
| import com.back.web7_9_codecrete_be.domain.artists.repository.ArtistLikeRepository; | ||
| import com.back.web7_9_codecrete_be.global.error.code.ArtistErrorCode; | ||
| import com.back.web7_9_codecrete_be.global.error.exception.BusinessException; | ||
| import org.springframework.transaction.annotation.Transactional; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.stereotype.Service; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| @Service | ||
| @RequiredArgsConstructor | ||
| public class ArtistService { | ||
|
|
||
| private final SpotifyService spotifyService; | ||
| private final ArtistRepository artistRepository; | ||
| private final GenreService genreService; | ||
| private final ArtistLikeRepository artistLikeRepository; | ||
|
|
||
| @Transactional | ||
| public int setArtist() { | ||
| return spotifyService.seedKoreanArtists300(); | ||
| } | ||
|
|
||
| @Transactional | ||
| public Artist createArtist(String artistName, String artistGroup, String artistType, String genreName) { | ||
| Genre genre = genreService.findByGenreName(genreName); | ||
| if(artistRepository.existsByArtistName(artistName) || artistRepository.existsByNameKo(artistName)) { | ||
| throw new BusinessException(ArtistErrorCode.ARTIST_ALREADY_EXISTS); | ||
| } | ||
| Artist artist = new Artist(artistName, artistGroup, artistType, genre); | ||
| artistRepository.save(artist); | ||
| return artist; | ||
| } | ||
|
|
||
| @Transactional(readOnly = true) | ||
| public List<ArtistListResponse> listArtist() { | ||
| return artistRepository.findAll().stream() | ||
| .map(ArtistListResponse::from) | ||
| .toList(); | ||
| } | ||
|
|
||
| @Transactional(readOnly = true) | ||
| public ArtistDetailResponse getArtistDetail(Long artistId) { | ||
| Artist artist = artistRepository.findById(artistId) | ||
| .orElseThrow(() -> new BusinessException(ArtistErrorCode.ARTIST_NOT_FOUND)); | ||
|
|
||
| if (artist.getSpotifyArtistId() == null) { | ||
| throw new BusinessException(ArtistErrorCode.SPOTIFY_NOT_FOUND); | ||
| } | ||
|
|
||
| long likeCount = artistLikeRepository.countByArtistId(artistId); | ||
|
|
||
| return spotifyService.getArtistDetail( | ||
| artist.getSpotifyArtistId(), | ||
| artist.getArtistGroup(), | ||
| artist.getArtistType(), | ||
| likeCount, | ||
| artist.getId(), | ||
| artist.getGenre() != null ? artist.getGenre().getId() : null | ||
| ); | ||
| } | ||
|
|
||
| @Transactional | ||
| public void updateArtist(Long id, UpdateRequest req) { | ||
| Artist artist = artistRepository.findById(id) | ||
| .orElseThrow(() -> new BusinessException(ArtistErrorCode.ARTIST_NOT_FOUND)); | ||
|
|
||
| boolean changed = false; | ||
|
|
||
| if (req.artistName() != null && !req.artistName().isBlank()) { | ||
| artist.changeName(req.artistName().trim()); | ||
| changed = true; | ||
| } | ||
|
|
||
| if (req.artistGroup() != null && !req.artistGroup().isBlank()) { | ||
| artist.changeGroup(req.artistGroup().trim()); | ||
| changed = true; | ||
| } | ||
|
|
||
| if (req.artistType() != null && !req.artistType().isBlank()) { | ||
| artist.changeType(req.artistType().trim()); | ||
| changed = true; | ||
| } | ||
|
|
||
| if (req.genreName() != null && !req.genreName().isBlank()) { | ||
| Genre genre = genreService.findByGenreName(req.genreName().trim()); | ||
| artist.changeGenre(genre); | ||
| changed = true; | ||
| } | ||
|
|
||
| if (!changed) { | ||
| throw new BusinessException(ArtistErrorCode.INVALID_UPDATE_REQUEST); // "수정할 값이 없습니다" | ||
| } | ||
| } | ||
|
|
||
| @Transactional | ||
| public void delete(Long id) { | ||
| Artist artist = artistRepository.findById(id) | ||
| .orElseThrow(() -> new BusinessException(ArtistErrorCode.ARTIST_NOT_FOUND)); | ||
| artistRepository.delete(artist); | ||
| } | ||
|
|
||
| } |
22 changes: 22 additions & 0 deletions
22
src/main/java/com/back/web7_9_codecrete_be/domain/artists/service/GenreService.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| package com.back.web7_9_codecrete_be.domain.artists.service; | ||
|
|
||
| import com.back.web7_9_codecrete_be.domain.artists.entity.Genre; | ||
| import com.back.web7_9_codecrete_be.domain.artists.repository.GenreRepository; | ||
| import com.back.web7_9_codecrete_be.global.error.code.GenreErrorCode; | ||
| import com.back.web7_9_codecrete_be.global.error.exception.BusinessException; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.stereotype.Service; | ||
|
|
||
| @Service | ||
| @RequiredArgsConstructor | ||
| public class GenreService { | ||
|
|
||
| private final GenreRepository genreRepository; | ||
|
|
||
| public Genre findByGenreName(String genreName) { | ||
| String normalized = genreName.trim().toLowerCase(); | ||
| Genre genre = genreRepository.findByGenreName(normalized) | ||
| .orElseThrow(() -> new BusinessException(GenreErrorCode.GENRE_NOT_FOUND)); | ||
| return genre; | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
DTO에도 @Schema 통해서 설명 붙여주시면 좋을 것 같아요
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
넵 수정하겠씁니다!