package com.example.cosmos_serversb.resources; import com.example.cosmos_serversb.entities.Group; import com.example.cosmos_serversb.entities.User; import com.example.cosmos_serversb.jsons.*; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import org.hibernate.Session; import org.springframework.stereotype.Component; import javax.ws.rs.*; import com.example.cosmos_serversb.models.*; import java.io.IOException; import java.util.Date; @Component @Path("/groups") public class GroupsRest { @GET public String getGroupsListByUid(@QueryParam("uId") String uId, @QueryParam("token") String token) throws JsonProcessingException{ Session session = SessionManager.getInstance().getSession(); ObjectMapper mapper = new ObjectMapper(); String json = mapper.writeValueAsString(Groups.getInstance().getGroupsListByUid(uId)); SessionManager.getInstance().closeSession(); return json; } @POST public String createGroup(@FormParam("name") String name, @FormParam("uId") String uId, @FormParam("token") String token) throws IOException { //グループの作成 SessionManager.getInstance().getSession(); ObjectMapper mapper = new ObjectMapper(); Group group = Groups.getInstance().createGroup(name, uId); SessionManager.getInstance().closeSession();//一度データベースを更新。 //グループにメンバーの追加 SessionManager.getInstance().getSession(); Groups.addMember(group.getgId(), uId); SessionManager.getInstance().closeSession(); // String json = mapper.writeValueAsString(group);//このJSONには、グループを作成したユーザの情報が含まれていません。 Session session = SessionManager.getInstance().getSession(); // Group editedGroup = (Group) session.load(Group.class, group.getgId());//editedGroupの値がおかしい? // String json = mapper.writeValueAsString(editedGroup); GroupJson groupJson = new GroupJson(session.load(Group.class, group.getgId())); String json = mapper.writeValueAsString( groupJson ); SessionManager.getInstance().closeSession(); return json; } @Path("/{gId}") @GET public String getGroupByGid(@PathParam("gId") String gId, @QueryParam("token") String token) throws JsonProcessingException{ SessionManager.getInstance().getSession(); ObjectMapper mapper = new ObjectMapper(); String json = mapper.writeValueAsString(Groups.getInstance().getGroupByGid(gId)); SessionManager.getInstance().closeSession(); return json; } @Path("/{gId}") @DELETE public String deleteGroup(@PathParam("gId") String gId, @FormParam("token") String token) throws JsonProcessingException{ SessionManager.getInstance().getSession(); ObjectMapper mapper = new ObjectMapper(); String json = mapper.writeValueAsString(Groups.getInstance().deleteGroup(gId)); SessionManager.getInstance().closeSession(); return json; } @Path("/{gId}/members") @GET public String getGroupMembersListByGid(@PathParam("gId") String gId, @QueryParam("token") String token) throws JsonProcessingException{ SessionManager.getInstance().getSession(); ObjectMapper mapper = new ObjectMapper(); String json = mapper.writeValueAsString(Groups.getInstance().getMembersListByGid(gId)); SessionManager.getInstance().closeSession(); return json; } @Path("/{gId}/members") @POST public String addMember(@PathParam("gId") String gId, @FormParam("uId") String uId, @FormParam("token") String token) throws JsonProcessingException{ SessionManager.getInstance().getSession(); ObjectMapper mapper = new ObjectMapper(); Group editGroup = Groups.getInstance().addMember(gId, uId); String json = mapper.writeValueAsString(new GroupJson(editGroup)); SessionManager.getInstance().closeSession(); return json; } @Path("/{gId}/members") @DELETE public String deleteMember(@PathParam("gId") String gId, @FormParam("uId") String uId, @FormParam("token") String token) throws JsonProcessingException{ SessionManager.getInstance().getSession(); ObjectMapper mapper = new ObjectMapper(); String json = mapper.writeValueAsString(Groups.getInstance().deleteMember(gId, uId)); SessionManager.getInstance().closeSession(); return json; } // @Path("/{gId}/requests") // @GET // public String getRequestsListByGid(@PathParam("gId") String gId, @QueryParam("token") String token) throws JsonProcessingException{ // SessionManager.getInstance().getSession(); // ObjectMapper mapper = new ObjectMapper(); // String json = mapper.writeValueAsString(Groups.getInstance().getRequestsListByGid(gId)); // SessionManager.getInstance().closeSession(); // return json; // } @Path("/{gId}/requests") @GET public String getRequestsListByGid(@PathParam("gId") String gId, @QueryParam("token") String token, @QueryParam("detail") boolean detail, @QueryParam("quantity") int quantity) throws JsonProcessingException{ SessionManager.getInstance().getSession(); ObjectMapper mapper = new ObjectMapper(); String json = mapper.writeValueAsString(Groups.getInstance().getRequestsListByGid(gId, detail, quantity)); SessionManager.getInstance().closeSession(); return json; } @Path("/{gId}/requests") @POST public String addRequests(@PathParam("gId") String gId, @FormParam("uId") String uId, @FormParam("product") String product, @FormParam("deadline") String deadline, @FormParam("location") int location, @FormParam("token") String token) throws JsonProcessingException{ SessionManager.getInstance().getSession(); ObjectMapper mapper = new ObjectMapper(); String json = mapper.writeValueAsString(Groups.getInstance().addRequests(gId, uId, product, deadline, location)); SessionManager.getInstance().closeSession(); return json; } @Path("{gId}/requests/{rId}") @GET public String getRequestsDetailByGidAndRid(@PathParam("gId") String gId, @PathParam("rId") String rId, @QueryParam("token") String token) throws JsonProcessingException{ SessionManager.getInstance().getSession(); ObjectMapper mapper = new ObjectMapper(); String json = mapper.writeValueAsString(Groups.getInstance().getRequestDetailByGidAndRid(gId, rId)); SessionManager.getInstance().closeSession(); return json; } @Path("{gId}/requests/{rId}") @PUT public String updateRequest(@PathParam("gId") String gId, @PathParam("rId") String rId, @FormParam("uId") String uId, @FormParam("product") String product, @FormParam("deadline") String deadline, @FormParam("location") int location, @FormParam("done") boolean done, @FormParam("token") String token) throws JsonProcessingException{ SessionManager.getInstance().getSession(); ObjectMapper mapper = new ObjectMapper(); String json = mapper.writeValueAsString(Groups.getInstance().updateRequest(gId, rId, uId, product, deadline, location, done)); SessionManager.getInstance().closeSession(); return json; } @Path("{gId}/requests/{rId}") @DELETE public String deleteRequest(@PathParam("gId") String gId, @PathParam("rId") String rId, @QueryParam("token") String token) throws JsonProcessingException{ SessionManager.getInstance().getSession(); ObjectMapper mapper = new ObjectMapper(); String json = mapper.writeValueAsString(Groups.getInstance().deleteRequest(gId, rId)); SessionManager.getInstance().closeSession(); return json; } }