diff --git a/src/main/java/org/ntlab/tampoposerver/resources/NotificationsResource.java b/src/main/java/org/ntlab/tampoposerver/resources/NotificationsResource.java index 19dbb52..15761b5 100644 --- a/src/main/java/org/ntlab/tampoposerver/resources/NotificationsResource.java +++ b/src/main/java/org/ntlab/tampoposerver/resources/NotificationsResource.java @@ -2,119 +2,134 @@ 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; import java.util.List; -import java.util.Map; +//import java.util.Map; -@Path("/{userID}/notifications") +@Path("/{user-id}/notifications") @Component public class NotificationsResource { private UserRepository userRepository = null; + private NotificationRepository notificationRepository = null; @Autowired - public NotificationsResource(UserRepository userRepo){ - userRepository = userRepo; + public NotificationsResource(UserRepository userRepository, NotificationRepository notificationRepository) { + this.userRepository = userRepository; + this.notificationRepository = notificationRepository; } @GET - public String getNotifications(@PathParam("userID") String userID, @QueryParam("token") String token) { - if (!UserRepository.getuserID().contains(userID)) { + 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 (!UserRepository.checkToken(userID, token)) { - var response = Response.status(Response.Status.FORBIDDEN).entity("トークンが不正です"); + 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 Response.ok(notifications).build(); + List notifications = List.of("notification-id1", "notification-id2", "notification-id3"); //サンプル + return "{\"notifications\": [\"" + String.join("\",\"", notifications) + "\"]}"; } - @Path("/{notificationID}") + @Path("/{notification-id}") @GET - public String getNotification(@PathParam("userID") String userID, @PathParam("notificationID") String notificationID, @QueryParam("token") String token){ - if (!UserRepository.getuserID().contains(userID,notificationID)) { + 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 (!UserRepository.checkToken(userID, token)) { - var response = Response.status(Response.Status.FORBIDDEN).entity("トークンが不正です"); + 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("通知が存在しません"); + var response = Response.status(Response.Status.NOT_FOUND).entity("通知が存在しません");//404 throw new WebApplicationException(response.build()); } - return Response.ok(notification).build(); + //Map response = Map.of("text", notification.getText()); + return notification.getText(); //文字列を直接返す } - @Path("/{notificationID}/text") + @Path("/{notification-id}/time") //通知日時だけを返す @GET - public String getNotificationText(@PathParam("userID") String userID, @PathParam("notificationID") String notificationID, @QueryParam("token") String token){ - if (!UserRepository.getuserID().contains(userID,notificationID)) { + 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 (!UserRepository.checkToken(userID, token)) { - var response = Response.status(Response.Status.FORBIDDEN).entity("トークンが不正です"); + 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("通知が存在しません"); + var response = Response.status(Response.Status.NOT_FOUND).entity("通知が存在しません");//404 throw new WebApplicationException(response.build()); } - Map response = Map.of("text", notification.text); - return Response.ok(response).build(); + //Map response = Map.of("time", notification.getTime()); + return notification.getTime(); } - @Path("/{notificationID}/time") + @Path("/{notification-id}/from") //通知送信者だけを返す @GET - public String getNotificationTime(@PathParam("userID") String userID, @PathParam("notificationID") String notificationID, @QueryParam("token") String token){ - if (!UserRepository.getuserID().contains(userID,notificationID)) { + 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 (!UserRepository.checkToken(userID, token)) { - var response = Response.status(Response.Status.FORBIDDEN).entity("トークンが不正です"); + 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("通知が存在しません"); + var response = Response.status(Response.Status.NOT_FOUND).entity("通知が存在しません");//404 throw new WebApplicationException(response.build()); } - Map response = Map.of("time", notification.time); - return Response.ok(response).build(); + //Map response = Map.of("from", notification.getFrom()); + return notification.getFrom(); } - } - - @Path("/{notificationID}/from") - @GET - public String getNotificationFrom(@PathParam("userID") String userID, @PathParam("notificationID") String notificationID, @QueryParam("token") String token) { - if (!UserRepository.getuserID().contains(userID, notificationID)) { - var response = Response.status(Response.Status.NOT_FOUND).entity("IDが存在しません");//404 - throw new WebApplicationException(response.build()); - } - if (!UserRepository.checkToken(userID, token)) { - var response = Response.status(Response.Status.FORBIDDEN).entity("トークンが不正です"); - throw new WebApplicationException(response.build()); - } - Notification notification = notificationRepository.getNotification(userID, notificationID); - if (notification == null) { - var response = Response.status(Response.Status.NOT_FOUND).entity("通知が存在しません"); - throw new WebApplicationException(response.build()); - } - - Map response = Map.of("from", notification.from); - return Response.ok(response).build(); - } +} \ No newline at end of file