Skip to content

Commit

Permalink
refactor: 분양희망자가 보는 분양후기 수정 로직 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
Suxxxxhyun committed Jan 24, 2024
1 parent f5ad3d6 commit 17bad17
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,6 @@ public class Review {
@JoinColumn(name = "adopter_id")
private Adopter adopter;

@OneToMany(mappedBy = "review", cascade = CascadeType.ALL, orphanRemoval = true)
@OneToMany(mappedBy = "review", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
private List<ReviewImgFile> images = new ArrayList<>();
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class ReviewImgFile {
private String originalFileName;
private String fileName;
private String fileUrl;
@ManyToOne(fetch = FetchType.LAZY)
@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinColumn(name = "review_id")
private Review review;
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ public static class ReviewUpdateRequestDto {
@Length(max = 3000, message = "내용은 최대 3000자까지 입력 가능합니다.")
@Schema(description = "내용", example = "정성스럽게 키우곘습니다!")
private String content;
@Schema(description = "이미지 삭제 여부")
private boolean deleteImages;
@NotEmpty(message = "이미지는 필수 입력값입니다.")
@Schema(description = "분양 후기 이미지 리스트")
@JsonSerialize(using = ToStringSerializer.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@

import com.example.petree.domain.adopter.domain.Review;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

/**
* packageName : com.example.petree.domain.adopter.repository
Expand All @@ -18,4 +22,8 @@

@Repository
public interface ReviewRepository extends JpaRepository<Review, Long>, ReviewRepositoryCustom {
@Transactional
@Modifying
@Query("DELETE FROM ReviewImgFile WHERE review = :review")
void deleteReviewImgFilesByReview(@Param("review") Review review);
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import com.example.petree.global.util.FileUtil;
import com.example.petree.global.util.S3Util;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
Expand All @@ -39,6 +40,7 @@

@Service
@RequiredArgsConstructor
@Slf4j
public class ReviewService {

private final ReviewRepository reviewRepository;
Expand Down Expand Up @@ -67,10 +69,26 @@ public void update(ReviewDto.ReviewUpdateRequestDto request, Review review, Adop
review.setContent(request.getContent());
}

if (request.isDeleteImages()) {
deleteReviewImages(review);
}

updateReviewImages(request.getReviewImgFiles(), review);

// 리뷰 저장
reviewRepository.save(review);
}

private void deleteReviewImages(Review review) {
// 기존 이미지 삭제
reviewRepository.deleteReviewImgFilesByReview(review);
}

private void updateReviewImages(List<MultipartFile> newImages, Review review) {
// 이미지 리스트를 수정하려면 이전 이미지 리스트를 제거하고 새로운 이미지를 추가
if (request.getReviewImgFiles() != null && !request.getReviewImgFiles().isEmpty()) {
List<ReviewImgFile> newImages = new ArrayList<>();
for (MultipartFile file : request.getReviewImgFiles()) {
List<ReviewImgFile> images = new ArrayList<>();
if (newImages != null && !newImages.isEmpty()) {
for (MultipartFile file : newImages) {
String originalFilename = file.getOriginalFilename();
String fileName = UUID.randomUUID().toString() + "." + fileUtil.extractExt(originalFilename);
String fileUrl = s3Util.upload(file, "review-img", fileName);
Expand All @@ -79,17 +97,19 @@ public void update(ReviewDto.ReviewUpdateRequestDto request, Review review, Adop
.originalFileName(file.getOriginalFilename())
.fileName(fileName)
.fileUrl(fileUrl)
.review(review)
.build();

reviewImgFileRepository.save(imgFile);
newImages.add(imgFile);
images.add(imgFile);
}
review.setImages(newImages);
// 이미지 저장 및 리뷰에 새로운 이미지 설정
review.setImages(images);
}

reviewRepository.save(review);
}




/**
* @author 박수현
* @date 2023-08-29
Expand Down
9 changes: 9 additions & 0 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ spring:
# spring banner off
main:
banner-mode: "off"
jpa:
show-sql: true

mvc:
pathmatch:
Expand Down Expand Up @@ -39,5 +41,12 @@ springdoc:
- com.example.petree.domain.breeder.controller
- com.example.petree.domain.main_breed.controller
- com.example.petree.domain.basic_test.controller
- com.example.petree.domain.adopter.controller

logging:
level:
org:
hibernate:
SQL: DEBUG


0 comments on commit 17bad17

Please sign in to comment.