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

import com.fasterxml.jackson.annotation.JsonTypeInfo;
import jakarta.ws.rs.*;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;
import org.ntlab.tampoposerver.models.FriendPair;
import org.ntlab.tampoposerver.repositories.UserRepository;
import org.ntlab.tampoposerver.services.FriendService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.AliasFor;
import org.springframework.stereotype.Component;

import java.util.UUID;

@Path("/friends")
@Component
public class FriendsResource {

    private UserRepository userRepository = null;
    private FriendService friendService = null;

    @Autowired
    public FriendsResource(UserRepository userRepository, FriendService friendService) {
        this.userRepository = userRepository;
        this.friendService = friendService;
    }




    @Path("/{pair-id}")
    @GET
    public Response getFriend(@QueryParam("token") String token, @PathParam("pair-id") int pairId) {
        //400

        //200
        FriendPair pair = friendService.getFriendPair(token, pairId);
        if (pair == null) {
            return Response.status(Response.Status.NOT_FOUND).build();
        }
        return Response.status(Response.Status.OK).build();
    }
    @POST
    @Consumes (MediaType.APPLICATION_FORM_URLENCODED)
    public void postFriends(@FormParam("token")String token,@FormParam("user0-id")String user0Id,@FormParam("user1-id")String user1Id){
        //400
        if (token.isBlank()){ //トークンが空文字だった時
            var response = Response.status(Response.Status.NOT_FOUND).entity("不正なリクエストです。");
            throw new WebApplicationException(response.build());
        }
        //401
        //if (!UserRepository.getUser()){ //トークンが通らなかった時
        //    var response = Response.status(Response.Status.NOT_FOUND).entity("トークンが無効です。");//404
        //    throw new WebApplicationException(response.build());
        //}

        //403

        //404
        //if (!friendService.createFriendPair(user0Id,user1Id){ //ユーザIDが変化した時、
        //    var response = Response.status(Response.Status.NOT_FOUND).entity("データが存在しません。");//404
        //    throw new WebApplicationException(response.build());
        //}


        //200

        //500は勝手にサーバがエラーでたらでるから書かない


        return;
    }
    @Path("/{pair-id}")
    @DELETE
    public Response deleteFriend(@QueryParam("token") String token, @PathParam("pair-id") Integer pairId) {
        //400
        if (token.isBlank()){ //トークンが空文字だった時
            var response = Response.status(Response.Status.NOT_FOUND).entity("不正なリクエストです。");
            throw new WebApplicationException(response.build());
        }

        //401
        //if (!userRepository.getUser()){ //トークンが通らなかった時
        //    var response = Response.status(Response.Status.NOT_FOUND).entity("トークンが無効です。");//404
        //    throw new WebApplicationException(response.build());
        //}
        //403ここで書く必要なし

        //404
        if (!friendService.removeFriendPair(token,pairId)){ //トークンに対応したユーザが選択したフレンドが存在しない時
            var response = Response.status(Response.Status.NOT_FOUND).entity("データが存在しないです。");//404
            throw new WebApplicationException(response.build());
        }

        //200
        return Response.status(Response.Status.OK).build();

        //500は勝手にサーバがエラーでたらでるから書かない
    }


}