From 3db7e1fdd412ca6477effe13e07b25f5f0fc9e92 Mon Sep 17 00:00:00 2001 From: JuseungL <121665437+JuseungL@users.noreply.github.com> Date: Fri, 9 Aug 2024 12:33:54 +0900 Subject: [PATCH 1/2] =?UTF-8?q?REFACTORING=20:=20=EB=A7=88=EA=B0=90=20?= =?UTF-8?q?=EC=9E=84=EB=B0=95=20=EA=B2=8C=EC=8B=9C=EA=B8=80=20=EB=B0=98?= =?UTF-8?q?=ED=99=98=20=EB=B0=A9=EC=8B=9D=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/PublicSupportController.java | 12 ++++----- .../repository/SupportImageRepository.java | 7 +++--- .../repository/SupportPostRepository.java | 9 +++++-- .../support/service/SupportService.java | 25 +++++++++++-------- 4 files changed, 30 insertions(+), 23 deletions(-) diff --git a/src/main/java/com/fledge/fledgeserver/support/controller/PublicSupportController.java b/src/main/java/com/fledge/fledgeserver/support/controller/PublicSupportController.java index 9ff8447..a07af29 100644 --- a/src/main/java/com/fledge/fledgeserver/support/controller/PublicSupportController.java +++ b/src/main/java/com/fledge/fledgeserver/support/controller/PublicSupportController.java @@ -2,6 +2,7 @@ import com.fledge.fledgeserver.response.ApiResponse; import com.fledge.fledgeserver.support.dto.response.PostGetResponse; +import com.fledge.fledgeserver.support.dto.response.PostPagingResponse; import com.fledge.fledgeserver.support.dto.response.PostTotalPagingResponse; import com.fledge.fledgeserver.support.dto.response.RecordProgressGetResponse; import com.fledge.fledgeserver.support.service.SupportService; @@ -67,12 +68,9 @@ public ResponseEntity> pagingSupportPost( } @Operation(summary = "마감 임박한 후원하기 게시글", - description = "4개씩 D-7까지 (limit=4, leftDays=7)") - @GetMapping("/deadline") - public ResponseEntity> deadlineApproachingPosts( - @RequestParam(defaultValue = "1") int page -// @RequestParam(defaultValue = "10") int limit // 무조건 4개 - ) { - return ApiResponse.success(GET_DEADLINE_APPROACHING_POST_SUCCESS, supportService.deadlineApproachingPosts(page-1)); + description = "4개씩 D-Day부터 D-7까지 한번에 리스트로 반환합니다.") + @GetMapping("/deadline-approaching") + public ResponseEntity>> deadlineApproachingPosts() { + return ApiResponse.success(GET_DEADLINE_APPROACHING_POST_SUCCESS, supportService.deadlineApproachingPosts()); } } diff --git a/src/main/java/com/fledge/fledgeserver/support/repository/SupportImageRepository.java b/src/main/java/com/fledge/fledgeserver/support/repository/SupportImageRepository.java index 914ca4f..28ce78b 100644 --- a/src/main/java/com/fledge/fledgeserver/support/repository/SupportImageRepository.java +++ b/src/main/java/com/fledge/fledgeserver/support/repository/SupportImageRepository.java @@ -14,14 +14,15 @@ public interface SupportImageRepository extends JpaRepository { @Query("SELECT si FROM SupportImage si WHERE si.supportPost.id = :supportPostId ORDER BY si.id ASC") - Optional findFirstImageBySupportPostId(@Param("supportPostId") Long supportPostId); + List findImagesBySupportPostId(@Param("supportPostId") Long supportPostId); default SupportImage findFirstImageBySupportPostIdOrDefault(Long supportPostId) { - return findFirstImageBySupportPostId(supportPostId).orElse(null); + List images = findImagesBySupportPostId(supportPostId); + return images.isEmpty() ? null : images.get(0); // Get the first image or null if empty } // Soft Delete 시 한방 쿼리 용 @Modifying - @Query("UPDATE SupportImage si SET si.deletedAt = :deletedAt WHERE si.id IN :ids") + @Query("UPDATE SupportImage si SET si.deletedAt = :deletedAt WHERE si.id IN (:ids)") void softDeleteByIds(@Param("ids") List ids, @Param("deletedAt") LocalDateTime deletedAt); } diff --git a/src/main/java/com/fledge/fledgeserver/support/repository/SupportPostRepository.java b/src/main/java/com/fledge/fledgeserver/support/repository/SupportPostRepository.java index dadfba4..3798f59 100644 --- a/src/main/java/com/fledge/fledgeserver/support/repository/SupportPostRepository.java +++ b/src/main/java/com/fledge/fledgeserver/support/repository/SupportPostRepository.java @@ -45,8 +45,13 @@ Page findByCategoryAndSearchAndSupportPostStatusWithImages(@Param(" @Param("q") String q, @Param("status") String status, Pageable pageable); - @Query("SELECT sp FROM SupportPost sp WHERE FUNCTION('DATEDIFF', sp.expirationDate, CURRENT_DATE) <= 7") - Page findByExpirationDateWithinSevenDays(Pageable pageable); + @Query("SELECT sp FROM SupportPost sp " + + "WHERE FUNCTION('DATEDIFF', sp.expirationDate, CURRENT_DATE) <= 7 " + + "AND (sp.supportPostStatus = :pending OR sp.supportPostStatus = :inProgress) " + + "ORDER BY sp.expirationDate ASC") + List findByExpirationDateWithinSevenDays(@Param("pending") SupportPostStatus pending, @Param("inProgress") SupportPostStatus inProgress); + + // IN 절보다 AND // List findAllBySupportPostStatusIn(List statuses); diff --git a/src/main/java/com/fledge/fledgeserver/support/service/SupportService.java b/src/main/java/com/fledge/fledgeserver/support/service/SupportService.java index d7f9c98..06ca146 100644 --- a/src/main/java/com/fledge/fledgeserver/support/service/SupportService.java +++ b/src/main/java/com/fledge/fledgeserver/support/service/SupportService.java @@ -29,6 +29,9 @@ import java.util.Map; import java.util.stream.Collectors; +import static com.fledge.fledgeserver.support.entity.SupportPostStatus.IN_PROGRESS; +import static com.fledge.fledgeserver.support.entity.SupportPostStatus.PENDING; + @Service @RequiredArgsConstructor public class SupportService { @@ -222,7 +225,7 @@ public void updateSupportPost(Long memberId, Long supportId, PostUpdateRequest p if (!supportPost.getMember().getId().equals(memberId)) { throw new CustomException(ErrorCode.NO_ACCESS); } - if (SupportPostStatus.PENDING.equals(supportPost.getSupportPostStatus())) { + if (PENDING.equals(supportPost.getSupportPostStatus())) { supportPost.updateAll(postUpdateRequestDto); clearAndUpdateImages(supportPost, postUpdateRequestDto); } else { @@ -277,23 +280,24 @@ public PostTotalPagingResponse pagingSupportPost(int page, String q, List supportPostPage = supportPostRepository.findByExpirationDateWithinSevenDays(pageable); + public List deadlineApproachingPosts() { + List supportPosts = supportPostRepository.findByExpirationDateWithinSevenDays(PENDING, IN_PROGRESS); - long totalElements = supportPostPage.getTotalElements(); - List supportPosts = supportPostPage.getContent().stream() + List supportPostsList = supportPosts.stream() .map(supportPost -> { int totalPrice = supportPost.getPrice(); - int supportedPrice = supportRecordRepository.sumSupportedPriceBySupportPostId(supportPost.getId()); + Long supportPostId = supportPost.getId(); + System.out.println("supportPostId = " + supportPostId); + int supportedPrice = supportRecordRepository.sumSupportedPriceBySupportPostId(supportPostId); RecordProgressGetResponse supportRecordProgress = new RecordProgressGetResponse(totalPrice, supportedPrice); - SupportImage supportImage = supportImageRepository.findFirstImageBySupportPostIdOrDefault(supportPost.getId()); + SupportImage supportImage = supportImageRepository.findFirstImageBySupportPostIdOrDefault(supportPostId); + String imageUrl = (supportImage != null) ? fileService.getFileUrl(supportImage.getImageUrl()) : null; // Set to null if no image found return new PostPagingResponse( - supportPost.getId(), + supportPostId, supportPost.getTitle(), supportPost.getExpirationDate(), imageUrl, @@ -301,9 +305,8 @@ public PostTotalPagingResponse deadlineApproachingPosts(int page) { ); }) .collect(Collectors.toList()); - int totalPages = supportPostPage.getTotalPages(); - return new PostTotalPagingResponse((int) totalElements, totalPages, supportPosts); + return supportPostsList; } @Transactional From 19ecc5dfdbf7bd75bbc651c039f599f7b56fe488 Mon Sep 17 00:00:00 2001 From: JuseungL <121665437+JuseungL@users.noreply.github.com> Date: Fri, 9 Aug 2024 12:37:25 +0900 Subject: [PATCH 2/2] =?UTF-8?q?REFACTORING=20:=20=EB=A7=88=EA=B0=90=20?= =?UTF-8?q?=EC=9E=84=EB=B0=95=20=EA=B2=8C=EC=8B=9C=EA=B8=80=20=EB=B0=98?= =?UTF-8?q?=ED=99=98=20=EB=B0=A9=EC=8B=9D=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../support/controller/PublicSupportController.java | 1 - .../fledge/fledgeserver/support/entity/SupportRecord.java | 2 +- .../support/repository/SupportImageRepository.java | 3 +-- .../support/repository/SupportPostRepository.java | 8 -------- 4 files changed, 2 insertions(+), 12 deletions(-) diff --git a/src/main/java/com/fledge/fledgeserver/support/controller/PublicSupportController.java b/src/main/java/com/fledge/fledgeserver/support/controller/PublicSupportController.java index a07af29..6b93fe0 100644 --- a/src/main/java/com/fledge/fledgeserver/support/controller/PublicSupportController.java +++ b/src/main/java/com/fledge/fledgeserver/support/controller/PublicSupportController.java @@ -63,7 +63,6 @@ public ResponseEntity> pagingSupportPost( @RequestParam(defaultValue = "") List category, // 카테고리 @RequestParam(defaultValue = "ing") String status ) { - // 응답에 이미지 포함 시키기 return ApiResponse.success(GET_SUPPORT_POST_PAGING_SUCCESS, supportService.pagingSupportPost(page-1, q, category, status)); } diff --git a/src/main/java/com/fledge/fledgeserver/support/entity/SupportRecord.java b/src/main/java/com/fledge/fledgeserver/support/entity/SupportRecord.java index 58f1e0c..72f5c30 100644 --- a/src/main/java/com/fledge/fledgeserver/support/entity/SupportRecord.java +++ b/src/main/java/com/fledge/fledgeserver/support/entity/SupportRecord.java @@ -38,7 +38,7 @@ public class SupportRecord extends BaseTimeEntity { @Column(nullable = false) private int amount; - @Column(name = "deleted_at") // 삭제 시각 저장 + @Column(name = "deleted_at") private LocalDateTime deletedAt; @Builder diff --git a/src/main/java/com/fledge/fledgeserver/support/repository/SupportImageRepository.java b/src/main/java/com/fledge/fledgeserver/support/repository/SupportImageRepository.java index 28ce78b..567be35 100644 --- a/src/main/java/com/fledge/fledgeserver/support/repository/SupportImageRepository.java +++ b/src/main/java/com/fledge/fledgeserver/support/repository/SupportImageRepository.java @@ -18,10 +18,9 @@ public interface SupportImageRepository extends JpaRepository images = findImagesBySupportPostId(supportPostId); - return images.isEmpty() ? null : images.get(0); // Get the first image or null if empty + return images.isEmpty() ? null : images.get(0); } - // Soft Delete 시 한방 쿼리 용 @Modifying @Query("UPDATE SupportImage si SET si.deletedAt = :deletedAt WHERE si.id IN (:ids)") void softDeleteByIds(@Param("ids") List ids, @Param("deletedAt") LocalDateTime deletedAt); diff --git a/src/main/java/com/fledge/fledgeserver/support/repository/SupportPostRepository.java b/src/main/java/com/fledge/fledgeserver/support/repository/SupportPostRepository.java index 3798f59..c9dc81e 100644 --- a/src/main/java/com/fledge/fledgeserver/support/repository/SupportPostRepository.java +++ b/src/main/java/com/fledge/fledgeserver/support/repository/SupportPostRepository.java @@ -15,9 +15,6 @@ import java.util.Optional; public interface SupportPostRepository extends JpaRepository { - /** - * 한방 쿼리: Fetch Join - */ @Query("SELECT s FROM SupportPost s " + "JOIN FETCH s.member m " + "LEFT JOIN FETCH s.images i " + @@ -51,11 +48,6 @@ Page findByCategoryAndSearchAndSupportPostStatusWithImages(@Param(" "ORDER BY sp.expirationDate ASC") List findByExpirationDateWithinSevenDays(@Param("pending") SupportPostStatus pending, @Param("inProgress") SupportPostStatus inProgress); - - - // IN 절보다 AND -// List findAllBySupportPostStatusIn(List statuses); - // JPQL을 사용하여 PENDING 또는 IN_PROGRESS 상태의 SupportPost를 찾는 메서드 @Query("SELECT sp FROM SupportPost sp WHERE sp.supportPostStatus = com.fledge.fledgeserver.support.entity.SupportPostStatus.PENDING " + "OR sp.supportPostStatus = com.fledge.fledgeserver.support.entity.SupportPostStatus.IN_PROGRESS") List findAllBySupportPostStatusOr();