Newer
Older
CosmosServer / src / main / java / com / example / cosmos_serversb / resources / UsersRest.java
package com.example.cosmos_serversb.resources;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.stereotype.Component;

import javax.ws.rs.*;

import com.example.cosmos_serversb.models.*;
import com.example.cosmos_serversb.views.UsersView;

import com.example.common.LogUtils;

@Component

@Path("/users")
public class UsersRest {
    
    @POST
    public String createUsers(@FormParam("name") String name, @FormParam("pw") String pw, @FormParam("iconImage") String iconImage) throws JsonProcessingException {
        SessionManager.getInstance().getSession();
        ObjectMapper mapper = new ObjectMapper();
        String json = mapper.writeValueAsString(Users.getInstance().createUser(name, pw, iconImage));
        SessionManager.getInstance().closeSession();
        LogUtils.info(name+" さんのアカウント作成が完了しました。");
        return json;
    }
    
    @Path("/{uId}")
    @GET
    public String getUsersInfo(@PathParam("uId") String uId, @QueryParam("token") String token) throws JsonProcessingException {
        SessionManager.getInstance().getSession();
        ObjectMapper mapper = new ObjectMapper();
        String json = mapper.writerWithView(UsersView.getUsersView.class).writeValueAsString(Users.getInstance().getUserById(uId));
        SessionManager.getInstance().closeSession();
        LogUtils.info("uId:"+uId+" の方のデータを送信します");
        return json;
    }

    @Path("/{uId}")
    @PUT
    public String setUsersInfo(@PathParam("uId") String uId, @FormParam("token") String token, @FormParam("name") String name, @FormParam("pw") String pw, @FormParam("iconImage") String iconImage) throws JsonProcessingException {
        SessionManager.getInstance().getSession();
        ObjectMapper mapper = new ObjectMapper();
        String json = mapper.writeValueAsString(Users.getInstance().setUser(uId, name, pw, iconImage));
        SessionManager.getInstance().closeSession();
        LogUtils.info("uId:"+uId+" の方がプロフィールを変更しました。");
        return json;
    }

    @Path("/{uId}")
    @DELETE
    public String deleteUsersInfo(@PathParam("uId") String uId, @FormParam("token") String token) throws JsonProcessingException {
        SessionManager.getInstance().getSession();
        ObjectMapper mapper = new ObjectMapper();
        String json = mapper.writeValueAsString(Users.getInstance().deleteUser(uId,token));
        SessionManager.getInstance().closeSession();
        LogUtils.info("uId:"+uId+" ユーザを削除しました");
        return json;
    }

    @Path("/{uId}/login")
    @POST
    public String login(@PathParam("uId") String uId, @FormParam("pw") String pw) throws JsonProcessingException {
        SessionManager.getInstance().getSession();
        ObjectMapper mapper = new ObjectMapper();
        String json = mapper.writeValueAsString(Users.getInstance().login(uId,pw));
        SessionManager.getInstance().closeSession();
        LogUtils.info("uId:"+uId+" ログインしました");
        return json;
    }

    @Path("/{uId}/logout")
    @DELETE
    public String logout(@PathParam("uId") String uId, @FormParam("token") String token) throws JsonProcessingException {
        SessionManager.getInstance().getSession();
        ObjectMapper mapper = new ObjectMapper();
        String json = mapper.writeValueAsString(Users.getInstance().logout(uId,token));
        SessionManager.getInstance().closeSession();
        LogUtils.info("uId:"+uId+" ログアウトしました");
        return json;
    }
}