diff --git a/src/main/java/org/ntlab/tampoposerver/resources/NotificationsResource.java b/src/main/java/org/ntlab/tampoposerver/resources/NotificationsResource.java index eb96ea3..15761b5 100644 --- a/src/main/java/org/ntlab/tampoposerver/resources/NotificationsResource.java +++ b/src/main/java/org/ntlab/tampoposerver/resources/NotificationsResource.java @@ -1,13 +1,135 @@ package org.ntlab.tampoposerver.resources; -import jakarta.ws.rs.GET; -import jakarta.ws.rs.Path; +import jakarta.ws.rs.*; +import jakarta.ws.rs.core.Response; +import org.ntlab.tampoposerver.models.Notification; +import org.ntlab.tampoposerver.models.User; +import org.ntlab.tampoposerver.repositories.NotificationRepository; +import org.ntlab.tampoposerver.repositories.UserRepository; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; -@Path("/Users") +import java.util.List; +//import java.util.Map; + +@Path("/{user-id}/notifications") @Component public class NotificationsResource { - @GET - public String getHello(){return "Hello World"; + private UserRepository userRepository = null; + private NotificationRepository notificationRepository = null; + + @Autowired + public NotificationsResource(UserRepository userRepository, NotificationRepository notificationRepository) { + this.userRepository = userRepository; + this.notificationRepository = notificationRepository; } -} + + + @GET + public String getNotifications(@PathParam("user-id") String userID, @QueryParam("token") String token) { + User user = userRepository.getUser(userID); + if (user == null) { + var response = Response.status(Response.Status.NOT_FOUND).entity("IDが存在しません");//404 + throw new WebApplicationException(response.build()); + } + if (token == null || !token.equals(user.getToken())) { + var response = Response.status(Response.Status.FORBIDDEN).entity("トークンが不正です");//403 + throw new WebApplicationException(response.build()); + } + + List notifications = List.of("notification-id1", "notification-id2", "notification-id3"); //サンプル + return "{\"notifications\": [\"" + String.join("\",\"", notifications) + "\"]}"; + } + + @Path("/{notification-id}") + @GET + public String getNotification(@PathParam("user-id") String userID, @PathParam("notification-id") String notificationID, @QueryParam("token") String token) { + User user = userRepository.getUser(userID); + if (user == null) { + var response = Response.status(Response.Status.NOT_FOUND).entity("IDが存在しません");//404 + throw new WebApplicationException(response.build()); + } + if (token == null || !token.equals(user.getToken())) { + var response = Response.status(Response.Status.FORBIDDEN).entity("トークンが不正です");//403 + throw new WebApplicationException(response.build()); + } + Notification notification = notificationRepository.getNotification(userID, notificationID); //通知詳細を取得 + if (notification == null) { + var response = Response.status(Response.Status.NOT_FOUND).entity("通知が存在しません");//404 + throw new WebApplicationException(response.build()); + } + + return "{" + + "\"notificationId\":\"" + notification.getNotificationId() + "\"," + + "\"from\":\"" + notification.getFrom() + "\"," + + "\"text\":\"" + notification.getText() + "\"," + + "\"time\":\"" + notification.getTime() + "\"" + + "}"; //JSON形式の文字列で返す + } + + @Path("/{notification-id}/text") //通知本文だけを返す + @GET + public String getNotificationText(@PathParam("user-id") String userID, @PathParam("notification-id") String notificationID, @QueryParam("token") String token) { + User user = userRepository.getUser(userID); + if (user == null) { + var response = Response.status(Response.Status.NOT_FOUND).entity("IDが存在しません");//404 + throw new WebApplicationException(response.build()); + } + if (token == null || !token.equals(user.getToken())) { + var response = Response.status(Response.Status.FORBIDDEN).entity("トークンが不正です");//403 + throw new WebApplicationException(response.build()); + } + Notification notification = notificationRepository.getNotification(userID, notificationID); + if (notification == null) { + var response = Response.status(Response.Status.NOT_FOUND).entity("通知が存在しません");//404 + throw new WebApplicationException(response.build()); + } + + //Map response = Map.of("text", notification.getText()); + return notification.getText(); //文字列を直接返す + } + + @Path("/{notification-id}/time") //通知日時だけを返す + @GET + public String getNotificationTime(@PathParam("user-id") String userID, @PathParam("notification-id") String notificationID, @QueryParam("token") String token) { + User user = userRepository.getUser(userID); + if (user == null) { + var response = Response.status(Response.Status.NOT_FOUND).entity("IDが存在しません");//404 + throw new WebApplicationException(response.build()); + } + if (token == null || !token.equals(user.getToken())) { + var response = Response.status(Response.Status.FORBIDDEN).entity("トークンが不正です");//403 + throw new WebApplicationException(response.build()); + } + Notification notification = notificationRepository.getNotification(userID, notificationID); + if (notification == null) { + var response = Response.status(Response.Status.NOT_FOUND).entity("通知が存在しません");//404 + throw new WebApplicationException(response.build()); + } + + //Map response = Map.of("time", notification.getTime()); + return notification.getTime(); + } + + @Path("/{notification-id}/from") //通知送信者だけを返す + @GET + public String getNotificationFrom(@PathParam("user-id") String userID, @PathParam("notification-id") String notificationID, @QueryParam("token") String token) { + User user = userRepository.getUser(userID); + if (user == null) { + var response = Response.status(Response.Status.NOT_FOUND).entity("IDが存在しません");//404 + throw new WebApplicationException(response.build()); + } + if (token == null || !token.equals(user.getToken())) { + var response = Response.status(Response.Status.FORBIDDEN).entity("トークンが不正です");//403 + throw new WebApplicationException(response.build()); + } + Notification notification = notificationRepository.getNotification(userID, notificationID); + if (notification == null) { + var response = Response.status(Response.Status.NOT_FOUND).entity("通知が存在しません");//404 + throw new WebApplicationException(response.build()); + } + + //Map response = Map.of("from", notification.getFrom()); + return notification.getFrom(); + } +} \ No newline at end of file