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. if (password == null) {
  50. var response = Response.status(Response.Status.BAD_REQUEST).entity("passwordを入力してください");
  51. throw new WebApplicationException(response.build());
  52. }
  53. token = accountManager.createAccount(accountId, password);
  54. if (token == null){
  55. var response = Response.status(Response.Status.CONFLICT).entity("id '" + accountId + "' は既に存在します");//404
  56. throw new WebApplicationException(response.build());
  57. }
  58. return token;
  59. }
  60.  
  61. // 指定されたアカウントの情報を返す(GET)
  62. @Path("/{account_id}")
  63. @GET
  64. @Produces(MediaType.APPLICATION_JSON)
  65. public Account getAccountInfo(@PathParam("account_id") String accountId) {
  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. return accountManager.getAccount(accountId);
  72. }
  73.  
  74. // アカウント情報を全削除する(DELETE)
  75. @Path("/{account_id}")
  76. @DELETE
  77. public void deleteAccount(@PathParam("account_id") String accountId,
  78. @QueryParam("token") String token,
  79. @QueryParam("password")String password) {
  80. if(accountManager.checkToken(accountId, token)) {
  81. accountManager.deleteAccount(accountId, token, password);
  82. bookManager.deleteAllBooks(accountId);
  83. todoManager.deleteAllTodosByAccountId(accountId);
  84. scheduleManager.deleteSchedules(accountId);
  85. return;
  86. }
  87. //404
  88. if (!accountManager.getAccountsID().contains(accountId)){ //account_idが存在しない時
  89. var response = Response.status(Response.Status.NOT_FOUND).entity("IDが存在しません");//404
  90. throw new WebApplicationException(response.build());
  91. }
  92. //403
  93. var response = Response.status(Response.Status.FORBIDDEN).entity("アカウント削除失敗");//forbiddenは403
  94. throw new WebApplicationException(response.build());
  95.  
  96. }
  97.  
  98.  
  99. //指定されたIDのパスワードを変更する (PUT)
  100. @Path("/{account_id}/password")
  101. @PUT
  102. public void changePassword(@PathParam("account_id") String accountId,
  103. @FormParam("new_password")String newPassword,
  104. @FormParam("old_password")String oldPassword,
  105. @FormParam("token") String token){
  106. if(accountManager.checkToken(accountId, token)) {
  107. accountManager.changePassword(accountId, newPassword, oldPassword, token);
  108. return;
  109. }
  110.  
  111. //404
  112. if (!accountManager.getAccountsID().contains(accountId)){ //account_idが存在しない時
  113. var response = Response.status(Response.Status.NOT_FOUND).entity("IDが存在しません");//404
  114. throw new WebApplicationException(response.build());
  115. }
  116. //403
  117. var response = Response.status(Response.Status.FORBIDDEN).entity("認証失敗");//forbiddenは403
  118. throw new WebApplicationException(response.build());
  119. }
  120.  
  121. // 指定されたIDの自己紹介を返す(GET)
  122. @Path("/{account_id}/introduction")
  123. @GET
  124. @Produces(MediaType.APPLICATION_JSON)
  125. public String getIntroduction(@PathParam("account_id") String accountId){
  126. String ac = accountManager.AccountIntro(accountId);
  127. return ac;
  128. }
  129.  
  130. // 指定されたIDの自己紹介を変更する (PUT)
  131. @Path("/{account_id}/introduction")
  132. @PUT
  133. public void changeIntroduction(@PathParam("account_id") String accountId,
  134. @FormParam("introduction")String introduction,
  135. @FormParam("token") String token){
  136. if(accountManager.checkToken(accountId, token)) {
  137. accountManager.changeIntroduction(accountId, introduction, token);
  138. return;
  139. }
  140. //404
  141. if (!accountManager.getAccountsID().contains(accountId)){ //account_idが存在しない時
  142. var response = Response.status(Response.Status.NOT_FOUND).entity("IDが存在しません");//404
  143. throw new WebApplicationException(response.build());
  144. }
  145. //403
  146. var response = Response.status(Response.Status.FORBIDDEN).entity("認証失敗");//forbiddenは403
  147. throw new WebApplicationException(response.build());
  148. }
  149. /////////
  150. // @Path("/{account_id}/photo")
  151. // //画像を返す
  152. // @GET
  153. // public String getAccount(@PathParam("account_id") String accountId){
  154. // Account ac = accountManager.getAccount(accountId);
  155. // return ac.getPhoto();
  156. // }
  157. // @Path("/{account_id}/photo")
  158. // @PUT
  159.  
  160.  
  161. //指定されたIDのお気に入りの本のリストを返す
  162. @Path("/accounts/{account_id}/favorites")
  163. @GET
  164. @Produces(MediaType.APPLICATION_JSON)
  165. public ArrayList<HashMap<String, String>> favoriteBook(@PathParam("account_id") String accountId,
  166. @QueryParam("token")String token){
  167. if(accountManager.checkToken(accountId, token)) {
  168. return accountManager.Favorites(accountId, token);
  169. }
  170. //404
  171. if (!accountManager.getAccountsID().contains(accountId)){ //account_idが存在しない時
  172. var response = Response.status(Response.Status.NOT_FOUND).entity("IDが存在しません");//404
  173. throw new WebApplicationException(response.build());
  174. }
  175. return null;
  176. }
  177.  
  178. //指定されたIDのお気に入りの本のリストを返す(指定した人物)
  179. @Path("/accounts/{account_id}/favorites/{other_account_id}")
  180. @GET
  181. @Produces(MediaType.APPLICATION_JSON)
  182. public ArrayList<Integer> FavoriteBook(@PathParam("account_id") String accountId,
  183. @PathParam("other_account_id") String otherAccountId,
  184. @QueryParam("token")String token){
  185. if(accountManager.checkToken(accountId, token)) {
  186. return accountManager.FavoritesBookId(accountId, otherAccountId, token);
  187. }
  188.  
  189. //404
  190. if (!accountManager.getAccountsID().contains(accountId)){ //account_idが存在しない時
  191. var response = Response.status(Response.Status.NOT_FOUND).entity("IDが存在しません");//404
  192. throw new WebApplicationException(response.build());
  193. }
  194. //403
  195. var response = Response.status(Response.Status.FORBIDDEN).entity("認証失敗");//forbiddenは403
  196. throw new WebApplicationException(response.build());
  197. }
  198.  
  199. //////////
  200. // お気に入りの本のbook_idを削除する (DELETE)
  201. // @Path("/{account_id}/favorites/{other_account_id}/{book_id}")
  202. // @DELETE
  203.  
  204. // いいねした本のアカウントIDとbook_idを追加する(いいねした側に追加) (PUT)
  205. //@Path("/{account_id}/favorites/{other_account_id}/{book_id}")
  206. // @PUT
  207.  
  208.  
  209.  
  210. // アカウントidとパスワードでログインし、tokenを返す (POST)
  211. @Path("/{account_id}/login")
  212. @POST
  213. public String login(@PathParam("account_id") String accountId,@FormParam("password") String password) {
  214. //404
  215. if (!accountManager.getAccountsID().contains(accountId)){ //account_idが存在しない時
  216. var response = Response.status(Response.Status.NOT_FOUND).entity("IDが存在しません");//404
  217. throw new WebApplicationException(response.build());
  218. }
  219. return accountManager.login(accountId, password);
  220. }
  221.  
  222. }