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