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)) {
  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)) {
  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)) {
  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)) {
  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)) {
  90. return todoManager.createTodo(account_id, book_id, year, month, day, title);
  91. }
  92. return null;
  93. }
  94.  
  95. //todoを選んで達成状態を変更する
  96. //フォームパラメータでチェック状況
  97. //-1エラー 1達成
  98. @PUT
  99. @Path("/{account_id}/books/{book_id}/todos/{year}/{month}/{day}/{todo_id}/check")
  100. @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
  101. 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) {
  102. if (accountManager.checkToken(account_id, token)) {
  103. if (todoManager.setCheck(account_id, book_id, year, month, day, todo_id, check) == -1) {
  104. var response = Response.status(Response.Status.BAD_REQUEST).entity("変更失敗");
  105. throw new WebApplicationException(response.build());
  106. }
  107. }else{
  108. var response = Response.status(Response.Status.FORBIDDEN).entity("認証失敗");
  109. throw new WebApplicationException(response.build());
  110. }
  111. }
  112.  
  113.  
  114. /* //本のtodoを年月日とtodo_idを指定してそのtodoを削除する
  115. @DELETE
  116. @Path("/{account_id}/books/{book_id}/todos/{year}/{month}/{day}/{todo_id}")
  117. @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
  118. 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){
  119. if(accountManager.checkToken(account_id,token)==true) {
  120. todoManager.deleteTodoById(account_id, book_id, year, month, day, todo_id);
  121. }
  122. }*/
  123.  
  124. //本のtodoを年月日とtodo_idを指定してそのtodoを削除する
  125. //-1えらー 1達成
  126. @DELETE
  127. @Path("/{account_id}/books/{book_id}/todos/{year}/{month}/{day}/{todo_id}")
  128. @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
  129. 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) {
  130. if (accountManager.checkToken(account_id, token)) {
  131. if (todoManager.deleteTodoById(account_id, book_id, year, month, day, todo_id) == -1) {
  132. var response = Response.status(Response.Status.BAD_REQUEST).entity("消去失敗");
  133. throw new WebApplicationException(response.build());
  134. }
  135. }else {
  136. var response = Response.status(Response.Status.FORBIDDEN).entity("認証失敗");
  137. throw new WebApplicationException(response.build());
  138. }
  139. }
  140. }
  141.  
  142.