-
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
[Artist] CRUD 구현 #80
Changes from 4 commits
ecbb340
387571f
64d8b1a
857116b
69a5e08
6aa1fb7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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 | ||
| ) { | ||
| } |
| 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 | ||
| ) { | ||
| } |
| 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 | ||
| ) { | ||
| } | ||
| 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 | ||
| ) { | ||
| } |
| 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() | ||
| ); | ||
| } | ||
| } |
| 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 | ||
| ) { | ||
| } |
| 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 | ||
| ) { | ||
| } |
| 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)) { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if (artistRepository.existsByArtistName(artistName)
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 넵 그렇게 수정하도록 하겠습니다! |
||
| 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); | ||
| } | ||
|
|
||
| } | ||
| 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; | ||
| } | ||
| } |
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.
넵 수정하겠씁니다!