|
@@ -2,6 +2,7 @@ package com.lhamacorp.knotes.service;
|
|
|
|
|
|
|
|
import com.lhamacorp.knotes.domain.Note;
|
|
import com.lhamacorp.knotes.domain.Note;
|
|
|
import com.lhamacorp.knotes.repository.NoteRepository;
|
|
import com.lhamacorp.knotes.repository.NoteRepository;
|
|
|
|
|
+import com.lhamacorp.knotes.repository.PinRepository;
|
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.Logger;
|
|
|
import org.springframework.scheduling.annotation.Scheduled;
|
|
import org.springframework.scheduling.annotation.Scheduled;
|
|
|
import org.springframework.stereotype.Component;
|
|
import org.springframework.stereotype.Component;
|
|
@@ -15,27 +16,34 @@ import static org.slf4j.LoggerFactory.getLogger;
|
|
|
@Component
|
|
@Component
|
|
|
public class CleanupScheduler {
|
|
public class CleanupScheduler {
|
|
|
|
|
|
|
|
- private final NoteRepository repository;
|
|
|
|
|
|
|
+ private final NoteRepository noteRepository;
|
|
|
|
|
+ private final PinRepository pinRepository;
|
|
|
|
|
|
|
|
private static final String ONCE_PER_DAY_AT_2AM = "0 0 2 * * *";
|
|
private static final String ONCE_PER_DAY_AT_2AM = "0 0 2 * * *";
|
|
|
private static final Logger log = getLogger(CleanupScheduler.class);
|
|
private static final Logger log = getLogger(CleanupScheduler.class);
|
|
|
|
|
|
|
|
- public CleanupScheduler(NoteRepository repository) {
|
|
|
|
|
- this.repository = repository;
|
|
|
|
|
|
|
+ public CleanupScheduler(NoteRepository noteRepository, PinRepository pinRepository) {
|
|
|
|
|
+ this.noteRepository = noteRepository;
|
|
|
|
|
+ this.pinRepository = pinRepository;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
@Scheduled(cron = ONCE_PER_DAY_AT_2AM)
|
|
@Scheduled(cron = ONCE_PER_DAY_AT_2AM)
|
|
|
public void cleanup() {
|
|
public void cleanup() {
|
|
|
- List<String> ids = repository.findEmptyNotes()
|
|
|
|
|
|
|
+ List<String> ids = noteRepository.findEmptyNotes()
|
|
|
.stream()
|
|
.stream()
|
|
|
.filter(note -> note.createdAt().isBefore(now().minus(1, DAYS)))
|
|
.filter(note -> note.createdAt().isBefore(now().minus(1, DAYS)))
|
|
|
.map(Note::id)
|
|
.map(Note::id)
|
|
|
|
|
+ .filter(id -> !isPinned(id))
|
|
|
.toList();
|
|
.toList();
|
|
|
|
|
|
|
|
if (!ids.isEmpty()) {
|
|
if (!ids.isEmpty()) {
|
|
|
log.info("Cleaning empty notes [{}]", ids);
|
|
log.info("Cleaning empty notes [{}]", ids);
|
|
|
- repository.deleteAllById(ids);
|
|
|
|
|
|
|
+ noteRepository.deleteAllById(ids);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ private boolean isPinned(String noteId) {
|
|
|
|
|
+ return !pinRepository.findAllByNoteId(noteId).isEmpty();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
}
|
|
}
|