Skip to content

Commit

Permalink
Merge pull request #33 from K-Hackathon-Fledge/32-DeadlineApproaching
Browse files Browse the repository at this point in the history
32 deadline approaching
LGTM👏🏻
  • Loading branch information
JuseungL authored Aug 9, 2024
2 parents ee80ed4 + 19ecc5d commit 2428b5b
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -62,17 +63,13 @@ public ResponseEntity<ApiResponse<PostTotalPagingResponse>> pagingSupportPost(
@RequestParam(defaultValue = "") List<String> category, // 카테고리
@RequestParam(defaultValue = "ing") String status
) {
// 응답에 이미지 포함 시키기
return ApiResponse.success(GET_SUPPORT_POST_PAGING_SUCCESS, supportService.pagingSupportPost(page-1, q, category, status));
}

@Operation(summary = "마감 임박한 후원하기 게시글",
description = "4개씩 D-7까지 (limit=4, leftDays=7)")
@GetMapping("/deadline")
public ResponseEntity<ApiResponse<PostTotalPagingResponse>> 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<ApiResponse<List<PostPagingResponse>>> deadlineApproachingPosts() {
return ApiResponse.success(GET_DEADLINE_APPROACHING_POST_SUCCESS, supportService.deadlineApproachingPosts());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@

public interface SupportImageRepository extends JpaRepository<SupportImage, Long> {
@Query("SELECT si FROM SupportImage si WHERE si.supportPost.id = :supportPostId ORDER BY si.id ASC")
Optional<SupportImage> findFirstImageBySupportPostId(@Param("supportPostId") Long supportPostId);
List<SupportImage> findImagesBySupportPostId(@Param("supportPostId") Long supportPostId);

default SupportImage findFirstImageBySupportPostIdOrDefault(Long supportPostId) {
return findFirstImageBySupportPostId(supportPostId).orElse(null);
List<SupportImage> images = findImagesBySupportPostId(supportPostId);
return images.isEmpty() ? null : images.get(0);
}

// 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<Long> ids, @Param("deletedAt") LocalDateTime deletedAt);
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@
import java.util.Optional;

public interface SupportPostRepository extends JpaRepository<SupportPost, Long> {
/**
* 한방 쿼리: Fetch Join
*/
@Query("SELECT s FROM SupportPost s " +
"JOIN FETCH s.member m " +
"LEFT JOIN FETCH s.images i " +
Expand Down Expand Up @@ -45,12 +42,12 @@ Page<SupportPost> 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<SupportPost> 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<SupportPost> findByExpirationDateWithinSevenDays(@Param("pending") SupportPostStatus pending, @Param("inProgress") SupportPostStatus inProgress);

// IN 절보다 AND
// List<SupportPost> findAllBySupportPostStatusIn(List<SupportPostStatus> 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<SupportPost> findAllBySupportPostStatusOr();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -277,33 +280,33 @@ public PostTotalPagingResponse pagingSupportPost(int page, String q, List<String


@Transactional(readOnly = true)
public PostTotalPagingResponse deadlineApproachingPosts(int page) {
PageRequest pageable = PageRequest.of(page, 4);
Page<SupportPost> supportPostPage = supportPostRepository.findByExpirationDateWithinSevenDays(pageable);
public List<PostPagingResponse> deadlineApproachingPosts() {
List<SupportPost> supportPosts = supportPostRepository.findByExpirationDateWithinSevenDays(PENDING, IN_PROGRESS);

long totalElements = supportPostPage.getTotalElements();
List<PostPagingResponse> supportPosts = supportPostPage.getContent().stream()
List<PostPagingResponse> 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,
supportRecordProgress
);
})
.collect(Collectors.toList());
int totalPages = supportPostPage.getTotalPages();

return new PostTotalPagingResponse((int) totalElements, totalPages, supportPosts);
return supportPostsList;
}

@Transactional
Expand Down

0 comments on commit 2428b5b

Please sign in to comment.