diff --git a/src/main/java/org/ntlab/tampoposerver/repositories/NotificationRepository.java b/src/main/java/org/ntlab/tampoposerver/repositories/NotificationRepository.java index 0bc32e6..0c427de 100644 --- a/src/main/java/org/ntlab/tampoposerver/repositories/NotificationRepository.java +++ b/src/main/java/org/ntlab/tampoposerver/repositories/NotificationRepository.java @@ -3,21 +3,26 @@ import org.ntlab.tampoposerver.models.Notification; import org.springframework.stereotype.Repository; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.stream.Collectors; +import java.util.*; @Repository public class NotificationRepository { private HashMap> notificationMap = new HashMap<>(); //通知の追加 - public Notification addNotification(String userId, String notificationId, String from, String text, String time) { + public Notification addNotification(String userId, String from, String text, String time) { + String notificationId; + Map userNotifications = notificationMap.get(userId); + if (userNotifications == null) { + notificationId = Integer.toString(0); + } + else{ + notificationId = Integer.toString(userNotifications.size()); + } + Notification n = new Notification(userId, notificationId, from, text, time); - if(notificationMap.get(userId) == null) { + if(userNotifications == null) { notificationMap.put(userId, new HashMap<>()); } notificationMap.get(userId).put(notificationId, n); @@ -40,8 +45,36 @@ return new ArrayList<>(); } - return userNotifications.values().stream() - .sorted((a, b) -> b.getTime().compareTo(a.getTime())) - .collect(Collectors.toList()); + List sortedNotifications = new ArrayList<>(userNotifications.values()); + + Collections.sort(sortedNotifications, new Comparator() { + @Override + public int compare(Notification o1, Notification o2) { + if(o1.getTime().compareTo(o2.getTime()) < 0) { + return 1; + } + else if(o1.getTime().compareTo(o2.getTime()) > 0) { + return -1; + } + return 0; + } + }); + +// for(int i = 0; i < sortedNotifications.size() - 1; i++) { +// for(int j = i + 1; j < sortedNotifications.size(); j++) { +// if(sortedNotifications.get(i).getTime().compareTo(sortedNotifications.get(j).getTime()) < 0) { +// Notification tmp = sortedNotifications.get(i); +// sortedNotifications.set(i, sortedNotifications.get(j)); +// sortedNotifications.set(j, tmp); +// } +// } +// } + + return sortedNotifications; + +// ラムダ式 +// return userNotifications.values().stream() +// .sorted((a, b) -> b.getTime().compareTo(a.getTime())) +// .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 4f00ba7..2990edf 100644 --- a/src/test/java/org/ntlab/tampoposerver/repositories/NotificationRepositoryTest.java +++ b/src/test/java/org/ntlab/tampoposerver/repositories/NotificationRepositoryTest.java @@ -19,35 +19,39 @@ void contextLoads() throws JsonProcessingException { NotificationRepository notificationRepository = new NotificationRepository(); - Notification n1_1 = notificationRepository.addNotification("u01","n01","u00","telephone1","2025-06-10 10:00:11"); - Notification n1_2 = notificationRepository.addNotification("u01","n02","u02","telephone2","2025-06-07 10:31:11"); - Notification n1_3 = notificationRepository.addNotification("u01","n03","u02","telephone2","2025-06-07 12:09:22"); - Notification n2 = notificationRepository.addNotification("u02","n01","u00","telephone3","2025-06-07 09:22:12"); + Notification n1_1 = notificationRepository.addNotification("u01","u00","telephone1","2025-06-10 10:00:11"); + Notification n1_2 = notificationRepository.addNotification("u01","u02","telephone2","2025-06-07 10:31:11"); + Notification n1_3 = notificationRepository.addNotification("u01","u02","telephone2","2025-06-07 12:09:22"); + Notification n2 = notificationRepository.addNotification("u02","u00","telephone3","2025-06-07 09:22:12"); - Notification n = notificationRepository.getNotification("u01","n02"); + Notification n = notificationRepository.getNotification("u01","1"); assertEquals(n1_2.getText(),n.getText()); - n = notificationRepository.getNotification("u01","n04"); + n = notificationRepository.getNotification("u01","4"); assertNull(n); - n = notificationRepository.getNotification("u03","n03"); + n = notificationRepository.getNotification("u03","0"); assertNull(n); System.out.println(notificationRepository.getNotificationsForUser("u01")); + List sortednotifications = notificationRepository.getNotificationsForUser("u01"); + for (Notification notification : sortednotifications) { + System.out.println("time:" + notification.getTime() + " nid:" + notification.getNotificationId()); + } - System.out.println(notificationRepository.getNotification("u01","n01")); + System.out.println(notificationRepository.getNotification("u01","0")); //JSONに変換して表示するテスト ObjectMapper mapper = new ObjectMapper(); - mapper.registerModule(new JavaTimeModule()); // LocalDateTime対応 - mapper.findAndRegisterModules(); // 任意(自動登録) + mapper.registerModule(new JavaTimeModule()); + 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") + notificationRepository.getNotification("u01", "1") ); System.out.println(singleNotificationJson); }