Skip to content

Commit

Permalink
refactor: User - Favorite Artist/Portfolio 연관관계 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
daeun084 committed Oct 4, 2024
1 parent 414804a commit 4a53698
Show file tree
Hide file tree
Showing 13 changed files with 38 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class FavoriteConverter {
public static FavoriteArtist toFavoriteArtist(Artist artist, Model model){
return FavoriteArtist.builder()
.model(model)
.artistId(artist.getUserId())
.artist(artist)
.build();
}

Expand All @@ -31,7 +31,7 @@ public static FavoriteArtist toFavoriteArtist(Artist artist, Model model){
// favoritePortfolio
public static FavoritePortfolio toFavoritePortfolio(Model model, Portfolio portfolio){
return FavoritePortfolio.builder()
.model(model.getUser())
.model(model)
.portfolio(portfolio)
.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ public static ReviewResponse.ReviewDto toReviewDto(Review review){

return ReviewResponse.ReviewDto.builder()
.reviewId(review.getReviewId())
.modelNickName(review.getModel().getNickname())
.modelProfileImg(review.getModel().getProfileImg())
.modelNickName(review.getModel().getUser().getNickname())
.modelProfileImg(review.getModel().getUser().getProfileImg())
.star(review.getStar())
.comment(review.getComment())
.createdAt(review.getCreatedAt())
Expand All @@ -46,7 +46,7 @@ public static ReviewResponse.ReviewDetailsDto toReviewDetailDto(Review review){
.toList();

return ReviewResponse.ReviewDetailsDto.builder()
.artistNickName(review.getPortfolio().getArtist().getNickname())
.artistNickName(review.getPortfolio().getArtist().getUser().getNickname())
.makeupName(review.getPortfolio().getMakeupName())
.star(review.getStar())
.comment(review.getComment())
Expand All @@ -57,7 +57,7 @@ public static ReviewResponse.ReviewDetailsDto toReviewDetailDto(Review review){
public static ReviewResponse.MyReviewDto toMyReviewDto(Review review){
return ReviewResponse.MyReviewDto.builder()
.reviewId(review.getReviewId())
.artistNickName(review.getPortfolio().getArtist().getNickname())
.artistNickName(review.getPortfolio().getArtist().getUser().getNickname())
.makeupName(review.getPortfolio().getMakeupName())
.portfolioImg(review.getPortfolio().getPortfolioImgList().get(0).getSrc()) //첫 번째 포트폴리오 이미지
.location(review.getPortfolio().getArtist().getShopLocation())
Expand All @@ -69,7 +69,7 @@ public static ReviewResponse.ReviewAvailableListDto toReviewAvailableListDto(Res
return ReviewResponse.ReviewAvailableListDto.builder()
.reservationId(reservation.getReservationId())
.portfolioId(reservation.getReservationId())
.artistNickName(reservation.getPortfolio().getArtist().getNickname())
.artistNickName(reservation.getPortfolio().getArtist().getUser().getNickname())
.makeupName(reservation.getPortfolio().getMakeupName())
.portfolioImg(reservation.getPortfolio().getPortfolioImgList().get(0).getSrc())
.reservationYear(reservation.getYear())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public class FavoriteArtist extends BaseEntity {
@JoinColumn(name="user_id", nullable = false)
private Model model;

private Long artistId;
@OneToOne
private Artist artist;

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class FavoritePortfolio extends BaseEntity {

@ManyToOne
@JoinColumn(name="user_id", nullable = false)
private User model;
private Model model;

@ManyToOne
@JoinColumn(name="portfolio_id", nullable = false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class Review extends BaseEntity {

@ManyToOne
@JoinColumn(name="user_id", nullable = false)
private User model;
private Model model;

@OneToMany(cascade = CascadeType.ALL, mappedBy = "review")
private List<ReviewImg> reviewImgList;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,5 @@

@Repository
public interface ArtistRepository extends JpaRepository<Artist, Long> {
Optional<Artist> findByEmail(String email);

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.meme.service.domain.repository;

import org.meme.service.domain.entity.Artist;
import org.meme.service.domain.entity.FavoriteArtist;
import org.meme.service.domain.entity.Model;
import org.springframework.data.jpa.repository.JpaRepository;
Expand All @@ -11,7 +12,7 @@
@Repository
public interface FavoriteArtistRepository extends JpaRepository<FavoriteArtist, Long> {
List<FavoriteArtist> findByModel(Model model);
boolean existsByModelAndArtistId(Model model, Long userId);
Optional<FavoriteArtist> findByModelAndArtistId(Model model, Long userId);
Long countByArtistId(Long artistId);
boolean existsByModelAndArtist(Model model, Artist artist);
Optional<FavoriteArtist> findByModelAndArtist(Model model, Artist artist);
Long countByArtist(Artist artist);
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,4 @@
@Repository
public interface ModelRepository extends JpaRepository<Model, Long> {

Optional<Model> findByEmail(String email);
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@
import org.springframework.stereotype.Service;
import org.meme.service.domain.entity.Artist;
import org.meme.service.common.exception.GeneralException;
import org.springframework.transaction.annotation.Transactional;

import java.util.Optional;

@Service
@RequiredArgsConstructor
@Transactional(readOnly = true)
public class ArtistService {
private final ArtistRepository artistRepository;
private final ModelRepository modelRepository;
Expand All @@ -29,7 +31,7 @@ public ArtistResponse.ArtistDto getArtistProfile(Long userId, Long artistId){
Artist artist = findArtistById(artistId);

boolean isFavorite = false;
Optional<FavoriteArtist> favoriteArtist = favoriteArtistRepository.findByModelAndArtistId(model, artistId);
Optional<FavoriteArtist> favoriteArtist = favoriteArtistRepository.findByModelAndArtist(model, artist);
if(favoriteArtist.isPresent())
isFavorite = true;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.meme.service.domain.service;

import jakarta.transaction.Transactional;
import lombok.RequiredArgsConstructor;
import org.meme.service.common.exception.GeneralException;
import org.meme.service.common.status.ErrorStatus;
Expand All @@ -16,12 +15,14 @@
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
@Transactional(readOnly = true)
public class FavoriteService {
private final ModelRepository modelRepository;
private final ArtistRepository artistRepository;
Expand All @@ -42,9 +43,9 @@ public FavoriteResponse.FavoriteArtistPageDto getFavoriteArtist(Long modelId, in
//관심 아티스트 리스트
List<ArtistResponse.ArtistSimpleDto> content = favoriteArtistPage.getContent().stream()
.map(favoriteArtist -> {
Artist artist = findArtistById(favoriteArtist.getArtistId());
Artist artist = favoriteArtist.getArtist();
//해당 아티스트를 관심 아티스트로 설정한 모델 수 카운트
Long modelCount = favoriteArtistRepository.countByArtistId(artist.getUserId());
Long modelCount = favoriteArtistRepository.countByArtist(artist);
return ArtistConverter.toArtistSimpleDto(artist, modelCount);
})
.collect(Collectors.toList());
Expand All @@ -71,7 +72,7 @@ public void addFavoriteArtist(FavoriteRequest.FavoriteArtistDto favoriteArtistDt
Artist artist = findArtistById(favoriteArtistDto.getArtistId());

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

Expand Down Expand Up @@ -102,7 +103,7 @@ public void deleteFavoriteArtist(FavoriteRequest.FavoriteArtistDto favoriteArtis
Model model = findModelById(favoriteArtistDto.getModelId());
Artist artist = findArtistById(favoriteArtistDto.getArtistId());

FavoriteArtist favoriteArtist = findFavoriteArtistByModelAndArtistId(model, artist.getUserId());
FavoriteArtist favoriteArtist = findFavoriteArtistByModelAndArtist(model, artist);
favoriteArtistRepository.delete(favoriteArtist);
}

Expand Down Expand Up @@ -136,8 +137,8 @@ private FavoritePortfolio findFavoritePortfolioByModelAndPortfolio(Model model,
.orElseThrow(() -> new GeneralException(ErrorStatus.NOT_EXIST_FAVORITE_PORTFOLIO));
}

private FavoriteArtist findFavoriteArtistByModelAndArtistId(Model model, Long artistId){
return favoriteArtistRepository.findByModelAndArtistId(model, artistId)
private FavoriteArtist findFavoriteArtistByModelAndArtist(Model model, Artist artist){
return favoriteArtistRepository.findByModelAndArtist(model, artist)
.orElseThrow(() -> new GeneralException(ErrorStatus.NOT_EXIST_FAVORITE_ARTIST));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.meme.service.domain.service;

import jakarta.transaction.Transactional;
import lombok.RequiredArgsConstructor;
import org.meme.service.common.exception.GeneralException;
import org.meme.service.common.status.ErrorStatus;
Expand All @@ -16,11 +15,13 @@
import org.meme.service.domain.dto.request.MypageRequest;
import org.meme.service.domain.dto.response.MypageResponse;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@RequiredArgsConstructor
@Transactional(readOnly = true)
public class MypageService {
private final ModelRepository modelRepository;
private final ArtistRepository artistRepository;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.meme.service.domain.service;

import jakarta.transaction.Transactional;
import lombok.RequiredArgsConstructor;
import org.meme.service.domain.dto.request.PortfolioRequest;
import org.meme.service.domain.dto.response.PortfolioResponse;
Expand All @@ -15,6 +14,7 @@
import org.meme.service.domain.repository.PortfolioRepository;
import org.meme.service.common.status.ErrorStatus;
import org.meme.service.common.exception.GeneralException;
import org.springframework.transaction.annotation.Transactional;


import java.util.ArrayList;
Expand All @@ -23,6 +23,7 @@

@Service
@RequiredArgsConstructor
@Transactional(readOnly = true)
public class PortfolioService {
private final ArtistRepository artistRepository;
private final PortfolioRepository portfolioRepository;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.meme.service.domain.service;

import jakarta.transaction.Transactional;
import lombok.RequiredArgsConstructor;
import org.meme.service.common.exception.GeneralException;
import org.meme.service.domain.entity.*;
Expand All @@ -16,13 +15,16 @@
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.meme.service.common.status.ErrorStatus;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

@Service
@RequiredArgsConstructor
@Transactional(readOnly = true)
public class ReviewService {
private final ModelRepository modelRepository;
private final ReviewRepository reviewRepository;
Expand Down Expand Up @@ -110,15 +112,15 @@ public ReviewResponse.ReviewDetailsDto updateReview(ReviewRequest.UpdateReviewDt
Model model = findModelById(updateReviewDto.getModelId());
Review review = findReviewById(updateReviewDto.getReviewId());

if (!review.getModel().equals(model))
if (!review.getModel().getUserId().equals(model.getUserId()))
throw new GeneralException(ErrorStatus.INVALID_MODEL_FOR_REVIEW);

// 리뷰 이미지 수정
if (!updateReviewDto.getReviewImgSrcList().isEmpty())
updateReviewImgList(review, updateReviewDto.getReviewImgSrcList());

// 리뷰 수정
updateReviewEntity(review, updateReviewDto);
updateReview(review, updateReviewDto);
return ReviewConverter.toReviewDetailDto(review);
}

Expand Down Expand Up @@ -160,18 +162,17 @@ public void updateReviewImgList(Review review, List<String> reviewImgSrcList){
public void deleteReview(ReviewRequest.DeleteReviewDto reviewDto){
Model model = findModelById(reviewDto.getModelId());
Review review = findReviewById(reviewDto.getReviewId());
if(!review.getModel().equals(model))

if(!Objects.equals(review.getModel().getUserId(), model.getUserId()))
throw new GeneralException(ErrorStatus.INVALID_MODEL_FOR_REVIEW);

reviewRepository.delete(review);
}

private void validateReservationStatus(Reservation reservation) {
// 이미 리뷰 작성 완료
if (reservation.isReviewed())
throw new GeneralException(ErrorStatus.ALREADY_REVIEWED);

// 예약 미완료
if (!reservation.isCompleted())
throw new GeneralException(ErrorStatus.INVALID_REVIEW_REQUEST);
}
Expand Down Expand Up @@ -207,7 +208,7 @@ private Page<Review> getPage(int page, List<Review> list){
pageable, list.size());
}

private void updateReviewEntity(Review review, ReviewRequest.UpdateReviewDto updateReviewDto){
private void updateReview(Review review, ReviewRequest.UpdateReviewDto updateReviewDto){
if(updateReviewDto.getStar() > 0)
review.setStar(updateReviewDto.getStar());
if(updateReviewDto.getComment() != null)
Expand Down

0 comments on commit 4a53698

Please sign in to comment.