Skip to content

Commit

Permalink
후원하기 게시글 만료 처리
Browse files Browse the repository at this point in the history
  • Loading branch information
JuseungL committed Aug 7, 2024
1 parent 7ad6704 commit ed78f10
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,4 @@ public ResponseEntity<ApiResponse<PresignedUrlResponse>> getPresignedUrl(
@Parameter(description = "파일 이름", required = true, example = "example.txt") @RequestParam(name = "fileName") String fileName) {
return ApiResponse.success(SuccessStatus.FILE_RETRIEVAL_SUCCESS, fileService.getPresignedUrl(prefix, fileName));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.fledge.fledgeserver.scheduler;

import com.fledge.fledgeserver.challenge.service.ChallengeParticipationService;
import com.fledge.fledgeserver.support.service.SupportService;
import lombok.RequiredArgsConstructor;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
@RequiredArgsConstructor
public class SupportExpirationScheduler {
private final SupportService supportService;

@Scheduled(cron = "0 0 0 * * ?") // 매일 자정에 만료된 게시글 처리
public void checkSupportPostExpiration() {
System.out.println("===START SupportPostExpirationScheduler===");
supportService.checkAndExpireSupportPosts();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class SupportController {

@Operation(summary = "후원하기 게시글 등록",
description = "후원하기 게시글을 등록합니다.(자립 준비 청소년만)\n" +
"\n" + // 추가적인 줄바꿈
"\n" +
"### promise\n" +
"[ONCE, WEEKLY, MONTHLY]" +
"\n" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,10 +150,15 @@ public void updateNotPending(PostUpdateRequest postUpdateRequest) {
this.zip = postUpdateRequest.getZip();
}



// 첫 후원하기 시에 후원 진행 중 처리 "IN_PROGRESS"
public void support() {
this.supportPostStatus = SupportPostStatus.IN_PROGRESS;
}

// 현재 시점이 만료 시점을 지남 후원 종료 처리 "TERMINATED"
public void setExpiration() { this.supportPostStatus = SupportPostStatus.TERMINATED; }

// 후원 물품 금액 달성 시 후원 완료 처리 "COMPLETED"
public void setCompleted() { this.supportPostStatus = SupportPostStatus.COMPLETED; }
}

Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ public enum SupportPostStatus {
COMPLETED("COMPLETED"), // 후원 금액 달성으로 종료
TERMINATED("TERMINATED"); // 후원 기간 말료로 종료


private final String key;
public String getKey() {
return key;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.fledge.fledgeserver.exception.ErrorCode;
import com.fledge.fledgeserver.support.entity.SupportCategory;
import com.fledge.fledgeserver.support.entity.SupportPost;
import com.fledge.fledgeserver.support.entity.SupportPostStatus;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
Expand Down Expand Up @@ -44,13 +45,14 @@ 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);

// 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();

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -294,4 +296,17 @@ public PostTotalPagingResponse deadlineApproachingPosts(int page) {
return new PostTotalPagingResponse((int) totalElements, totalPages, supportPosts);
}

@Transactional
public void checkAndExpireSupportPosts() {
LocalDate currentDate = LocalDate.now(); // 현재 날짜와 시간 가져오기
List<SupportPost> supportPosts = supportPostRepository.findAllBySupportPostStatusOr();
System.out.println("supportPosts.size() = " + supportPosts.size());

supportPosts.stream()
.filter(supportPost -> supportPost.getExpirationDate().isBefore(currentDate)) // expirationDate가 현재 날짜보다 이전인 경우
.forEach(supportPost -> {
supportPost.setExpiration(); // 만료 처리 메서드 호출
supportPostRepository.save(supportPost); // 변경 사항 저장
});
}
}

0 comments on commit ed78f10

Please sign in to comment.