diff --git a/src/main/java/org/ntlab/citrusserver/resources/BooksRest.java b/src/main/java/org/ntlab/citrusserver/resources/BooksRest.java index 99802ce..2695eb9 100644 --- a/src/main/java/org/ntlab/citrusserver/resources/BooksRest.java +++ b/src/main/java/org/ntlab/citrusserver/resources/BooksRest.java @@ -2,6 +2,7 @@ import jakarta.ws.rs.*; import jakarta.ws.rs.core.MediaType; +import jakarta.ws.rs.core.Response; import org.ntlab.citrusserver.entities.Book; import org.ntlab.citrusserver.repositories.AccountManager; import org.ntlab.citrusserver.repositories.BookManager; @@ -34,8 +35,13 @@ @GET @Produces(MediaType.APPLICATION_JSON) public HashMap getBooks(@PathParam("account_id") String account_id, @QueryParam("token") String token){ - if(!accountManager.checkToken(account_id, token)) return null; - return bookManager.getBooks(account_id, token); + 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); + } } @Path("/{account_id}/books") @@ -43,8 +49,13 @@ @Produces(MediaType.APPLICATION_JSON) // intとかstringとかがたくさん返ってくるから、json public voidじゃないときは、返さなあかんから、 @Produces(MediaType.APPLICATION_JSON) これがいる @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.checkToken(account_id, token)) return null; - return bookManager.createBook(account_id, title, color, publicity, token); + 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); + } } @@ -54,16 +65,28 @@ @GET @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.checkToken(account_id, token)) return null; - return bookManager.getBook(account_id, book_id, token); + 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); + } } /// 本の削除 @Path("/{account_id}/books/{book_id}") @DELETE + @Produces(MediaType.TEXT_PLAIN) @Consumes(MediaType.APPLICATION_FORM_URLENCODED) - public void deleteTodoById(@PathParam("account_id") String account_id, @PathParam("book_id") Integer book_id, @QueryParam("token") String token){ - if(!accountManager.checkToken(account_id, token)) return; - bookManager.deleteBook(account_id, book_id, token); + public String deleteTodoById(@PathParam("account_id") String account_id, @PathParam("book_id") Integer book_id, @QueryParam("token") String token){ + if(!accountManager.checkToken(account_id, token)) { + var response = Response.status(Response.Status.FORBIDDEN).entity("認証失敗"); + throw new WebApplicationException(response.build()); + } + else{ + bookManager.deleteBook(account_id, book_id); + return "success"; + } } /// /{account_id}/books/{book_id}/title @@ -72,17 +95,29 @@ @GET @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.checkToken(account_id, token)) return null; - return bookManager.getTitle(account_id, book_id, token); + 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); + } } /// 本のタイトル変更 @Path("/{account_id}/books/{book_id}/title") @PUT + @Produces(MediaType.TEXT_PLAIN) @Consumes(MediaType.APPLICATION_FORM_URLENCODED) - public void putTitle(@PathParam("account_id") String account_id, @PathParam("book_id") Integer book_id, @FormParam("title") String title, @FormParam("token") String token){ - if(!accountManager.checkToken(account_id, token)) return; - bookManager.putTitle(account_id, book_id, title, token); + 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.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"; + } } /// /accounts/{account_id}/books/{book_id}/public @@ -91,26 +126,44 @@ @GET @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.checkToken(account_id, token)) return null; - return bookManager.getPublicity(account_id, book_id, token); + 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); + } } /// 公開情報を変更する @Path("/{account_id}/books/{book_id}/public") @PUT + @Produces(MediaType.TEXT_PLAIN) @Consumes(MediaType.APPLICATION_FORM_URLENCODED) - public void putPublicity(@PathParam("account_id") String account_id, @PathParam("book_id") Integer book_id, @FormParam("publicity") Boolean publicity, @FormParam("token") String token){ - if(!accountManager.checkToken(account_id, token)) return; - bookManager.putPublicity(account_id, book_id, publicity, token); + 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.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"; + } } /// /accounts/{account_id}/books/{book_id}/color - /// 公開情報を変更する + /// 色を変更する @Path("/{account_id}/books/{book_id}/color") @PUT @Consumes(MediaType.APPLICATION_FORM_URLENCODED) - public void putColor(@PathParam("account_id") String account_id, @PathParam("book_id") Integer book_id, @FormParam("color") String color, @FormParam("token") String token){ - if(!accountManager.checkToken(account_id, token)) return; - bookManager.putColor(account_id, book_id, color, token); + 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.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"; + } } } \ No newline at end of file