Newer
Older
CitrusServer / src / main / java / org / ntlab / citrusserver / resources / TodoRest.java
  1. package org.ntlab.citrusserver.resources;
  2.  
  3. import jakarta.ws.rs.*;
  4. import jakarta.ws.rs.core.MediaType;
  5. import jakarta.ws.rs.core.Response;
  6. import org.apache.coyote.http11.upgrade.UpgradeServletOutputStream;
  7. import org.ntlab.citrusserver.entities.Todo;
  8. import org.ntlab.citrusserver.repositories.AccountManager;
  9. import org.ntlab.citrusserver.repositories.TodoManager;
  10. import org.springframework.beans.factory.annotation.Autowired;
  11. import org.springframework.stereotype.Component;
  12.  
  13. import java.util.HashMap;
  14.  
  15. @Path("/accounts")
  16. @Component
  17. public class TodoRest {
  18.  
  19. private final TodoManager todoManager;
  20. private final AccountManager accountManager;
  21.  
  22. @Autowired
  23. public TodoRest(TodoManager todoManager, AccountManager accountManager) {
  24. this.todoManager = todoManager;
  25. this.accountManager = accountManager;
  26. }
  27.  
  28. //test用
  29. @Path("/TodoTest")
  30. @GET
  31. @Produces(MediaType.TEXT_PLAIN)
  32. public String testHello() {
  33. return "hello";
  34. }
  35.  
  36. //指定された本のtodoをすべて返す
  37. //変更↓
  38. @Path("/{account_id}/books/{book_id}/todos")
  39. @GET
  40. @Produces(MediaType.APPLICATION_JSON)
  41. public HashMap<Integer, HashMap<Integer, HashMap<Integer, HashMap<Integer, Todo>>>> getAllTodos(@PathParam("account_id") String account_id, @PathParam("book_id") Integer book_id, @QueryParam("token") String token) {
  42. if (accountManager.checkToken(account_id, token) == true) {
  43. return todoManager.getAllTodos(account_id, book_id);
  44. }
  45. return null;
  46. }
  47.  
  48. //指定された本の指定された年と月のtodoをすべて返す
  49. @Path("/{account_id}/books/{book_id}/todos/{year}/{month}")
  50. @GET
  51. @Produces(MediaType.APPLICATION_JSON)
  52. public HashMap<Integer, HashMap<Integer, Todo>> getTodosByMonth(@PathParam("account_id") String account_id, @PathParam("book_id") Integer book_id, @PathParam("year") Integer year, @PathParam("month") Integer month, @QueryParam("token") String token) {
  53. if (accountManager.checkToken(account_id, token) == true) {
  54. return todoManager.getTodosByMonth(account_id, book_id, year, month);
  55. }
  56. return null;
  57. }
  58.  
  59. //指定された本の指定された年と月と日のtodoをすべて返す
  60. @Path("/{account_id}/books/{book_id}/todos/{year}/{month}/{day}")
  61. @GET
  62. @Produces(MediaType.APPLICATION_JSON)
  63. public HashMap<Integer, Todo> getTodosByDay(@PathParam("account_id") String account_id, @PathParam("book_id") Integer book_id, @PathParam("year") Integer year, @PathParam("month") Integer month, @PathParam("day") Integer day, @QueryParam("token") String token) {
  64. if (accountManager.checkToken(account_id, token) == true) {
  65. return todoManager.getTodosByDay(account_id, book_id, year, month, day);
  66. }
  67. return null;
  68. }
  69.  
  70. //本のtodoを年月日とtodo_idを指定してtodoを一つ返す
  71. @Path("/{account_id}/books/{book_id}/todos/{year}/{month}/{day}/{todo_id}")
  72. @GET
  73. @Produces(MediaType.APPLICATION_JSON)
  74. public Todo getTodoById(@PathParam("account_id") String account_id, @PathParam("book_id") Integer book_id, @PathParam("year") Integer year, @PathParam("month") Integer month, @PathParam("day") Integer day, @PathParam("todo_id") Integer todo_id, @QueryParam("token") String token) {
  75. if (accountManager.checkToken(account_id, token) == true) {
  76. Todo todo = todoManager.getTodoById(account_id, book_id, year, month, day, todo_id);
  77. return todo;
  78. }
  79. return null;
  80. }
  81.  
  82.  
  83. //指定した本と年月日にtodoを新しく追加する
  84. @POST
  85. @Path("/{account_id}/books/{book_id}/todos/{year}/{month}/{day}")
  86. @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
  87. @Produces(MediaType.APPLICATION_JSON)
  88. public Todo createTodo(@PathParam("account_id") String account_id, @PathParam("book_id") Integer book_id, @PathParam("year") Integer year, @PathParam("month") Integer month, @PathParam("day") Integer day, @FormParam("title") String title, @FormParam("token") String token) {
  89. if (accountManager.checkToken(account_id, token) == true) {
  90. return todoManager.createTodo(account_id, book_id, year, month, day, title);
  91. }
  92. return null;
  93. }
  94.  
  95. //todoを選んで達成状態を変更する
  96. //フォームパラメータでチェック状況
  97. @PUT
  98. @Path("/{account_id}/books/{book_id}/todos/{year}/{month}/{day}/{todo_id}/check")
  99. @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
  100. public void setCheck(@PathParam("account_id") String account_id, @PathParam("book_id") Integer book_id, @PathParam("year") Integer year, @PathParam("month") Integer month, @PathParam("day") Integer day, @PathParam("todo_id") Integer todo_id, @FormParam("check") boolean check, @FormParam("token") String token) {
  101. if (accountManager.checkToken(account_id, token) == true) {
  102. todoManager.setCheck(account_id, book_id, year, month, day, todo_id, check);
  103. }
  104. }
  105.  
  106.  
  107. //本のtodoを年月日とtodo_idを指定してそのtodoを削除する
  108. //@DELETE
  109. //@Path("/{account_id}/books/{book_id}/todos/{year}/{month}/{day}/{todo_id}")
  110. //@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
  111. //public void deleteTodoById(@PathParam("account_id") String account_id, @PathParam("book_id") Integer book_id, @PathParam("year") Integer year, @PathParam("month") Integer month, @PathParam("day") Integer day, @PathParam("todo_id") Integer todo_id, @QueryParam("token") String token){
  112. // if(accountManager.checkToken(account_id,token)==true) {
  113. // todoManager.deleteTodoById(account_id, book_id, year, month, day, todo_id, token);
  114. // }
  115. //}
  116.  
  117. @DELETE
  118. @Path("/{account_id}/books/{book_id}/todos/{year}/{month}/{day}/{todo_id}")
  119. @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
  120. public void deleteTodoById(@PathParam("account_id") String account_id, @PathParam("book_id") Integer book_id, @PathParam("year") Integer year, @PathParam("month") Integer month, @PathParam("day") Integer day, @PathParam("todo_id") Integer todo_id, @QueryParam("token") String token) {
  121. if (accountManager.checkToken(account_id, token) == true) {
  122. if (todoManager.deleteTodoById(account_id, book_id, year, month, day, todo_id) == -1) {
  123. var response = Response.status(Response.Status.BAD_REQUEST).entity("消去失敗");
  124. throw new WebApplicationException(response.build());
  125. } else {
  126. var response = Response.status(Response.Status.FORBIDDEN).entity("認証失敗」");
  127. throw new WebApplicationException(response.build());
  128. }
  129. }
  130. }
  131.  
  132. }