Skip to content

Commit

Permalink
Merge pull request #112 from MEME-UMC/fix/#107
Browse files Browse the repository at this point in the history
[Fix] ReviewImg, PortfolioImg 관련 로직 수정
  • Loading branch information
yeopyeop-82 authored Feb 11, 2024
2 parents b63ea36 + ee2516a commit dbd578f
Show file tree
Hide file tree
Showing 11 changed files with 95 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public ApiResponse createPortfolio(@RequestBody CreatePortfolioDto portfolioDto)
@Operation(summary = "포트폴리오 전체 조회", description = "포트폴리오 전체를 조회하는 API입니다.")
@GetMapping("/{artistId}")
public ApiResponse getPortfolio(@PathVariable Long artistId,
@PageableDefault(size = 30, sort = "id", direction = Sort.Direction.ASC) Pageable page
@RequestParam(value = "page", defaultValue = "0", required = false) int page
){
return ApiResponse.SuccessResponse(SuccessStatus.PORTFOLIO_GET, portfolioService.getPortfolio(artistId, page));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import umc.meme.shop.domain.portfolio.dto.response.PortfolioImgDto;
import umc.meme.shop.global.enums.Category;

import java.util.List;

@Data
@NoArgsConstructor
@AllArgsConstructor
Expand All @@ -20,5 +22,6 @@ public class UpdatePortfolioDto {
private int price;
private String info;
private Boolean isBlock;
private PortfolioImgDto portfolioImg;
private List<PortfolioImgDto> portfolioImgList; // List 형태로 변경
}

Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,8 @@ public static Portfolio from(Artist artist, CreatePortfolioDto dto){
.build();
}

public void addPortfolioImg(PortfolioImg portfolioImg) {
this.portfolioImgList.add(portfolioImg);
portfolioImg.setPortfolio(this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,12 @@ public class PortfolioImg extends BaseEntity {

@Column(nullable = false)
private String src;

public PortfolioImg(String src) {
this.src = src;
}

public void updateSrc(String src) {
this.src = src;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

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

@Service
@RequiredArgsConstructor
Expand All @@ -41,27 +42,20 @@ public void createPortfolio(CreatePortfolioDto portfolioDto) {
if(portfolioRepository.existsByMakeupName(portfolioDto.getMakeupName()))
throw new GlobalException(ErrorStatus.ALREADY_EXIST_PORTFOLIO);

List<PortfolioImg> portfolioImgList = new ArrayList<>();
for (String src : portfolioDto.getPortfolioImgSrc()) {
PortfolioImg portfolioImg = new PortfolioImg();
portfolioImg.setSrc(src);
portfolioImgList.add(portfolioImg);
}
List<PortfolioImg> portfolioImgList = portfolioDto.getPortfolioImgSrc().stream()
.map(PortfolioImg::new)
.toList();

Portfolio portfolio = Portfolio.from(artist, portfolioDto);

for (PortfolioImg portfolioImg : portfolioImgList) {
portfolioImg.setPortfolio(portfolio); // Portfolio 객체 설정
portfolio.getPortfolioImgList().add(portfolioImg); // Portfolio의 이미지 리스트에 추가
}
portfolioImgList.forEach(portfolio::addPortfolioImg);

artist.updatePortfolioList(portfolio);
portfolioRepository.save(portfolio);
}

// 포트폴리오 전체 조회
@Transactional
public PortfolioPageDto getPortfolio(Long artistId, Pageable page) {
public PortfolioPageDto getPortfolio(Long artistId, int page) {
Artist artist = artistRepository.findById(artistId)
.orElseThrow(() -> new GlobalException(ErrorStatus.NOT_EXIST_ARTIST));

Expand All @@ -71,7 +65,7 @@ public PortfolioPageDto getPortfolio(Long artistId, Pageable page) {
portfolioList.removeIf(Portfolio::isBlock);

//list를 page로 변환
Page<Portfolio> portfolioPage = portfolioRepository.findByArtist(artist, page);
Page<Portfolio> portfolioPage = getPage(page, portfolioList);

return PortfolioPageDto.from(portfolioPage);
}
Expand All @@ -87,44 +81,66 @@ public PortfolioDto getPortfolioDetails(Long portfolioId) {
return PortfolioDto.from(portfolio);
}


// 포트폴리오 수정/삭제
@Transactional
public void updatePortfolio(UpdatePortfolioDto request) {
Artist artist = artistRepository.findById(request.getArtistId())
.orElseThrow(() -> new GlobalException(ErrorStatus.NOT_EXIST_ARTIST));

// Portfolio를 getPortfolioDetails 메서드를 이용해 조회
PortfolioDto portfolioDto = getPortfolioDetails(request.getPortfolioId());
Portfolio portfolio = portfolioRepository.findById(portfolioDto.getPortfolioId())
.orElseThrow(() -> new GlobalException(ErrorStatus.NOT_EXIST_PORTFOLIO));
.orElseThrow(() -> new GlobalException(ErrorStatus.NOT_EXIST_PORTFOLIO));

// 아티스트가 해당 portfolio에 권한이 없을때 (포트폴리오에 있는 artist가 본인이 아닐때)
if (!portfolio.getArtist().equals(artist)) {
throw new GlobalException(ErrorStatus.NOT_AUTHORIZED_PORTFOLIO);
}

if (request.getPortfolioImg() != null) {
PortfolioImgDto portfolioImgDto = request.getPortfolioImg();
updatePortfolioImg(portfolio, request.getPortfolioImgList()); // 수정

portfolio.updatePortfolio(request);
}

private void updatePortfolioImg(Portfolio portfolio, List<PortfolioImgDto> portfolioImgDtoList) {
List<PortfolioImg> updatedPortfolioImgs = new ArrayList<>();

for (PortfolioImgDto portfolioImgDto : portfolioImgDtoList) {
PortfolioImg portfolioImg = portfolioImgRepository.findById(portfolioImgDto.getPortfolioImgId())
.orElseThrow(() -> new RuntimeException("포트폴리오 이미지를 찾을 수 없습니다."));
.orElseThrow(() -> new GlobalException(ErrorStatus.NOT_EXIST_PORTFOLIO_IMG));

if (!portfolio.getPortfolioImgList().contains(portfolioImg)) {
throw new GlobalException(ErrorStatus.NOT_EXIST_PORTFOLIO_IMG);
}

if (portfolioImgDto.isDelete()) {
// 이미지 삭제
portfolio.getPortfolioImgList().remove(portfolioImg);
portfolioImgRepository.delete(portfolioImg);
} else if (portfolioImgDto.getPortfolioImgSrc() != null) {
// 이미지 수정 (src 업데이트)
portfolioImg.setSrc(portfolioImgDto.getPortfolioImgSrc());
portfolioImg.updateSrc(portfolioImgDto.getPortfolioImgSrc());
portfolioImgRepository.save(portfolioImg);

// 업데이트된 이미지 정보를 포트폴리오의 이미지 리스트에 반영
portfolio.getPortfolioImgList().removeIf(img -> img.getPortfolioImgId().equals(portfolioImg.getPortfolioImgId()));
portfolio.getPortfolioImgList().add(portfolioImg);
// 업데이트된 이미지 정보를 리스트에 추가
updatedPortfolioImgs.add(portfolioImg);
}
}
// Portfolio 업데이트
portfolio.updatePortfolio(request);

// 업데이트된 이미지 리스트를 포트폴리오의 이미지 리스트에 반영
portfolio.getPortfolioImgList().removeAll(updatedPortfolioImgs);
portfolio.getPortfolioImgList().addAll(updatedPortfolioImgs);
}


private Page<Portfolio> getPage(int page, List<Portfolio> list){
Pageable pageable = PageRequest.of(page, 30);

int start = (int) pageable.getOffset();
int end = Math.min((start + pageable.getPageSize()), list.size());

//list를 page로 변환
return new PageImpl<>(list.subList(start, end),
pageable, list.size());
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,14 @@ public class ReviewController {
@Operation(summary = "리뷰 리스트 조회", description = "리뷰 리스트를 조회하는 API입니다.")
@GetMapping("/{portfolioId}")
public ApiResponse getReviewList(@PathVariable Long portfolioId,
@PageableDefault(size = 30, sort = "portfolio", direction = Sort.Direction.ASC) Pageable page
@RequestParam(value = "page", defaultValue = "0", required = false) int page
){
return ApiResponse.SuccessResponse(SuccessStatus.REVIEW_GET, reviewService.getReviewList(portfolioId, page));
}

@Operation(summary = "리뷰 작성", description = "리뷰를 작성하는 API입니다.")
@PostMapping("/")
public ApiResponse createReview(@RequestBody ReviewDto reviewDto){
//TODO: ReviewImg 추가
reviewService.createReview(reviewDto);
return ApiResponse.SuccessResponse(SuccessStatus.REVIEW_CREATE);
}
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/umc/meme/shop/domain/review/entity/Review.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,9 @@ public static Review from(Model model, Portfolio portfolio, ReviewDto dto){
.reviewImgList(new ArrayList<ReviewImg>())
.build();
}

public void addReviewImg(ReviewImg reviewImg) {
this.reviewImgList.add(reviewImg);
reviewImg.setReview(this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,8 @@ public class ReviewImg extends BaseEntity {

@Column(nullable = false)
private String src;

public ReviewImg(String src) {
this.src = src;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,4 @@

public interface ReviewRepository extends JpaRepository<Review, Long> {
List<Review> findByModel(Model model);

Page<Review> findByPortfolio(Portfolio portfolio, Pageable page);
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

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

@Service
@RequiredArgsConstructor
Expand All @@ -38,35 +39,29 @@ public class ReviewService {

//리뷰 작성
@Transactional
public void createReview(ReviewDto reviewDto){
public void createReview(ReviewDto reviewDto) {
Model model = modelRepository.findById(reviewDto.getModelId())
.orElseThrow(() -> new GlobalException(ErrorStatus.NOT_EXIST_MODEL));
Reservation reservation = reservationRepository.findByReservationIdAndModelId(reviewDto.getReservationId(), reviewDto.getModelId())
.orElseThrow(() -> new GlobalException(ErrorStatus.NOT_EXIST_RESERVATION));

//이미 리뷰 작성 완료
if(reservation.isReview())
// 이미 리뷰 작성 완료
if (reservation.isReview())
throw new GlobalException(ErrorStatus.ALREADY_REVIEWED);

//예약 미완료
if(reservation.getStatus() != Status.COMPLETE)
// 예약 미완료
if (reservation.getStatus() != Status.COMPLETE)
throw new GlobalException(ErrorStatus.INVALID_REVIEW_REQUEST);

Portfolio portfolio = reservation.getPortfolio();

List<ReviewImg> reviewImgList = new ArrayList<>();
for (String src : reviewDto.getReviewImgSrc()) {
ReviewImg reviewImg = new ReviewImg();
reviewImg.setSrc(src);
reviewImgList.add(reviewImg);
}
List<ReviewImg> reviewImgList = reviewDto.getReviewImgSrc().stream()
.map(ReviewImg::new)
.toList();

Review review = Review.from(model, portfolio, reviewDto);

for (ReviewImg reviewImg : reviewImgList) {
reviewImg.setReview(review);
review.getReviewImgList().add(reviewImg);
}
reviewImgList.forEach(review::addReviewImg);

portfolio.updateReviewList(review);
model.updateReviewList(review);
Expand All @@ -88,12 +83,13 @@ public List<ReviewResponseDto> getMyReview(Long modelId){
}

//리뷰 리스트 조회
public ReviewListPageDto getReviewList(Long portfolioId, Pageable page) {
public ReviewListPageDto getReviewList(Long portfolioId, int page) {
Portfolio portfolio = portfolioRepository.findById(portfolioId)
.orElseThrow(() -> new GlobalException(ErrorStatus.NOT_EXIST_PORTFOLIO));

// list를 page로 변환
Page<Review> reviewPage = reviewRepository.findByPortfolio(portfolio, page);
List<Review> reviewList = portfolio.getReviewList();
Page<Review> reviewPage = getPage(page, reviewList);

return ReviewListPageDto.from(reviewPage);
}
Expand All @@ -110,4 +106,15 @@ public void deleteReview(DeleteReviewDto reviewDto){

reviewRepository.delete(review);
}

private Page<Review> getPage(int page, List<Review> list){
Pageable pageable = PageRequest.of(page, 30);

int start = (int) pageable.getOffset();
int end = Math.min((start + pageable.getPageSize()), list.size());

//list를 page로 변환
return new PageImpl<>(list.subList(start, end),
pageable, list.size());
}
}
1 change: 1 addition & 0 deletions src/main/java/umc/meme/shop/global/ErrorStatus.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public enum ErrorStatus {
ALREADY_EXIST_PORTFOLIO(400, "해당 포트폴리오 제목이 이미 존재합니다"),
INVALID_SORT_CRITERIA(400, "잘못된 정렬 기준입니다"),
BLOCKED_PORTFOLIO(400, "숨김 처리된 포트폴리오입니다"),
NOT_EXIST_PORTFOLIO_IMG(400, "포트폴리오 이미지를 찾을 수 없습니다."),

//review
ALREADY_REVIEWED(400, "이미 리뷰 작성이 완료된 예약입니다."),
Expand Down

0 comments on commit dbd578f

Please sign in to comment.