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; 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.HashMap; import java.util.List; import java.util.Map; @Path("/users/{user-id}/notifications") @Component public class NotificationsResource { private final UserRepository userRepository; private final NotificationRepository notificationRepository; //finalによりインスタンス作成後に再代入不可,不変オブジェクト @Autowired public NotificationsResource(UserRepository userRepository, NotificationRepository notificationRepository) { this.userRepository = userRepository; this.notificationRepository = notificationRepository; } @GET @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.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<Notification> notifications = notificationRepository.getNotificationsForUser(userID); return Response.ok(notifications).build(); } //テスト用 @POST @Consumes(MediaType.APPLICATION_FORM_URLENCODED) public Response addNotification(@PathParam("user-id") String userID, @FormParam("from") String from, @FormParam("text") String text, @FormParam("time") String time) { if (userID == null || userID.isEmpty()) { var response = Response.status(Response.Status.BAD_REQUEST).entity("404"); throw new WebApplicationException(response.build()); } Notification notification = notificationRepository.addNotification(userID, from, text, time); return Response.ok(notification).build(); } //通知詳細を取得 @Path("/{notification-id}") @GET @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.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()); } 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 Response.ok(notification).build(); } //通知本文だけを返す @Path("/{notification-id}/text") @GET @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.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()); } Notification notification = notificationRepository.getNotification(userID, notificationID); if (notification == null) { var response = Response.status(Response.Status.NOT_FOUND).entity("通知が存在しません"); //404 throw new WebApplicationException(response.build()); } //HashMapクラスのインスタンスを生成(キーの順序を保持しない) Map<String, String> response = new HashMap<>(); response.put("text", notification.getText()); //Mapにキーと値のペアを追加 //テキストをキーでMapに格納し、レスポンスに使用 return Response.ok(response).build(); } //通知日時だけを返す @Path("/{notification-id}/time") @GET @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.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()); } 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<String, String> response = new HashMap<>(); response.put("time", notification.getTime()); return Response.ok(response).build(); } //通知送信者だけを返す @Path("/{notification-id}/from") @GET @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.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()); } 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<String, String> response = new HashMap<>(); response.put("from", notification.getFrom()); return Response.ok(response).build(); } }