diff --git a/src/main/java/org/ntlab/tampoposerver/models/Notification.java b/src/main/java/org/ntlab/tampoposerver/models/Notification.java index ccefd39..115664b 100644 --- a/src/main/java/org/ntlab/tampoposerver/models/Notification.java +++ b/src/main/java/org/ntlab/tampoposerver/models/Notification.java @@ -1,18 +1,22 @@ package org.ntlab.tampoposerver.models; +import com.fasterxml.jackson.annotation.JsonIgnore; + public class Notification { + @JsonIgnore private String userId; + @JsonIgnore private String notificationId; private String from; private String text; private String time; public Notification(String u, String n, String f, String tx, String tm) { - userId = u; - notificationId = n; - from = f; - text = tx; - time = tm; + this.userId = u; + this.notificationId = n; + this.from = f; + this.text = tx; + this.time = tm; } public String getUserId() { diff --git a/src/main/java/org/ntlab/tampoposerver/repositories/NotificationRepository.java b/src/main/java/org/ntlab/tampoposerver/repositories/NotificationRepository.java index d0d1642..0bc32e6 100644 --- a/src/main/java/org/ntlab/tampoposerver/repositories/NotificationRepository.java +++ b/src/main/java/org/ntlab/tampoposerver/repositories/NotificationRepository.java @@ -6,6 +6,7 @@ import java.util.ArrayList; import java.util.HashMap; import java.util.List; +import java.util.Map; import java.util.stream.Collectors; @Repository @@ -33,14 +34,14 @@ } //ユーザーに対応した通知リストの取得(時刻降順) - public List getNotificationsForUser(String userId){ - if(notificationMap.get(userId) == null) { + public List getNotificationsForUser(String userId){ + Map userNotifications = notificationMap.get(userId); + if(userNotifications == null) { return new ArrayList<>(); } - return notificationMap.get(userId).values().stream() + return userNotifications.values().stream() .sorted((a, b) -> b.getTime().compareTo(a.getTime())) - .map(Notification::getNotificationId) .collect(Collectors.toList()); } } diff --git a/src/test/java/org/ntlab/tampoposerver/repositories/NotificationRepositoryTest.java b/src/test/java/org/ntlab/tampoposerver/repositories/NotificationRepositoryTest.java index 698f01f..4f00ba7 100644 --- a/src/test/java/org/ntlab/tampoposerver/repositories/NotificationRepositoryTest.java +++ b/src/test/java/org/ntlab/tampoposerver/repositories/NotificationRepositoryTest.java @@ -1,9 +1,14 @@ package org.ntlab.tampoposerver.repositories; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; import org.junit.jupiter.api.Test; import org.ntlab.tampoposerver.models.Notification; import org.springframework.boot.test.context.SpringBootTest; +import java.util.List; + import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNull; @@ -11,7 +16,7 @@ public class NotificationRepositoryTest { @Test - void contextLoads() { + void contextLoads() throws JsonProcessingException { NotificationRepository notificationRepository = new NotificationRepository(); Notification n1_1 = notificationRepository.addNotification("u01","n01","u00","telephone1","2025-06-10 10:00:11"); @@ -29,5 +34,21 @@ assertNull(n); System.out.println(notificationRepository.getNotificationsForUser("u01")); + + System.out.println(notificationRepository.getNotification("u01","n01")); + + //JSONに変換して表示するテスト + ObjectMapper mapper = new ObjectMapper(); + mapper.registerModule(new JavaTimeModule()); // LocalDateTime対応 + mapper.findAndRegisterModules(); // 任意(自動登録) + + List notifications = notificationRepository.getNotificationsForUser("u01"); + String notificationsJson = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(notifications); + System.out.println(notificationsJson); + + String singleNotificationJson = mapper.writerWithDefaultPrettyPrinter().writeValueAsString( + notificationRepository.getNotification("u01", "n01") + ); + System.out.println(singleNotificationJson); } }