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 jakarta.ws.rs.core.Response;
  5. import org.ntlab.citrusserver.entities.Account;
  6. import org.ntlab.citrusserver.repositories.AccountManager;
  7. import org.springframework.beans.factory.annotation.Autowired;
  8. //import org.springframework.http.HttpStatus;
  9. import org.springframework.stereotype.Component;
  10.  
  11. import java.util.ArrayList;
  12. import java.util.HashMap;
  13. import java.util.Set;
  14.  
  15. @Path("/accounts")
  16. @Component //accountRestのインスタンスを一個作る
  17.  
  18. public class AccountsRest {
  19. private final AccountManager accountManager; //finalは書き換えられない
  20. @Autowired//springbootの決まり
  21. public AccountsRest(AccountManager am) {
  22. accountManager = am;
  23. }
  24.  
  25. // アカウントの一覧をリストとして返す(GET)
  26. @GET
  27. @Produces(MediaType.APPLICATION_JSON)
  28. public Set<String> getAccount(){
  29. return accountManager.getAccountsID();
  30. }
  31.  
  32.  
  33.  
  34. // account_idとpasswordを設定し新しいアカウントを作成する(POST)
  35. @POST
  36. @Consumes(MediaType.APPLICATION_FORM_URLENCODED)//bodyに入力する値がある時
  37. public String signup(@FormParam("account_id") String accountId, @FormParam("password") String password) {
  38. String token;
  39. token = accountManager.createAccount(accountId, password);
  40. return token;
  41. }
  42.  
  43. // 指定されたアカウントの情報を返す(GET)
  44. @Path("/{account_id}")
  45. @GET
  46. @Produces(MediaType.APPLICATION_JSON)
  47. public Account getAccountInfo(@PathParam("account_id") String accountId) {
  48. //404
  49. if (!accountManager.getAccountsID().contains(accountId)){ //account_idが存在しない時
  50. var response = Response.status(Response.Status.NOT_FOUND).entity("IDが存在しません");//404
  51. throw new WebApplicationException(response.build());
  52. }
  53. Account ac = accountManager.getAccount(accountId);
  54. return ac;
  55. }
  56.  
  57. // アカウント情報を全削除する(DELETE)
  58. @Path("/{account_id}")
  59. @DELETE
  60. public void deleteAccount(@PathParam("account_id") String accountId,
  61. @QueryParam("token") String token,
  62. @QueryParam("password")String password) {
  63. if(accountManager.checkToken(accountId, token)) {
  64. accountManager.deleteAccount(accountId, token, password);
  65. }
  66. //404
  67. if (!accountManager.getAccountsID().contains(accountId)){ //account_idが存在しない時
  68. var response = Response.status(Response.Status.NOT_FOUND).entity("IDが存在しません");//404
  69. throw new WebApplicationException(response.build());
  70. }
  71. //403
  72. var response = Response.status(Response.Status.FORBIDDEN).entity("アカウント削除失敗");//forbiddenは403
  73. throw new WebApplicationException(response.build());
  74.  
  75. }
  76.  
  77.  
  78. //指定されたIDのパスワードを変更する (PUT)
  79. @Path("/{account_id}/password")
  80. @PUT
  81. public void changePassword(@PathParam("account_id") String accountId,
  82. @FormParam("new_password")String newPassword,
  83. @FormParam("old_password")String oldPassword,
  84. @FormParam("token") String token){
  85. if(accountManager.checkToken(accountId, token)) {
  86. accountManager.changePassword(accountId, newPassword, oldPassword, token);
  87. }
  88.  
  89. //404
  90. if (!accountManager.getAccountsID().contains(accountId)){ //account_idが存在しない時
  91. var response = Response.status(Response.Status.NOT_FOUND).entity("IDが存在しません");//404
  92. throw new WebApplicationException(response.build());
  93. }
  94. //403
  95. var response = Response.status(Response.Status.FORBIDDEN).entity("認証失敗");//forbiddenは403
  96. throw new WebApplicationException(response.build());
  97. }
  98.  
  99. // 指定されたIDの自己紹介を返す(GET)
  100. @Path("/{account_id}/introduction")
  101. @GET
  102. @Produces(MediaType.APPLICATION_JSON)
  103. public String getIntroduction(@PathParam("account_id") String accountId){
  104. String ac = accountManager.AccountIntro(accountId);
  105. return ac;
  106. }
  107.  
  108. // 指定されたIDの自己紹介を変更する (PUT)
  109. @Path("/{account_id}/introduction")
  110. @PUT
  111. public void changeIntroduction(@PathParam("account_id") String accountId,
  112. @FormParam("introduction")String introduction,
  113. @FormParam("token") String token){
  114. if(accountManager.checkToken(accountId, token)) {
  115. accountManager.changeIntroduction(accountId, introduction, token);
  116. }
  117. //404
  118. if (!accountManager.getAccountsID().contains(accountId)){ //account_idが存在しない時
  119. var response = Response.status(Response.Status.NOT_FOUND).entity("IDが存在しません");//404
  120. throw new WebApplicationException(response.build());
  121. }
  122. //403
  123. var response = Response.status(Response.Status.FORBIDDEN).entity("認証失敗");//forbiddenは403
  124. throw new WebApplicationException(response.build());
  125. }
  126. /////////
  127. // @Path("/{account_id}/photo")
  128. // //画像を返す
  129. // @GET
  130. // public String getAccount(@PathParam("account_id") String accountId){
  131. // Account ac = accountManager.getAccount(accountId);
  132. // return ac.getPhoto();
  133. // }
  134. // @Path("/{account_id}/photo")
  135. // @PUT
  136.  
  137.  
  138. //指定されたIDのお気に入りの本のリストを返す
  139. @Path("/accounts/{account_id}/favorites")
  140. @GET
  141. @Produces(MediaType.APPLICATION_JSON)
  142. public ArrayList<HashMap<String, String>> favoriteBook(@PathParam("account_id") String accountId,
  143. @QueryParam("token")String token){
  144. if(accountManager.checkToken(accountId, token)) {
  145. return accountManager.Favorites(accountId, token);
  146. }
  147. //404
  148. if (!accountManager.getAccountsID().contains(accountId)){ //account_idが存在しない時
  149. var response = Response.status(Response.Status.NOT_FOUND).entity("IDが存在しません");//404
  150. throw new WebApplicationException(response.build());
  151. }
  152. return null;
  153. }
  154.  
  155. //指定されたIDのお気に入りの本のリストを返す(指定した人物)
  156. @Path("/accounts/{account_id}/favorites/{other_account_id}")
  157. @GET
  158. @Produces(MediaType.APPLICATION_JSON)
  159. public ArrayList<Integer> FavoriteBook(@PathParam("account_id") String accountId,
  160. @PathParam("other_account_id") String otherAccountId,
  161. @QueryParam("token")String token){
  162. if(accountManager.checkToken(accountId, token)) {
  163. return accountManager.FavoritesBookId(accountId, otherAccountId, token);
  164. }
  165.  
  166. //404
  167. if (!accountManager.getAccountsID().contains(accountId)){ //account_idが存在しない時
  168. var response = Response.status(Response.Status.NOT_FOUND).entity("IDが存在しません");//404
  169. throw new WebApplicationException(response.build());
  170. }
  171. //403
  172. var response = Response.status(Response.Status.FORBIDDEN).entity("認証失敗");//forbiddenは403
  173. throw new WebApplicationException(response.build());
  174. }
  175.  
  176. //////////
  177. // お気に入りの本のbook_idを削除する (DELETE)
  178. // @Path("/{account_id}/favorites/{other_account_id}/{book_id}")
  179. // @DELETE
  180.  
  181. // いいねした本のアカウントIDとbook_idを追加する(いいねした側に追加) (PUT)
  182. //@Path("/{account_id}/favorites/{other_account_id}/{book_id}")
  183. // @PUT
  184.  
  185.  
  186.  
  187. // アカウントidとパスワードでログインし、tokenを返す (POST)
  188. @Path("/{account_id}/login")
  189. @POST
  190. public String login(@PathParam("account_id") String accountId,@FormParam("password") String password) {
  191. //404
  192. if (!accountManager.getAccountsID().contains(accountId)){ //account_idが存在しない時
  193. var response = Response.status(Response.Status.NOT_FOUND).entity("IDが存在しません");//404
  194. throw new WebApplicationException(response.build());
  195. }
  196. return accountManager.login(accountId, password);
  197. }
  198.  
  199. }