Skip to content

Commit

Permalink
Merge pull request #58 from Donut-DONationUTile/feature/donation/giver
Browse files Browse the repository at this point in the history
feat: 자동 기부 실행
  • Loading branch information
Ganghee-Lee-0522 authored Apr 24, 2024
2 parents 5bd34fb + 6651a1d commit f9f1f79
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/main/java/zero/eight/donut/DonutApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
import org.springframework.scheduling.annotation.EnableScheduling;

@EnableScheduling
@EnableJpaAuditing
@SpringBootApplication
public class DonutApplication {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/zero/eight/donut/domain/enums/Status.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

@Getter
public enum Status {
SELFUSED("SELFUSED"),
SELF_USED("SELF_USED"),
STORED("STORED"),
REPORTED("REPORTED"),
UNUSED("UNUSED"),
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/zero/eight/donut/repository/GiftRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,8 @@ public interface GiftRepository extends JpaRepository<Gift, Long> {
int sumByNotAssigned();
@Query(value = "SELECT SUM(g.price) FROM gift g WHERE g.store = :storeName", nativeQuery = true)
int sumByStoreName(String storeName);

@Query(value = "SELECT * FROM gift g WHERE g.status = :status AND g.is_assigned = false" +
"AND g.due_date BETWEEN :startDate AND :endDate", nativeQuery = true)
List<Gift> findAllByNotAssignedAndStatusAndDueDateBetween(String status,LocalDateTime startDate, LocalDateTime endDate);
}
20 changes: 20 additions & 0 deletions src/main/java/zero/eight/donut/service/DonationService.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,39 @@

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import zero.eight.donut.common.response.ApiResponse;
import zero.eight.donut.domain.Gift;
import zero.eight.donut.domain.enums.Status;
import zero.eight.donut.dto.donation.DonateGiftRequestDto;
import zero.eight.donut.dto.donation.GiftboxRequestDto;
import zero.eight.donut.repository.GiftRepository;

import java.io.IOException;
import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@RequiredArgsConstructor
@Service
public class DonationService {

private final SerialDonationService donationService;
private final GiftRepository giftRepository;

@Async
@Transactional
@Scheduled(cron = "0 0 0 * * *")
public void autoDonate(){
List<Gift> giftList = giftRepository.findAllByNotAssignedAndStatusAndDueDateBetween( "STORED", LocalDateTime.now(), LocalDateTime.now().minusDays(30));
for (Gift gift : giftList) {
gift.updateStatus("UNUSED");
giftRepository.save(gift);
}
}

public synchronized ApiResponse<?> assignGiftbox(GiftboxRequestDto giftboxRequestDto) {
return donationService.assignGiftbox(giftboxRequestDto);
Expand Down

0 comments on commit f9f1f79

Please sign in to comment.