diff --git a/src/main/java/org/ntlab/tampoposerver/resources/NotificationsResource.java b/src/main/java/org/ntlab/tampoposerver/resources/NotificationsResource.java index 15761b5..1e08b5e 100644 --- a/src/main/java/org/ntlab/tampoposerver/resources/NotificationsResource.java +++ b/src/main/java/org/ntlab/tampoposerver/resources/NotificationsResource.java @@ -1,6 +1,7 @@ package org.ntlab.tampoposerver.resources; import jakarta.ws.rs.*; +import jakarta.ws.rs.core.MediaType; import jakarta.ws.rs.core.Response; import org.ntlab.tampoposerver.models.Notification; import org.ntlab.tampoposerver.models.User; @@ -8,15 +9,15 @@ import org.ntlab.tampoposerver.repositories.UserRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; - +import java.util.HashMap; import java.util.List; -//import java.util.Map; +import java.util.Map; -@Path("/{user-id}/notifications") +@Path("/users/{user-id}/notifications") @Component public class NotificationsResource { - private UserRepository userRepository = null; - private NotificationRepository notificationRepository = null; + private final UserRepository userRepository; + private final NotificationRepository notificationRepository; //finalによりインスタンス作成後に再代入不可,不変オブジェクト @Autowired public NotificationsResource(UserRepository userRepository, NotificationRepository notificationRepository) { @@ -26,30 +27,48 @@ @GET - public String getNotifications(@PathParam("user-id") String userID, @QueryParam("token") String token) { + @Produces(MediaType.APPLICATION_JSON) + public Response getNotifications(@PathParam("user-id") String userID, @QueryParam("token") String token) { User user = userRepository.getUser(userID); + if (userID == null || userID.isEmpty()) { //userIDがnullまたは空文字列("")であるかをチェック + var response = Response.status(Response.Status.BAD_REQUEST).entity("不正なリクエスト"); //400 + throw new WebApplicationException(response.build()); + } 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())) { + if (token == null || token.isEmpty()) { + var response = Response.status(Response.Status.UNAUTHORIZED).entity("認証トークンがありません"); //401 + throw new WebApplicationException(response.build()); + } + if (!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) + "\"]}"; + List notifications = notificationRepository.getNotificationsForUser(userID); + return Response.ok(notifications).build(); } @Path("/{notification-id}") @GET - public String getNotification(@PathParam("user-id") String userID, @PathParam("notification-id") String notificationID, @QueryParam("token") String token) { + @Produces(MediaType.APPLICATION_JSON) + public Response getNotification(@PathParam("user-id") String userID, @PathParam("notification-id") String notificationID, @QueryParam("token") String token) { User user = userRepository.getUser(userID); + if (userID == null || userID.isEmpty() || notificationID == null || notificationID.isEmpty()) { + var response = Response.status(Response.Status.BAD_REQUEST).entity("不正なリクエスト"); //400 + throw new WebApplicationException(response.build()); + } 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())) { + if (token == null || token.isEmpty()) { + var response = Response.status(Response.Status.UNAUTHORIZED).entity("認証トークンがありません"); //401 + throw new WebApplicationException(response.build()); + } + if (!token.equals(user.getToken())) { var response = Response.status(Response.Status.FORBIDDEN).entity("トークンが不正です");//403 throw new WebApplicationException(response.build()); } @@ -58,24 +77,27 @@ 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形式の文字列で返す + return Response.ok(notification).build(); } @Path("/{notification-id}/text") //通知本文だけを返す @GET - public String getNotificationText(@PathParam("user-id") String userID, @PathParam("notification-id") String notificationID, @QueryParam("token") String token) { + @Produces(MediaType.APPLICATION_JSON) + public Response getNotificationText(@PathParam("user-id") String userID, @PathParam("notification-id") String notificationID, @QueryParam("token") String token) { User user = userRepository.getUser(userID); + if (userID == null || userID.isEmpty() || notificationID == null || notificationID.isEmpty()) { + var response = Response.status(Response.Status.BAD_REQUEST).entity("不正なリクエスト"); //400 + throw new WebApplicationException(response.build()); + } 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())) { + if (token == null || token.isEmpty()) { + var response = Response.status(Response.Status.UNAUTHORIZED).entity("認証トークンがありません"); //401 + throw new WebApplicationException(response.build()); + } + if (!token.equals(user.getToken())) { var response = Response.status(Response.Status.FORBIDDEN).entity("トークンが不正です");//403 throw new WebApplicationException(response.build()); } @@ -85,19 +107,29 @@ throw new WebApplicationException(response.build()); } - //Map response = Map.of("text", notification.getText()); - return notification.getText(); //文字列を直接返す + Map response = new HashMap<>(); //HashMapクラスのインスタンスを生成 + response.put("text", notification.getText()); //Mapにキーと値のペアを追加 + return Response.ok(response).build(); } @Path("/{notification-id}/time") //通知日時だけを返す @GET - public String getNotificationTime(@PathParam("user-id") String userID, @PathParam("notification-id") String notificationID, @QueryParam("token") String token) { + @Produces(MediaType.APPLICATION_JSON) + public Response getNotificationTime(@PathParam("user-id") String userID, @PathParam("notification-id") String notificationID, @QueryParam("token") String token) { User user = userRepository.getUser(userID); + if (userID == null || userID.isEmpty() || notificationID == null || notificationID.isEmpty()) { + var response = Response.status(Response.Status.BAD_REQUEST).entity("不正なリクエスト"); //400 + throw new WebApplicationException(response.build()); + } 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())) { + if (token == null || token.isEmpty()) { + var response = Response.status(Response.Status.UNAUTHORIZED).entity("認証トークンがありません"); //401 + throw new WebApplicationException(response.build()); + } + if (!token.equals(user.getToken())) { var response = Response.status(Response.Status.FORBIDDEN).entity("トークンが不正です");//403 throw new WebApplicationException(response.build()); } @@ -107,19 +139,29 @@ throw new WebApplicationException(response.build()); } - //Map response = Map.of("time", notification.getTime()); - return notification.getTime(); + Map response = new HashMap<>(); + response.put("time", notification.getText()); + return Response.ok(response).build(); } @Path("/{notification-id}/from") //通知送信者だけを返す @GET - public String getNotificationFrom(@PathParam("user-id") String userID, @PathParam("notification-id") String notificationID, @QueryParam("token") String token) { + @Produces(MediaType.APPLICATION_JSON) + public Response getNotificationFrom(@PathParam("user-id") String userID, @PathParam("notification-id") String notificationID, @QueryParam("token") String token) { User user = userRepository.getUser(userID); + if (userID == null || userID.isEmpty() || notificationID == null || notificationID.isEmpty()) { + var response = Response.status(Response.Status.BAD_REQUEST).entity("不正なリクエスト"); //400 + throw new WebApplicationException(response.build()); + } 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())) { + if (token == null || token.isEmpty()) { + var response = Response.status(Response.Status.UNAUTHORIZED).entity("認証トークンがありません"); //401 + throw new WebApplicationException(response.build()); + } + if (!token.equals(user.getToken())) { var response = Response.status(Response.Status.FORBIDDEN).entity("トークンが不正です");//403 throw new WebApplicationException(response.build()); } @@ -129,7 +171,8 @@ throw new WebApplicationException(response.build()); } - //Map response = Map.of("from", notification.getFrom()); - return notification.getFrom(); + Map response = new HashMap<>(); + response.put("from", notification.getText()); + return Response.ok(response).build(); } } \ No newline at end of file