diff --git a/src/main/java/org/ntlab/citrusserver/resources/BooksRest.java b/src/main/java/org/ntlab/citrusserver/resources/BooksRest.java index 99802ce..49f2e4a 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, token); + } } @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, token); + } } @@ -54,8 +65,13 @@ @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, token); + } } /// 本の削除 @Path("/{account_id}/books/{book_id}") @@ -72,8 +88,13 @@ @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, token); + } } /// 本のタイトル変更 @@ -91,8 +112,13 @@ @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, token); + } } /// 公開情報を変更する @@ -105,7 +131,7 @@ } /// /accounts/{account_id}/books/{book_id}/color - /// 公開情報を変更する + /// 色を変更する @Path("/{account_id}/books/{book_id}/color") @PUT @Consumes(MediaType.APPLICATION_FORM_URLENCODED)