-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20 from TingSHI2015/Add-Alarm-function-backend
Add alarm function backend
- Loading branch information
Showing
14 changed files
with
266 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
backend/src/main/java/com/github/tingshi2015/backend/reminder/NotificationService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.github.tingshi2015.backend.reminder; | ||
|
||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
public class NotificationService { | ||
public void sendNotification(Reminder reminder){ | ||
System.out.println("Reminder Alert: " + reminder.name() + " at " + reminder.time() + ", " + reminder.date()); | ||
} | ||
} |
1 change: 0 additions & 1 deletion
1
backend/src/main/java/com/github/tingshi2015/backend/reminder/Reminder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
backend/src/main/java/com/github/tingshi2015/backend/reminder/ReminderRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|
||
|
||
} |
20 changes: 20 additions & 0 deletions
20
backend/src/main/java/com/github/tingshi2015/backend/reminder/ReminderScheduler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package com.github.tingshi2015.backend.reminder; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.scheduling.annotation.Scheduled; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.util.List; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
public class ReminderScheduler { | ||
private final ReminderService reminderService; | ||
private final NotificationService notificationService; | ||
|
||
@Scheduled(fixedRate = 60000) //check every 60 seconds | ||
public void checkReminders(){ | ||
List<Reminder> upcomingReminders = reminderService.getUpcomingReminders(); | ||
upcomingReminders.forEach(notificationService::sendNotification); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
backend/src/test/java/com/github/tingshi2015/backend/reminder/ReminderSchedulerUnitTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package com.github.tingshi2015.backend.reminder; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import java.time.LocalDate; | ||
import java.time.LocalTime; | ||
import java.util.List; | ||
|
||
import static org.mockito.Mockito.*; | ||
|
||
class ReminderSchedulerUnitTest { | ||
ReminderService reminderService = mock(ReminderService.class); | ||
NotificationService notificationService = mock(NotificationService.class); | ||
ReminderScheduler reminderScheduler = new ReminderScheduler(reminderService, notificationService); | ||
|
||
|
||
@Test | ||
void checkReminders() { | ||
//GIVEN | ||
Reminder reminder1 = new Reminder("id1", "Drink Water!", LocalTime.now().withSecond(0).withNano(0), LocalDate.now()); | ||
Reminder reminder2 = new Reminder("id2", "Call Mama & Papa!",LocalTime.now().withSecond(0).withNano(0), LocalDate.now()); | ||
|
||
List<Reminder> reminders = List.of(reminder1, reminder2); | ||
|
||
when(reminderService.getUpcomingReminders()).thenReturn(reminders); | ||
//doNothing().when(notificationService).sendNotification(reminder1); //---can be omitted! | ||
//doNothing().when(notificationService).sendNotification(reminder2); //---can be omitted! | ||
|
||
//WHEN | ||
reminderScheduler.checkReminders(); | ||
|
||
//THEN | ||
verify(notificationService, times(1)).sendNotification(reminder1); | ||
verify(notificationService, times(1)).sendNotification(reminder2); | ||
|
||
|
||
} | ||
|
||
} | ||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import {useEffect, useState} from "react"; | ||
import {Reminder} from "../types/Reminder.ts"; | ||
import axios from "axios"; | ||
|
||
export default function UpcomingReminderGallery() { | ||
const [upcomingReminders, setUpcomingReminders] = useState<Reminder[]>([]); | ||
const [showUpcomingReminder, setShowUpcomingReminder] = useState<boolean>(false); | ||
|
||
useEffect(() => { | ||
|
||
getUpcomingReminders(); | ||
|
||
const interval = setInterval(() => { | ||
getUpcomingReminders(); | ||
}, 60000); | ||
|
||
return () => clearInterval(interval); //Cleanup the interval on unmount | ||
|
||
}, []); | ||
|
||
|
||
const getUpcomingReminders = () => { | ||
axios.get("/api/reminders/upcoming") | ||
.then(response => { | ||
if(response.data.length > 0) { | ||
//setUpcomingReminders(response.data); | ||
setUpcomingReminders(preReminders => [...preReminders, ...response.data]); | ||
setShowUpcomingReminder(true); | ||
} | ||
}) | ||
.catch(error => console.error("Error getting upcoming reminders", error)) | ||
.finally(() => {console.log("getUpcomingReminder_successful")}) | ||
} | ||
|
||
const handleClose = () => { | ||
setShowUpcomingReminder(false); | ||
setUpcomingReminders([]); | ||
} | ||
|
||
return( | ||
<div> | ||
{showUpcomingReminder && ( | ||
<div> | ||
<h3>Upcoming Reminders</h3> | ||
<ul> | ||
{upcomingReminders.map(reminder => ( | ||
<li key={reminder.id}> | ||
{reminder.name} at {reminder.date} {reminder.time} | ||
</li> | ||
))} | ||
</ul> | ||
<button onClick={handleClose}>Close</button> | ||
</div> | ||
) | ||
} | ||
</div> | ||
|
||
) | ||
|
||
} |
Oops, something went wrong.