Files

File: src/main/java/com/paymentlink/repository/ExchangeRateRepository.java

1 package com.paymentlink.repository;
2
3 import com.paymentlink.model.entity.ExchangeRate;
4 import org.springframework.data.jpa.repository.JpaRepository;
5 import org.springframework.data.jpa.repository.Query;
6 import org.springframework.data.repository.query.Param;
7 import org.springframework.stereotype.Repository;
8
9 import java.time.LocalDateTime;
10 import java.util.List;
11 import java.util.Optional;
12
13 @Repository
14 public interface ExchangeRateRepository extends JpaRepository<ExchangeRate, Long> {
15
16 @Query("SELECT e FROM ExchangeRate e WHERE e.fromCurrency = :from " +
17 "AND e.toCurrency = :to AND e.expiresAt > :now ORDER BY e.fetchedAt DESC")
18 Optional<ExchangeRate> findValidRate(
19 @Param("from") String fromCurrency,
20 @Param("to") String toCurrency,
21 @Param("now") LocalDateTime now);
22
23 Optional<ExchangeRate> findByFromCurrencyAndToCurrency(String fromCurrency, String toCurrency);
24
25 @Query("SELECT e FROM ExchangeRate e WHERE e.expiresAt < :now")
26 List<ExchangeRate> findExpiredRates(@Param("now") LocalDateTime now);
27
28 void deleteByExpiresAtBefore(LocalDateTime expiryDate);
29 }
30