Skip to content

Commit

Permalink
fix: 관심 아티스트 오류 해결
Browse files Browse the repository at this point in the history
  • Loading branch information
daeun084 committed Feb 3, 2024
1 parent 78a45e7 commit a47744c
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@
import java.util.List;

public class ArtistConverter {
public static FavoriteArtistPageResponseDto favoriteArtistPageConverter(Page<FavoriteArtist> page) {
List<FavoriteArtistResponseDto> content = page.stream()
.map(FavoriteArtistResponseDto::from)
.toList();
public static FavoriteArtistPageResponseDto favoriteArtistPageConverter(Page<FavoriteArtist> page, List<FavoriteArtistResponseDto> content) {

return FavoriteArtistPageResponseDto.builder()
.content(content)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ public class FavoriteArtistResponseDto {
private String profileImg;
private String artistNickName;

public static FavoriteArtistResponseDto from(FavoriteArtist favoriteArtist) {
Artist artist = favoriteArtist.getArtist();
public static FavoriteArtistResponseDto from(Artist artist) {
return FavoriteArtistResponseDto.builder()
.artistId(artist.getUserId())
.profileImg(artist.getProfileImg())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import umc.meme.shop.domain.artist.entity.Artist;
import umc.meme.shop.domain.model.entity.Model;

@Builder
Expand All @@ -19,14 +18,9 @@ public class FavoriteArtist {
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long favoriteArtistId;

//TODO: field fix
@ManyToOne
// @MapsId
@JoinColumn(name="user_id", nullable = false)
private Model model;

@ManyToOne
// @MapsId
@JoinColumn(name="user_id", nullable = false)
private Artist artist;
private Long artistId;
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@

public interface FavoriteArtistRepository extends JpaRepository<FavoriteArtist, Long> {
List<FavoriteArtist> findByModel(Model model);
boolean existsByModelAndArtist(Model model, Artist artist);
Optional<FavoriteArtist> findByModelAndArtist(Model model, Artist artist);
boolean existsByModelAndArtistId(Model model, Long userId);
Optional<FavoriteArtist> findByModelAndArtistId(Model model, Long userId);
}
19 changes: 15 additions & 4 deletions src/main/java/umc/meme/shop/domain/model/service/ModelService.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import umc.meme.shop.domain.favorite.dto.request.FavoriteArtistDto;
import umc.meme.shop.domain.favorite.dto.request.FavoritePortfolioDto;
import umc.meme.shop.domain.favorite.dto.response.FavoriteArtistPageResponseDto;
import umc.meme.shop.domain.favorite.dto.response.FavoriteArtistResponseDto;
import umc.meme.shop.domain.favorite.dto.response.FavoritePortfolioResponsePageDto;
import umc.meme.shop.domain.favorite.entity.FavoriteArtist;
import umc.meme.shop.domain.favorite.entity.FavoritePortfolio;
Expand All @@ -26,6 +27,7 @@
import umc.meme.shop.global.ErrorStatus;
import umc.meme.shop.global.exception.GlobalException;

import java.util.ArrayList;
import java.util.List;

@Service
Expand Down Expand Up @@ -78,7 +80,16 @@ public FavoriteArtistPageResponseDto getFavoriteArtist(Long modelId, int page){
Page<FavoriteArtist> favoriteArtistPage = new PageImpl<>(favoriteArtistList.subList(start, end),
pageable, favoriteArtistList.size());

return ArtistConverter.favoriteArtistPageConverter(favoriteArtistPage);
List<FavoriteArtistResponseDto> content = new ArrayList<>();
for(int i=0; i<favoriteArtistPage.getContent().size(); i++){
FavoriteArtist favoriteArtist = favoriteArtistPage.getContent().get(i);
Artist artist = artistRepository.findById(favoriteArtist.getArtistId())
.orElseThrow(() -> new GlobalException(ErrorStatus.NOT_EXIST_ARTIST));
FavoriteArtistResponseDto dto = FavoriteArtistResponseDto.from(artist);
content.add(dto);
}

return ArtistConverter.favoriteArtistPageConverter(favoriteArtistPage, content);
}

//관심 메이크업 조회
Expand Down Expand Up @@ -110,12 +121,12 @@ public void addFavoriteArtist(FavoriteArtistDto favoriteArtistDto) {
.orElseThrow(() -> new GlobalException(ErrorStatus.NOT_EXIST_ARTIST));

//이미 관심 아티스트가 존재하는 경우
if (favoriteArtistRepository.existsByModelAndArtist(model, artist)) {
if (favoriteArtistRepository.existsByModelAndArtistId(model, artist.getUserId())) {
throw new GlobalException(ErrorStatus.ALREADY_EXIST_FAVORITE_ARTIST);
}

FavoriteArtist favoriteArtist = FavoriteArtist.builder()
.artist(artist)
.artistId(artist.getUserId())
.model(model)
.build();
model.updateFavoriteArtistList(favoriteArtist);
Expand Down Expand Up @@ -153,7 +164,7 @@ public void deleteFavoriteArtist(FavoriteArtistDto favoriteArtistDto){
Artist artist = artistRepository.findById(favoriteArtistDto.getArtistId())
.orElseThrow(() -> new GlobalException(ErrorStatus.NOT_EXIST_ARTIST));

FavoriteArtist favoriteArtist = favoriteArtistRepository.findByModelAndArtist(model, artist)
FavoriteArtist favoriteArtist = favoriteArtistRepository.findByModelAndArtistId(model, artist.getUserId())
.orElseThrow(() -> new GlobalException(ErrorStatus.NOT_EXIST_FAVORITE_ARTIST));
favoriteArtistRepository.delete(favoriteArtist);
}
Expand Down

0 comments on commit a47744c

Please sign in to comment.