Newer
Older
CitrusServer / src / main / java / org / ntlab / citrusserver / resources / BooksRest.java
  1. package org.ntlab.citrusserver.resources;
  2.  
  3. import jakarta.ws.rs.*;
  4. import jakarta.ws.rs.core.MediaType;
  5. import org.ntlab.citrusserver.entities.Book;
  6. import org.ntlab.citrusserver.repositories.BookManager;
  7. import org.springframework.beans.factory.annotation.Autowired;
  8. import org.springframework.stereotype.Component;
  9.  
  10. import java.util.HashMap;
  11.  
  12.  
  13. @Path("/accounts")
  14. @Component
  15.  
  16.  
  17. public class BooksRest { // BookRestはクラス
  18.  
  19. private final BookManager bookManager;
  20. @Autowired // スプリングブートにいうサイン
  21. public BooksRest(BookManager bm){ //public クラス名()がコンストラクタ
  22. bookManager = bm;
  23. }
  24.  
  25.  
  26. private final HashMap<String, HashMap<Integer, Book>> books = new HashMap<>();
  27. /// {account_id}/books
  28. /// その人の本のタイトルとかを返す
  29. @Path("/{account_id}/books")
  30. @GET
  31. @Produces(MediaType.APPLICATION_JSON)
  32. public HashMap<Integer, Book> getBooks(@PathParam("account_id") String account_id, @QueryParam("token") String token){
  33. return bookManager.getBooks(account_id, token);
  34. }
  35.  
  36. @Path("/{account_id}/books")
  37. @POST
  38. @Consumes(MediaType.APPLICATION_FORM_URLENCODED)//bodyに入力する値がある時
  39. public Book createBook(@PathParam("account_id") String account_id, @FormParam("title") String title, @FormParam("color") String color, @FormParam("publicity") Boolean publicity, @FormParam("token") String token) {
  40. return bookManager.createBook(account_id, title, color, publicity, token);
  41. }
  42.  
  43.  
  44. /// {account_id}/books/{book_id}
  45. /// 本の情報を取得
  46. @Path("/{account_id}/books/{book_id}")
  47. @GET
  48. @Produces(MediaType.APPLICATION_JSON)
  49. public Book getBook(@PathParam("account_id") String account_id, @PathParam("book_id") Integer book_id, @QueryParam("token") String token){
  50. return bookManager.getBook(account_id, book_id, token);
  51. }
  52. /// 本の削除
  53. @Path("/{account_id}/books/{book_id}")
  54. @DELETE
  55. @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
  56. public void deleteTodoById(@PathParam("account_id") String account_id, @PathParam("book_id") Integer book_id, @QueryParam("token") String token){
  57. bookManager.deleteBook(account_id, book_id, token);
  58. }
  59.  
  60. /// /{account_id}/books/{book_id}/title
  61. /// 本のタイトルを返す
  62. @Path("/{account_id}/books/{book_id}/title")
  63. @GET
  64. @Produces(MediaType.TEXT_PLAIN)
  65. public String getTitle(@PathParam("account_id") String account_id, @PathParam("book_id") Integer book_id, @QueryParam("token") String token){
  66. return bookManager.getTitle(account_id, book_id, token);
  67. }
  68.  
  69. /// 本のタイトル変更
  70. @Path("/{account_id}/books/{book_id}/title")
  71. @PUT
  72. @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
  73. public void putTitle(@PathParam("account_id") String account_id, @PathParam("book_id") Integer book_id, @FormParam("title") String title, @FormParam("token") String token){
  74. bookManager.putTitle(account_id, book_id, title, token);
  75. }
  76.  
  77. /// /accounts/{account_id}/books/{book_id}/public
  78. /// 本の公開状態を返す
  79. @Path("/{account_id}/books/{book_id}/public")
  80. @GET
  81. @Produces(MediaType.TEXT_PLAIN)
  82. public Boolean getPublicity(@PathParam("account_id") String account_id, @PathParam("book_id") Integer book_id, @QueryParam("token") String token){
  83. return bookManager.getPublicity(account_id, book_id, token);
  84. }
  85.  
  86. /// 公開情報を変更する
  87. @Path("/{account_id}/books/{book_id}/public")
  88. @PUT
  89. @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
  90. public void putPublicity(@PathParam("account_id") String account_id, @PathParam("book_id") Integer book_id, @FormParam("publicity") Boolean publicity, @FormParam("token") String token){
  91. bookManager.putPublicity(account_id, book_id, publicity, token);
  92. }
  93.  
  94. /// /accounts/{account_id}/books/{book_id}/color
  95. /// 公開情報を変更する
  96. @Path("/{account_id}/books/{book_id}/color")
  97. @PUT
  98. @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
  99. public void putColor(@PathParam("account_id") String account_id, @PathParam("book_id") Integer book_id, @FormParam("color") String color, @FormParam("token") String token){
  100. bookManager.putColor(account_id, book_id, color, token);
  101. }
  102. }