Skip to content

Commit

Permalink
Backend:
Browse files Browse the repository at this point in the history
- Add the idea with @query for Alarm
  • Loading branch information
TingSHI2015 committed Jul 5, 2024
1 parent a2a0c58 commit 25c66ca
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
package com.github.tingshi2015.backend.reminder;

import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.data.mongodb.repository.Query;
import org.springframework.stereotype.Repository;

import java.time.LocalDate;
import java.time.LocalTime;
import java.util.List;

@Repository
public interface ReminderRepository extends MongoRepository<Reminder, String> {

// @Query("{'date': ?0, 'time': {$gte: ?1, $lt: ?2}}")
// List<Reminder> findRemindersByDateAndTime(LocalDate date, LocalTime startTime, LocalTime endTime);


}
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ public class ReminderService {
// return new ReminderDTO(reminder.name(), reminder.time(),reminder.date());
// }

private Reminder convertToEntity(ReminderDTO reminderDTO){
private Reminder convertToEntity(ReminderDTO reminderDTO) {
String id = idService.randomId();
return new Reminder(id, reminderDTO.name(),reminderDTO.time(),reminderDTO.date());
return new Reminder(id, reminderDTO.name(), reminderDTO.time(), reminderDTO.date());
}

public List<Reminder> getAllReminders() {
Expand All @@ -35,14 +35,14 @@ public Reminder createAReminder(ReminderDTO reminderDTO) {
}

public void deleteAReminder(String id) {
if (!reminderRepository.existsById(id)){
if (!reminderRepository.existsById(id)) {
throw new NoSuchElementException("Reminder with id: " + id + " not found. Can't delete!");
}
reminderRepository.deleteById(id);
}

public Reminder updateAReminder(ReminderDTO updateReminder, String id) {
if(!reminderRepository.existsById(id)){
if (!reminderRepository.existsById(id)) {
throw new NoSuchElementException("Reminder with id: " + id + " not found. Can't update!");
}
Reminder reminderToUpdate = new Reminder(id, updateReminder.name(), updateReminder.time(), updateReminder.date());
Expand All @@ -55,6 +55,7 @@ public Reminder updateAReminder(ReminderDTO updateReminder, String id) {
// .orElseThrow(()-> new NoSuchElementException("Reminder with id: " + id + " not found. Can't getReminderById"));
// }

//----------inefficient method with "filter"!---------------
public List<Reminder> getUpcomingReminders() {
LocalDate today = LocalDate.now();
LocalTime now = LocalTime.now().withSecond(0).withNano(0); //ignore second & Nano-second
Expand All @@ -64,4 +65,16 @@ public List<Reminder> getUpcomingReminders() {
.filter(reminder -> reminder.date().equals(today) && reminder.time().withSecond(0).withNano(0).equals(now))
.collect(Collectors.toList());
}

//---------efficient method with "@Query"------------------
/*
public List<Reminder> getUpcomingReminders() {
LocalDate today = LocalDate.now();
LocalTime now = LocalTime.now().withSecond(0).withNano(0); //ignore second & Nano-second
LocalTime nextMinute = now.plusMinutes(1);
return reminderRepository.findRemindersByDateAndTime(today, now, nextMinute);
}
*/

}

0 comments on commit 25c66ca

Please sign in to comment.