diff --git a/src/main/java/org/ntlab/citrusserver/resources/FavoritedRest.java b/src/main/java/org/ntlab/citrusserver/resources/FavoritedRest.java index 5a5a176..28428b3 100644 --- a/src/main/java/org/ntlab/citrusserver/resources/FavoritedRest.java +++ b/src/main/java/org/ntlab/citrusserver/resources/FavoritedRest.java @@ -5,6 +5,7 @@ import javax.ws.rs.core.Response; import org.ntlab.citrusserver.repositories.AccountManager; +import org.ntlab.citrusserver.repositories.BookManager; import org.ntlab.citrusserver.repositories.FavoriteManager; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @@ -15,13 +16,16 @@ @Component public class FavoritedRest { + private final BookManager bookManager; private FavoriteManager favoriteManager; private final AccountManager accountManager; + @Autowired - public FavoritedRest(FavoriteManager favoriteManager, AccountManager accountManager){ + public FavoritedRest(FavoriteManager favoriteManager, AccountManager accountManager, BookManager bookManager){ this.favoriteManager = favoriteManager; this.accountManager = accountManager; + this.bookManager = bookManager; } @Path("/{account_id}/books/{book_id}/favorited") @GET @@ -37,25 +41,38 @@ @PUT @Produces(MediaType.APPLICATION_FORM_URLENCODED) public void putFavorited(@PathParam("account_id") String account_id, @PathParam("book_id") Integer book_id, @PathParam("other_account_id") String other_account_id, @FormParam("token") String token){ - if(accountManager.checkToken(other_account_id,token)) { - favoriteManager.putFavorited(account_id, book_id, other_account_id); - favoriteManager.putFavorites(other_account_id, account_id, book_id);//変更点(要検討) - }else{ - var response = Response.status(Response.Status.FORBIDDEN).entity("認証失敗"); - throw new WebApplicationException(response.build()); + if(accountManager.getAccount(account_id) != null) { + if(accountManager.getAccount(other_account_id) != null){ + if (accountManager.checkToken(other_account_id, token)) { + if (bookManager.getBook(account_id, book_id) != null) { + favoriteManager.putFavorited(account_id, book_id, other_account_id); + favoriteManager.putFavorites(other_account_id, account_id, book_id);//変更点(要検討) + } else { + var response = Response.status(Response.Status.FORBIDDEN).entity("認証失敗"); + throw new WebApplicationException(response.build()); + } + } + } } + } @Path("/{account_id}/books/{book_id}/favorited/{other_account_id}") @DELETE @Produces(MediaType.APPLICATION_FORM_URLENCODED) public void removeFavorited(@PathParam("account_id") String account_id, @PathParam("book_id") Integer book_id, @PathParam("other_account_id") String other_account_id, @QueryParam("token") String token){ - if(accountManager.checkToken(other_account_id,token)) { - favoriteManager.removeFavorited(account_id, book_id, other_account_id); - favoriteManager.removeFavorites(other_account_id, account_id, book_id);//変更点(要検討) - }else{ - var response = Response.status(Response.Status.FORBIDDEN).entity("認証失敗"); - throw new WebApplicationException(response.build()); + if(accountManager.getAccount(account_id) != null){ + if(accountManager.getAccount(other_account_id) != null){ + if(accountManager.checkToken(other_account_id,token)) { + if(bookManager.getBook(account_id,book_id) != null){ + favoriteManager.removeFavorited(account_id, book_id, other_account_id); + favoriteManager.removeFavorites(other_account_id, account_id, book_id);//変更点(要検討) + }else{ + var response = Response.status(Response.Status.FORBIDDEN).entity("認証失敗"); + throw new WebApplicationException(response.build()); + } + } + } } } }