Newer
Older
tampopo-server / src / main / java / org / ntlab / tampoposerver / resources / NotificationsResource.java
package org.ntlab.tampoposerver.resources;

import jakarta.ws.rs.*;
import jakarta.ws.rs.core.Response;
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;

@Path("/{userID}/notifications")
@Component
public class NotificationsResource {
    private UserRepository userRepository = null;

    @Autowired
    public NotificationsResource(UserRepository userRepo){
        userRepository = userRepo;
    }


    @GET
    public String getNotifications(@PathParam("userID") String userID, @QueryParam("token") String token) {
        if (!UserRepository.getuserID().contains(userID)) {
            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());
        }

        List<String> notifications = List.of("notification-id1", "notification-id2", "notification-id3");
        return Response.ok(notifications).build();
    }

    @Path("/{notificationID}")
    @GET
    public String getNotification(@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());
        }

        return Response.ok(notification).build();
    }

    @Path("/{notificationID}/text")
    @GET
    public String getNotificationText(@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<String, String> response = Map.of("text", notification.text);
        return Response.ok(response).build();
    }

    @Path("/{notificationID}/time")
    @GET
    public String getNotificationTime(@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<String, String> response = Map.of("time", notification.time);
        return Response.ok(response).build();
    }
    }

    @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<String, String> response = Map.of("from", notification.from);
        return Response.ok(response).build();
    }