diff --git a/src/main/java/org/ntlab/citrusserver/resources/BooksRest.java b/src/main/java/org/ntlab/citrusserver/resources/BooksRest.java index 3398b0b..6e8982c 100644 --- a/src/main/java/org/ntlab/citrusserver/resources/BooksRest.java +++ b/src/main/java/org/ntlab/citrusserver/resources/BooksRest.java @@ -39,19 +39,9 @@ @Produces(MediaType.APPLICATION_JSON) public HashMap getBooks(@PathParam("account_id") String account_id, @QueryParam("token") String token){ - if(accountManager.getAccount(account_id) == null){ - var response = Response.status(Response.Status.NOT_FOUND).entity("アカウントが見つかりません"); - throw new WebApplicationException(response.build()); - } - else{ - if(!accountManager.checkToken(account_id, token)) { - var response = Response.status(Response.Status.FORBIDDEN).entity("認証失敗"); - throw new WebApplicationException(response.build()); - } - else{ - return bookManager.getBooks(account_id); - } - } + accountCheck(account_id); + tokenCheck(account_id, token); + return bookManager.getBooks(account_id); } @Path("/{account_id}/books") @@ -60,19 +50,9 @@ @Consumes(MediaType.APPLICATION_FORM_URLENCODED) // postmanのbodyに入力する値がある時 public Book createBook(@PathParam("account_id") String account_id, @FormParam("title") String title, @FormParam("color") String color, @FormParam("publicity") Boolean publicity, @FormParam("token") String token) { - if (accountManager.getAccount(account_id) == null){ - var response = Response.status(Response.Status.NOT_FOUND).entity("アカウントが見つかりません"); - throw new WebApplicationException(response.build()); - } - else{ - if(!accountManager.checkToken(account_id, token)) { - var response = Response.status(Response.Status.FORBIDDEN).entity("認証失敗"); - throw new WebApplicationException(response.build()); - } - else { - return bookManager.createBook(account_id, title, color, publicity); - } - } + accountCheck(account_id); + tokenCheck(account_id, token); + return bookManager.createBook(account_id, title, color, publicity); } @@ -83,19 +63,9 @@ @Produces(MediaType.APPLICATION_JSON) public Book getBook(@PathParam("account_id") String account_id, @PathParam("book_id") Integer book_id, @QueryParam("token") String token){ - if (accountManager.getAccount(account_id) == null){ - var response = Response.status(Response.Status.NOT_FOUND).entity("アカウントが見つかりません"); - throw new WebApplicationException(response.build()); - } - else{ - if(!accountManager.checkToken(account_id, token)) { - var response = Response.status(Response.Status.FORBIDDEN).entity("認証失敗"); - throw new WebApplicationException(response.build()); - } - else{ - return bookManager.getBook(account_id, book_id); - } - } + accountCheck(account_id); + tokenCheck(account_id, token); + return bookManager.getBook(account_id, book_id); } /// 本の削除 @Path("/{account_id}/books/{book_id}") @@ -104,21 +74,11 @@ @Consumes(MediaType.APPLICATION_FORM_URLENCODED) public String deleteBook(@PathParam("account_id") String account_id, @PathParam("book_id") Integer book_id, @QueryParam("token") String token){ - if(accountManager.getAccount(account_id) == null){ - var response = Response.status(Response.Status.NOT_FOUND).entity("アカウントが見つかりません"); - throw new WebApplicationException(response.build()); - } - else{ - if(!accountManager.checkToken(account_id, token)) { - var response = Response.status(Response.Status.FORBIDDEN).entity("認証失敗"); - throw new WebApplicationException(response.build()); - } - else{ - todoManager.deleteAllTodosByBookId(account_id, book_id);//削除時、Todoも消す - bookManager.deleteBook(account_id, book_id); - return "success"; - } - } + accountCheck(account_id); + tokenCheck(account_id, token); + todoManager.deleteAllTodosByBookId(account_id, book_id);//削除時、Todoも消す + bookManager.deleteBook(account_id, book_id); + return "success"; } /// /{account_id}/books/{book_id}/title @@ -128,19 +88,9 @@ @Produces(MediaType.TEXT_PLAIN) public String getTitle(@PathParam("account_id") String account_id, @PathParam("book_id") Integer book_id, @QueryParam("token") String token){ - if (accountManager.getAccount(account_id) == null) { - var response = Response.status(Response.Status.NOT_FOUND).entity("アカウントが見つかりません"); - throw new WebApplicationException(response.build()); - } - else{ - if(!accountManager.checkToken(account_id, token)) { - var response = Response.status(Response.Status.FORBIDDEN).entity("認証失敗"); - throw new WebApplicationException(response.build()); - } - else{ - return bookManager.getTitle(account_id, book_id); - } - } + accountCheck(account_id); + tokenCheck(account_id, token); + return bookManager.getTitle(account_id, book_id); } /// 本のタイトル変更 @@ -150,20 +100,10 @@ @Consumes(MediaType.APPLICATION_FORM_URLENCODED) public String putTitle(@PathParam("account_id") String account_id, @PathParam("book_id") Integer book_id, @FormParam("title") String title, @FormParam("token") String token){ - if(accountManager.getAccount(account_id) == null){ - var response = Response.status(Response.Status.NOT_FOUND).entity("アカウントが見つかりません"); - throw new WebApplicationException(response.build()); - } - else{ - if(!accountManager.checkToken(account_id, token)) { - var response = Response.status(Response.Status.FORBIDDEN).entity("認証失敗"); - throw new WebApplicationException(response.build()); - } - else{ - bookManager.putTitle(account_id, book_id, title); - return "success"; - } - } + accountCheck(account_id); + tokenCheck(account_id, token); + bookManager.putTitle(account_id, book_id, title); + return "success"; } /// /accounts/{account_id}/books/{book_id}/public @@ -173,19 +113,9 @@ @Produces(MediaType.TEXT_PLAIN) public Boolean getPublicity(@PathParam("account_id") String account_id, @PathParam("book_id") Integer book_id, @QueryParam("token") String token){ - if (accountManager.getAccount(account_id) == null) { - var response = Response.status(Response.Status.NOT_FOUND).entity("アカウントが見つかりません"); - throw new WebApplicationException(response.build()); - } - else{ - if(!accountManager.checkToken(account_id, token)) { - var response = Response.status(Response.Status.FORBIDDEN).entity("認証失敗"); - throw new WebApplicationException(response.build()); - } - else{ - return bookManager.getPublicity(account_id, book_id); - } - } + accountCheck(account_id); + tokenCheck(account_id, token); + return bookManager.getPublicity(account_id, book_id); } /// 公開情報を変更する @@ -195,20 +125,10 @@ @Consumes(MediaType.APPLICATION_FORM_URLENCODED) public String putPublicity(@PathParam("account_id") String account_id, @PathParam("book_id") Integer book_id, @FormParam("publicity") Boolean publicity, @FormParam("token") String token){ - if(accountManager.getAccount(account_id) == null){ - var response = Response.status(Response.Status.NOT_FOUND).entity("アカウントが見つかりません"); - throw new WebApplicationException(response.build()); - } - else{ - if(!accountManager.checkToken(account_id, token)) { - var response = Response.status(Response.Status.FORBIDDEN).entity("認証失敗"); - throw new WebApplicationException(response.build()); - } - else{ - bookManager.putPublicity(account_id, book_id, publicity); - return "success"; - } - } + accountCheck(account_id); + tokenCheck(account_id, token); + bookManager.putPublicity(account_id, book_id, publicity); + return "success"; } /// /accounts/{account_id}/books/{book_id}/color @@ -218,24 +138,27 @@ @Consumes(MediaType.APPLICATION_FORM_URLENCODED) public String putColor(@PathParam("account_id") String account_id, @PathParam("book_id") Integer book_id, @FormParam("color") String color, @FormParam("token") String token){ - if(accountManager.getAccount(account_id) == null){ - var response = Response.status(Response.Status.NOT_FOUND).entity("アカウントが見つかりません"); - throw new WebApplicationException(response.build()); - } - else{ - if(!accountManager.checkToken(account_id, token)) { - var response = Response.status(Response.Status.FORBIDDEN).entity("認証失敗"); - throw new WebApplicationException(response.build()); - } - else{ - bookManager.putColor(account_id, book_id, color); - return "success"; - } - } + accountCheck(account_id); + tokenCheck(account_id, token); + bookManager.putColor(account_id, book_id, color); + return "success"; } ///--------------------------------------------------------------------- ///private ///--------------------------------------------------------------------- + private void accountCheck(String account_id){ + if(accountManager.getAccount(account_id) == null){ + var response = Response.status(Response.Status.NOT_FOUND).entity("アカウントが見つかりません"); + throw new WebApplicationException(response.build()); + } + } + + private void tokenCheck(String account_id, String token){ + if(!accountManager.checkToken(account_id, token)) { + var response = Response.status(Response.Status.FORBIDDEN).entity("認証失敗"); + throw new WebApplicationException(response.build()); + } + } } \ No newline at end of file