package org.ntlab.citrusserver.resources; import javax.ws.rs.*; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; import org.ntlab.citrusserver.entities.Account; import org.ntlab.citrusserver.repositories.*; import org.springframework.beans.factory.annotation.Autowired; //import org.springframework.http.HttpStatus; import org.springframework.stereotype.Component; import java.util.ArrayList; import java.util.HashMap; import java.util.Set; @Path("/accounts") @Component //accountRestのインスタンスを一個作る public class AccountsRest { private final AccountManager accountManager; private final BookManager bookManager; private final ScheduleManager scheduleManager; private final TodoManager todoManager;//finalは書き換えられない @Autowired//springbootの決まり public AccountsRest(AccountManager am, BookManager bm, ScheduleManager sm, TodoManager tm) { bookManager = bm; scheduleManager = sm; accountManager = am; todoManager = tm; } // アカウントの一覧をリストとして返す(GET) @GET @Produces(MediaType.APPLICATION_JSON) public Set<String> getAccount(){ return accountManager.getAccountsID(); } // account_idとpasswordを設定し新しいアカウントを作成する(POST) @POST @Consumes(MediaType.APPLICATION_FORM_URLENCODED)//bodyに入力する値がある時 @Produces(MediaType.APPLICATION_JSON) public String signup(@FormParam("account_id") String accountId, @FormParam("password") String password) { String token; if (password == null) { var response = Response.status(Response.Status.BAD_REQUEST).entity("passwordを入力してください"); throw new WebApplicationException(response.build()); } token = accountManager.createAccount(accountId, password); if (token == null){ var response = Response.status(Response.Status.CONFLICT).entity("id '" + accountId + "' は既に存在します");//404 throw new WebApplicationException(response.build()); } return "\"" + token +"\""; } // 指定されたアカウントの情報を返す(GET) @Path("/{account_id}") @GET @Produces(MediaType.APPLICATION_JSON) public Account getAccountInfo(@PathParam("account_id") String accountId) { //404 if (!accountManager.getAccountsID().contains(accountId)){ //account_idが存在しない時 var response = Response.status(Response.Status.NOT_FOUND).entity("IDが存在しません");//404 throw new WebApplicationException(response.build()); } return accountManager.getAccount(accountId); } // アカウント情報を全削除する(DELETE) @Path("/{account_id}") @DELETE public void deleteAccount(@PathParam("account_id") String accountId, @QueryParam("token") String token, @QueryParam("password")String password) { if(accountManager.checkToken(accountId, token)) { accountManager.deleteAccount(accountId, token, password); return; } //404 if (!accountManager.getAccountsID().contains(accountId)){ //account_idが存在しない時 var response = Response.status(Response.Status.NOT_FOUND).entity("IDが存在しません");//404 throw new WebApplicationException(response.build()); } //403 var response = Response.status(Response.Status.FORBIDDEN).entity("アカウント削除失敗");//forbiddenは403 throw new WebApplicationException(response.build()); } //accountのidを変更する(PUT) @Path("/{account_id}") @PUT @Consumes(MediaType.APPLICATION_FORM_URLENCODED)//bodyに入力する値がある時 public void changeAccountId(@PathParam("account_id") String accountId, @FormParam("new_account_id")String newAccountId, @FormParam("old_password")String oldPassword, @FormParam("token") String token){ //404 if (!accountManager.getAccountsID().contains(accountId)){ //account_idが存在しない時 var response = Response.status(Response.Status.NOT_FOUND).entity("IDが存在しません");//404 throw new WebApplicationException(response.build()); } if(!accountManager.getAccount(accountId).getPassword().equals(oldPassword)) { var response = Response.status(Response.Status.BAD_REQUEST).entity("パスワードが違います");//404 throw new WebApplicationException(response.build()); } //成功 if(accountManager.checkToken(accountId, token)) { accountManager.changeAccountId(accountId, newAccountId,oldPassword, token); bookManager.changeAccountId(accountId, newAccountId); scheduleManager.changeAccountId(accountId, newAccountId); todoManager.changeAccountId(accountId, newAccountId); return; } //403 var response = Response.status(Response.Status.FORBIDDEN).entity("認証失敗");//forbiddenは403 throw new WebApplicationException(response.build()); } //指定されたIDのパスワードを変更する (PUT) @Path("/{account_id}/password") @PUT @Consumes(MediaType.APPLICATION_FORM_URLENCODED)//bodyに入力する値がある時 public void changePassword(@PathParam("account_id") String accountId, @FormParam("new_password")String newPassword, @FormParam("old_password")String oldPassword, @FormParam("token") String token){ if(!accountManager.getAccount(accountId).getPassword().equals(oldPassword)) { var response = Response.status(Response.Status.BAD_REQUEST).entity("パスワードが違います");//404 throw new WebApplicationException(response.build()); } if(accountManager.checkToken(accountId, token)) { accountManager.changePassword(accountId, newPassword, oldPassword, token); return; } //404 if (!accountManager.getAccountsID().contains(accountId)){ //account_idが存在しない時 var response = Response.status(Response.Status.NOT_FOUND).entity("IDが存在しません");//404 throw new WebApplicationException(response.build()); } //403 var response = Response.status(Response.Status.FORBIDDEN).entity("認証失敗");//forbiddenは403 throw new WebApplicationException(response.build()); } // 指定されたIDの自己紹介を返す(GET) @Path("/{account_id}/introduction") @GET @Produces(MediaType.APPLICATION_JSON) public String getIntroduction(@PathParam("account_id") String accountId){ String ac = accountManager.AccountIntro(accountId); return ac; } // 指定されたIDの自己紹介を変更する (PUT) @Path("/{account_id}/introduction") @PUT @Consumes(MediaType.APPLICATION_FORM_URLENCODED)//bodyに入力する値がある時 public void changeIntroduction(@PathParam("account_id") String accountId, @FormParam("introduction")String introduction, @FormParam("token") String token){ if(accountManager.checkToken(accountId, token)) { accountManager.changeIntroduction(accountId, introduction, token); return; } //404 if (!accountManager.getAccountsID().contains(accountId)){ //account_idが存在しない時 var response = Response.status(Response.Status.NOT_FOUND).entity("IDが存在しません");//404 throw new WebApplicationException(response.build()); } //403 var response = Response.status(Response.Status.FORBIDDEN).entity("認証失敗");//forbiddenは403 throw new WebApplicationException(response.build()); } ///////////(9/26) // accountの色を返す(GET) @Path("/{account_id}/accountColor") @GET @Produces(MediaType.APPLICATION_JSON) public String getAccountColor(@PathParam("account_id") String accountId){ String accountColor = accountManager.getAccountColor(accountId); return "\"" + accountColor + "\""; } //accountの色を変更する(PUT) @Path("/{account_id}/accountColor") @PUT @Consumes(MediaType.APPLICATION_FORM_URLENCODED)//bodyに入力する値がある時 public void changeAccountColor(@PathParam("account_id") String accountId, @FormParam("accountColor")String accountColor, @FormParam("token") String token){ //404 if (!accountManager.getAccountsID().contains(accountId)){ //account_idが存在しない時 var response = Response.status(Response.Status.NOT_FOUND).entity("IDが存在しません");//404 throw new WebApplicationException(response.build()); } //成功 if(accountManager.checkToken(accountId, token)) { //accountManager.changeAccountColor(accountId, "D5D5D5", token); accountManager.changeAccountColor(accountId, accountColor, token); return; } //403 var response = Response.status(Response.Status.FORBIDDEN).entity("認証失敗");//forbiddenは403 throw new WebApplicationException(response.build()); } /////////////////// // アカウントidとパスワードでログインし、tokenを返す (POST) @Path("/{account_id}/login") @POST @Produces(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_FORM_URLENCODED)//bodyに入力する値がある時 public String login(@PathParam("account_id") String accountId,@FormParam("password") String password) { //404 if (!accountManager.getAccountsID().contains(accountId)){ //account_idが存在しない時 var response = Response.status(Response.Status.NOT_FOUND).entity("IDが存在しません");//404 throw new WebApplicationException(response.build()); } return "\""+ accountManager.login(accountId, password)+"\""; } }