Skip to content

Commit

Permalink
Merge pull request #163 from MEME-UMC/chore/#162
Browse files Browse the repository at this point in the history
[Chore] 포트폴리오, 리뷰 이미지 수정 및 삭제 로직 수정
  • Loading branch information
daeun084 authored May 16, 2024
2 parents 5e9056c + 985cf83 commit 69b8648
Show file tree
Hide file tree
Showing 12 changed files with 121 additions and 98 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import umc.meme.shop.domain.portfolio.dto.response.PortfolioImgDto;
import umc.meme.shop.global.enums.Category;

import java.util.List;
Expand All @@ -22,6 +21,6 @@ public class UpdatePortfolioDto {
private int price;
private String info;
private Boolean isBlock;
private List<PortfolioImgDto> portfolioImgList; // List 형태로 변경
private List<String> portfolioImgSrcList;
}

Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,12 @@
@Builder
public class PortfolioImgDto {
private Long portfolioImgId;

//이미지 링크
private String portfolioImgSrc;

//삭제 여부
private boolean isDelete = false;

public static PortfolioImgDto from(PortfolioImg img){
return PortfolioImgDto.builder()
.portfolioImgId(img.getPortfolioImgId())
.portfolioImgSrc(img.getSrc())
.isDelete(false)
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ private void updateAverageStars(){
this.averageStars = String.format("%.2f", stars/count);
}

public void updatePortfolioImgList(List<PortfolioImg> portfolioImgList){this.portfolioImgList = portfolioImgList;}

public static Portfolio from(Artist artist, CreatePortfolioDto dto){
return Portfolio.builder()
.artist(artist)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
package umc.meme.shop.domain.portfolio.entity;

import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.*;
import umc.meme.shop.domain.common.BaseEntity;

@AllArgsConstructor
@NoArgsConstructor
@Entity
@Builder
@Getter @Setter
public class PortfolioImg extends BaseEntity {
@Id
Expand All @@ -23,8 +21,10 @@ public class PortfolioImg extends BaseEntity {
@Column(nullable = false)
private String src;

public PortfolioImg(String src) {
this.src = src;
public static PortfolioImg from(String src){
return PortfolioImg.builder()
.src(src)
.build();
}

public void updateSrc(String src) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package umc.meme.shop.domain.portfolio.repository;

import org.springframework.data.jpa.repository.JpaRepository;
import umc.meme.shop.domain.portfolio.entity.Portfolio;
import umc.meme.shop.domain.portfolio.entity.PortfolioImg;

import java.util.Optional;

public interface PortfolioImgRepository extends JpaRepository<PortfolioImg, Long> {
Optional<PortfolioImg> findBySrcAndPortfolio(String src, Portfolio portfolio);
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,21 +38,26 @@ public class PortfolioService {

//포트폴리오 생성
@Transactional
public Long createPortfolio(CreatePortfolioDto dto) {
Artist artist = artistRepository.findById(dto.getArtistId())
public Long createPortfolio(CreatePortfolioDto portfolioDto) {
Artist artist = artistRepository.findById(portfolioDto.getArtistId())
.orElseThrow(() -> new GlobalException(ErrorStatus.NOT_EXIST_ARTIST));

//포트폴리오 이름이 이미 존재할 시
if(portfolioRepository.existsByMakeupName(dto.getMakeupName()))
if (portfolioRepository.existsByMakeupName(portfolioDto.getMakeupName()))
throw new GlobalException(ErrorStatus.ALREADY_EXIST_PORTFOLIO);

List<PortfolioImg> portfolioImgList = dto.getPortfolioImgSrc().stream()
.map(PortfolioImg::new)
// 포트폴리오 이미지 리스트 생성
List<PortfolioImg> portfolioImgList = portfolioDto.getPortfolioImgSrc().stream()
.map(PortfolioImg::from)
.toList();

Portfolio portfolio = Portfolio.from(artist, dto);
// 포트폴리오 entity 생성
Portfolio portfolio = Portfolio.from(artist, portfolioDto);

// 포트폴리오 이미지, 포트폴리오 연관관계 설정
portfolioImgList.forEach(portfolio::addPortfolioImg);

// 포트폴리오 연관관계 설정
artist.updatePortfolioList(portfolio);
portfolioRepository.save(portfolio);
return portfolio.getPortfolioId();
Expand Down Expand Up @@ -82,72 +87,79 @@ public PortfolioDetailDto getPortfolioDetails(Long userId, Long portfolioId) {
Portfolio portfolio = portfolioRepository.findById(portfolioId)
.orElseThrow(() -> new GlobalException(ErrorStatus.NOT_EXIST_PORTFOLIO));

if(portfolio.isBlock())
if (portfolio.isBlock())
throw new GlobalException(ErrorStatus.BLOCKED_PORTFOLIO);

boolean isFavorite = false;
Optional<FavoritePortfolio> favoritePortfolio = favoritePortfolioRepository.findByModelAndPortfolio(model, portfolio);
if(favoritePortfolio.isPresent())
if (favoritePortfolio.isPresent())
isFavorite = true;

return PortfolioDetailDto.from(portfolio, isFavorite);
}

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

Portfolio portfolio = portfolioRepository.findById(dto.getPortfolioId())
Portfolio portfolio = portfolioRepository.findById(updatePortfolioDto.getPortfolioId())
.orElseThrow(() -> new GlobalException(ErrorStatus.NOT_EXIST_PORTFOLIO));

if(portfolio.isBlock() && dto.getIsBlock())
if (portfolio.isBlock() && updatePortfolioDto.getIsBlock())
throw new GlobalException(ErrorStatus.BLOCKED_PORTFOLIO);

if (!portfolio.getArtist().equals(artist)) {
if (!portfolio.getArtist().equals(artist))
throw new GlobalException(ErrorStatus.NOT_AUTHORIZED_PORTFOLIO);
}

if(!dto.getPortfolioImgList().isEmpty())
updatePortfolioImg(portfolio, dto.getPortfolioImgList()); // 수정
// 포트폴리오 이미지 수정
if (!updatePortfolioDto.getPortfolioImgSrcList().isEmpty())
updatePortfolioImgList(portfolio, updatePortfolioDto.getPortfolioImgSrcList());

portfolio.updatePortfolio(dto);
// 포트폴리오 수정
portfolio.updatePortfolio(updatePortfolioDto);
}

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 GlobalException(ErrorStatus.NOT_EXIST_PORTFOLIO_IMG));
@Transactional
public void updatePortfolioImgList(Portfolio portfolio, List<String> portfolioImgDtoList) {
List<PortfolioImg> updatedPortfolioImgList = new ArrayList<>();

if (!portfolio.getPortfolioImgList().contains(portfolioImg)) {
for (String portfolioImgSrc : portfolioImgDtoList) {
if (portfolioImgSrc == null)
throw new GlobalException(ErrorStatus.NOT_EXIST_PORTFOLIO_IMG);

Optional<PortfolioImg> portfolioImg = portfolioImgRepository.findBySrcAndPortfolio(portfolioImgSrc, portfolio);
if (portfolioImg.isEmpty()) {
// 새로운 이미지 추가
PortfolioImg newPortfolioImg = PortfolioImg.from(portfolioImgSrc);
newPortfolioImg.setPortfolio(portfolio);
portfolioImgRepository.save(newPortfolioImg);
updatedPortfolioImgList.add(newPortfolioImg);
} else {
// 기존 이미지 보존
updatedPortfolioImgList.add(portfolioImg.get());
}
}

if (portfolioImgDto.isDelete()) {
// 기존 리뷰 이미지 리스트와 새로운 리뷰 이미지 리스트 비교
List<PortfolioImg> existedPortfolioImgList = portfolio.getPortfolioImgList();
for (PortfolioImg portfolioImg : existedPortfolioImgList) {
if (!updatedPortfolioImgList.contains(portfolioImg)) {
// 이미지 삭제
portfolio.getPortfolioImgList().remove(portfolioImg);
portfolioImgRepository.delete(portfolioImg);
} else if (portfolioImgDto.getPortfolioImgSrc() != null) {
// 이미지 수정 (src 업데이트)
portfolioImg.updateSrc(portfolioImgDto.getPortfolioImgSrc());
portfolioImgRepository.save(portfolioImg);

// 업데이트된 이미지 정보를 리스트에 추가
updatedPortfolioImgs.add(portfolioImg);
}
}

// 업데이트된 이미지 리스트를 포트폴리오의 이미지 리스트에 반영
portfolio.getPortfolioImgList().removeAll(updatedPortfolioImgs);
portfolio.getPortfolioImgList().addAll(updatedPortfolioImgs);
// 포트폴리오 이미지 리스트 - 포트폴리오 연관관계 설정
portfolio.updatePortfolioImgList(updatedPortfolioImgList);
}

/**recommend**/
/**
* recommend
**/
//리뷰 많은 순 포트폴리오 추천
public List<SimplePortfolioDto> recommendReview(){
public List<SimplePortfolioDto> recommendReview() {
Pageable pageable = setPageRequest(0, "review");
Page<Portfolio> portfolioList = portfolioRepository.findAllNotBlocked(pageable);

Expand All @@ -157,7 +169,7 @@ public List<SimplePortfolioDto> recommendReview(){
}

//최신 등록 순 포트폴리오 추천
public List<SimplePortfolioDto> recommendRecent(){
public List<SimplePortfolioDto> recommendRecent() {
Pageable pageable = setPageRequest(0, "recent");
Page<Portfolio> portfolioList = portfolioRepository.findAllNotBlocked(pageable);

Expand All @@ -166,7 +178,7 @@ public List<SimplePortfolioDto> recommendRecent(){
.toList();
}

private Pageable setPageRequest(int page, String sortBy){
private Pageable setPageRequest(int page, String sortBy) {

Sort sort = switch (sortBy) {
case "desc" -> Sort.by("price").descending();
Expand All @@ -181,7 +193,7 @@ private Pageable setPageRequest(int page, String sortBy){
return PageRequest.of(page, 30, finalSort);
}

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

int start = (int) pageable.getOffset();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,10 @@

import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.annotation.*;
import umc.meme.shop.domain.review.dto.request.PatchReviewDto;
import umc.meme.shop.domain.review.dto.request.ReviewDto;
import umc.meme.shop.domain.review.dto.request.DeleteReviewDto;
import umc.meme.shop.domain.review.dto.request.UpdateReviewDto;
import umc.meme.shop.domain.review.service.ReviewService;
import umc.meme.shop.global.SuccessStatus;
import umc.meme.shop.global.response.ApiResponse;
Expand Down Expand Up @@ -36,8 +33,7 @@ public ApiResponse getReviewReservationList(@PathVariable(name = "modelId") Long
@Operation(summary = "리뷰 작성", description = "리뷰를 작성하는 API입니다.")
@PostMapping()
public ApiResponse createReview(@RequestBody ReviewDto reviewDto){
reviewService.createReview(reviewDto);
return ApiResponse.SuccessResponse(SuccessStatus.REVIEW_CREATE);
return ApiResponse.SuccessResponse(SuccessStatus.REVIEW_CREATE, reviewService.createReview(reviewDto));
}

@Operation(summary = "리뷰 세부 조회", description = "리뷰를 세부 조회하는 API입니다.")
Expand All @@ -54,13 +50,13 @@ public ApiResponse getMyReview(@PathVariable(name = "modelId") Long modelId){

@Operation(summary = "리뷰 수정", description = "모델이 작성한 리뷰를 수정하는 API입니다.")
@PatchMapping()
public ApiResponse patchReview(@RequestBody PatchReviewDto patchReviewDto){
return ApiResponse.SuccessResponse(SuccessStatus.REVIEW_PATCH, reviewService.patchReview(patchReviewDto));
public ApiResponse updateReview(@RequestBody UpdateReviewDto updateReviewDto){
return ApiResponse.SuccessResponse(SuccessStatus.REVIEW_PATCH, reviewService.updateReview(updateReviewDto));
}

@Operation(summary = "리뷰 삭제", description = "모델이 작성한 리뷰를 삭제하는 API입니다.")
@DeleteMapping()
public ApiResponse updateReview(@RequestBody DeleteReviewDto reviewDto){
public ApiResponse deleteReview(@RequestBody DeleteReviewDto reviewDto){
reviewService.deleteReview(reviewDto);
return ApiResponse.SuccessResponse(SuccessStatus.REVIEW_DELETE);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,18 @@

import jakarta.validation.constraints.NotBlank;
import lombok.*;
import umc.meme.shop.domain.review.dto.response.ReviewImgDto;

import java.util.List;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class PatchReviewDto {
public class UpdateReviewDto {
@NotBlank(message = "modelId를 입력해주세요")
private Long modelId;
@NotBlank(message = "reviewId를 입력해주세요")
private Long reviewId;
private int star;
private String comment;
private List<ReviewImgDto> reviewImgList;
private List<String> reviewImgSrcList;
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,11 @@
public class ReviewImgDto {
private Long reviewImgId;
private String reviewImgSrc;
private boolean isDelete = false;

public static ReviewImgDto from(ReviewImg img){
return ReviewImgDto.builder()
.reviewImgId(img.getReviewImgId())
.reviewImgSrc(img.getSrc())
.isDelete(false)
.build();
}

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/umc/meme/shop/domain/review/entity/Review.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
import umc.meme.shop.domain.common.BaseEntity;
import umc.meme.shop.domain.model.entity.Model;
import umc.meme.shop.domain.portfolio.entity.Portfolio;
import umc.meme.shop.domain.review.dto.request.PatchReviewDto;
import umc.meme.shop.domain.review.dto.request.ReviewDto;
import umc.meme.shop.domain.review.dto.request.UpdateReviewDto;
import umc.meme.shop.domain.user.User;

import java.util.ArrayList;
Expand Down Expand Up @@ -42,7 +42,7 @@ public class Review extends BaseEntity {
@Column(nullable = true, length = 200)
private String comment;

public void updateReview(PatchReviewDto patchReviewDto){
public void updateReview(UpdateReviewDto patchReviewDto){
if(patchReviewDto.getStar() > 0)
this.star = patchReviewDto.getStar();
if(patchReviewDto.getComment() != null)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package umc.meme.shop.domain.review.repository;

import org.springframework.data.jpa.repository.JpaRepository;
import umc.meme.shop.domain.review.entity.Review;
import umc.meme.shop.domain.review.entity.ReviewImg;

import java.util.Optional;

public interface ReviewImgRepository extends JpaRepository<ReviewImg, Long> {
Optional<ReviewImg> findBySrcAndReview(String src, Review review);
}
Loading

0 comments on commit 69b8648

Please sign in to comment.