diff --git a/apps/api/src/main/java/com/mally/api/pastebin/repositories/PastebinRepository.java b/apps/api/src/main/java/com/mally/api/pastebin/repositories/PastebinRepository.java index 237239b..5ece0e6 100644 --- a/apps/api/src/main/java/com/mally/api/pastebin/repositories/PastebinRepository.java +++ b/apps/api/src/main/java/com/mally/api/pastebin/repositories/PastebinRepository.java @@ -1,12 +1,22 @@ package com.mally.api.pastebin.repositories; import com.mally.api.pastebin.entities.Paste; +import jakarta.transaction.Transactional; +import org.springframework.data.jpa.repository.Modifying; +import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.CrudRepository; +import org.springframework.data.repository.query.Param; import org.springframework.stereotype.Repository; +import java.time.ZonedDateTime; import java.util.Optional; @Repository public interface PastebinRepository extends CrudRepository { Optional findBySlug(String slug); + + @Transactional + @Modifying + @Query("DELETE FROM Paste p WHERE p.expiresAt < :now") + void deleteExpiredPastes(@Param("now") ZonedDateTime now); } diff --git a/apps/api/src/main/java/com/mally/api/pastebin/services/PastebinService.java b/apps/api/src/main/java/com/mally/api/pastebin/services/PastebinService.java index bb3c8bc..ebc9994 100644 --- a/apps/api/src/main/java/com/mally/api/pastebin/services/PastebinService.java +++ b/apps/api/src/main/java/com/mally/api/pastebin/services/PastebinService.java @@ -35,4 +35,8 @@ public Paste create(CreatePasteDTO dto) { return pastebinRepository.save(paste); } + + public void deleteExpiredPastes() { + pastebinRepository.deleteExpiredPastes(ZonedDateTime.now()); + } } diff --git a/apps/api/src/main/java/com/mally/api/shared/schedulers/ExpiredRecordsPurger.java b/apps/api/src/main/java/com/mally/api/shared/schedulers/ExpiredRecordsPurger.java new file mode 100644 index 0000000..ada491b --- /dev/null +++ b/apps/api/src/main/java/com/mally/api/shared/schedulers/ExpiredRecordsPurger.java @@ -0,0 +1,38 @@ +package com.mally.api.shared.schedulers; + + +import com.mally.api.pastebin.services.PastebinService; +import com.mally.api.urlshortener.services.UrlShortenerService; +import lombok.AllArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.scheduling.annotation.EnableScheduling; +import org.springframework.scheduling.annotation.Scheduled; +import org.springframework.stereotype.Component; + +@EnableScheduling +@AllArgsConstructor +@Slf4j +@Component +public class ExpiredRecordsPurger { + static private final String AT_MIDNIGHT_CRON = "0 0 * * * *"; + + private final UrlShortenerService urlShortenerService; + + private final PastebinService pastebinService; + + @Scheduled(cron = AT_MIDNIGHT_CRON) + public void purgeExpiredRecords() { + deleteExpiredURLs(); + deleteExpiredPastes(); + + log.info("Expired records purged."); + } + + private void deleteExpiredURLs() { + urlShortenerService.deleteExpiredURLs(); + } + + private void deleteExpiredPastes() { + pastebinService.deleteExpiredPastes(); + } +} diff --git a/apps/api/src/main/java/com/mally/api/urlshortener/repositories/UrlShortenerRepository.java b/apps/api/src/main/java/com/mally/api/urlshortener/repositories/UrlShortenerRepository.java index 34700ca..5d0267e 100644 --- a/apps/api/src/main/java/com/mally/api/urlshortener/repositories/UrlShortenerRepository.java +++ b/apps/api/src/main/java/com/mally/api/urlshortener/repositories/UrlShortenerRepository.java @@ -1,12 +1,22 @@ package com.mally.api.urlshortener.repositories; import com.mally.api.urlshortener.entities.Url; +import jakarta.transaction.Transactional; +import org.springframework.data.jpa.repository.Modifying; +import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.CrudRepository; +import org.springframework.data.repository.query.Param; import org.springframework.stereotype.Repository; +import java.time.ZonedDateTime; import java.util.Optional; @Repository public interface UrlShortenerRepository extends CrudRepository { Optional findBySlug(String slug); + + @Transactional + @Modifying + @Query("DELETE FROM Url u WHERE u.expiresAt < :now") + void deleteExpiredURLs(@Param("now") ZonedDateTime now); } diff --git a/apps/api/src/main/java/com/mally/api/urlshortener/services/UrlShortenerService.java b/apps/api/src/main/java/com/mally/api/urlshortener/services/UrlShortenerService.java index 3ce8f9a..1b38a67 100644 --- a/apps/api/src/main/java/com/mally/api/urlshortener/services/UrlShortenerService.java +++ b/apps/api/src/main/java/com/mally/api/urlshortener/services/UrlShortenerService.java @@ -43,4 +43,8 @@ public Url save(ShortenUrlDTO dto) { return urlShortenerRepository.save(url); } + + public void deleteExpiredURLs() { + urlShortenerRepository.deleteExpiredURLs(ZonedDateTime.now()); + } }