Newer
Older
CitrusServer / src / main / java / org / ntlab / citrusserver / resources / AccountsRest.java
  1. package org.ntlab.citrusserver.resources;
  2. import jakarta.ws.rs.*;
  3. import jakarta.ws.rs.core.MediaType;
  4. import org.ntlab.citrusserver.entities.Account;
  5. import org.ntlab.citrusserver.repositories.AccountManager;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.stereotype.Component;
  8.  
  9. import java.util.ArrayList;
  10. import java.util.HashMap;
  11. import java.util.Set;
  12.  
  13. @Path("/accounts")
  14. @Component //accountRestのインスタンスを一個作る
  15.  
  16. public class AccountsRest {
  17. private final AccountManager accountManager; //finalは書き換えられない
  18. @Autowired//springbootの決まり
  19. public AccountsRest(AccountManager am) {
  20. accountManager = am;
  21. }
  22.  
  23. // アカウントの一覧をリストとして返す(GET)
  24. @GET
  25. @Produces(MediaType.APPLICATION_JSON)
  26. public Set<String> getAccount(){
  27. return accountManager.getAccountsID();
  28. }
  29.  
  30.  
  31. // account_idとpasswordを設定し新しいアカウントを作成する(POST)
  32. @POST
  33. @Consumes(MediaType.APPLICATION_FORM_URLENCODED)//bodyに入力する値がある時
  34. public String signup(@FormParam("account_id") String accountId, @FormParam("password") String password) {
  35. String token;
  36. token = accountManager.createAccount(accountId, password);
  37. return token;
  38. }
  39.  
  40. // 指定されたアカウントの情報を返す(GET)
  41. @Path("/{account_id}")
  42. @GET
  43. @Produces(MediaType.APPLICATION_JSON)
  44. public Account getAccountInfo(@PathParam("account_id") String accountId){
  45. Account ac = accountManager.getAccount(accountId);
  46. return ac;
  47. }
  48.  
  49. // アカウント情報を全削除する(DELETE)
  50. @Path("/{account_id}")
  51. @DELETE
  52. public void deleteAccount(@PathParam("account_id") String accountId,
  53. @QueryParam("token") String token,
  54. @QueryParam("password")String password) {
  55. accountManager.deleteAccount(accountId, token, password);
  56. }
  57.  
  58.  
  59. //指定されたIDのパスワードを変更する (PUT)
  60. @Path("/{account_id}/password")
  61. @PUT
  62. public void changePassword(@PathParam("account_id") String accountId,
  63. @FormParam("new_password")String newPassword,
  64. @FormParam("old_password")String oldPassword,
  65. @QueryParam("token") String token){
  66. accountManager.changePassword(accountId,newPassword,oldPassword,token);
  67. }
  68.  
  69. // 指定されたIDの自己紹介を返す(GET)
  70. @Path("/{account_id}/introduction")
  71. @GET
  72. @Produces(MediaType.APPLICATION_JSON)
  73. public String getIntroduction(@PathParam("account_id") String accountId){
  74. String ac = accountManager.AccountIntro(accountId);
  75. return ac;
  76. }
  77.  
  78. // 指定されたIDの自己紹介を変更する (PUT)
  79. @Path("/{account_id}/introduction")
  80. @PUT
  81. public void changeIntroduction(@PathParam("account_id") String accountId,
  82. @FormParam("introduction")String introduction,
  83. @FormParam("token") String token){
  84. accountManager.changeIntroduction(accountId,introduction,token);
  85. }
  86. /////////
  87. // @Path("/{account_id}/photo")
  88. // //画像を返す
  89. // @GET
  90. // public String getAccount(@PathParam("account_id") String accountId){
  91. // Account ac = accountManager.getAccount(accountId);
  92. // return ac.getPhoto();
  93. // }
  94. // @Path("/{account_id}/photo")
  95. // @PUT
  96.  
  97.  
  98. //指定されたIDのお気に入りの本のリストを返す
  99. @Path("/accounts/{account_id}/favorites")
  100. @GET
  101. @Produces(MediaType.APPLICATION_JSON)
  102. public ArrayList<HashMap<String, String>> favoriteBook(@PathParam("account_id") String accountId, @QueryParam("token")String token){
  103. return accountManager.Favorites(accountId,token);
  104. }
  105.  
  106. //指定されたIDのお気に入りの本のリストを返す(指定した人物)
  107. @Path("/accounts/{account_id}/favorites/{other_account_id}")
  108. @GET
  109. @Produces(MediaType.APPLICATION_JSON)
  110. public ArrayList<Integer> FavoriteBook(@PathParam("account_id") String accountId,
  111. @PathParam("other_account_id") String otherAccountId,
  112. @QueryParam("token")String token){
  113. return accountManager.FavoritesBookId(accountId,otherAccountId,token);
  114. }
  115.  
  116. //////////
  117. // お気に入りの本のbook_idを削除する (DELETE)
  118. // @Path("/{account_id}/favorites/{other_account_id}/{book_id}")
  119. // @DELETE
  120.  
  121. // いいねした本のアカウントIDとbook_idを追加する(いいねした側に追加) (PUT)
  122. //@Path("/{account_id}/favorites/{other_account_id}/{book_id}")
  123. // @PUT
  124.  
  125.  
  126.  
  127.  
  128.  
  129. // アカウントidとパスワードでログインし、tokenを返す (POST)
  130. @Path("/{account_id}/login")
  131. @POST
  132. public String login(@PathParam("account_id") String accountId,@FormParam("password") String password) {
  133. return accountManager.login(accountId, password);
  134. }
  135. }