diff --git a/src/main/java/org/ntlab/citrusserver/resources/FavoritedRest.java b/src/main/java/org/ntlab/citrusserver/resources/FavoritedRest.java new file mode 100644 index 0000000..966e2c9 --- /dev/null +++ b/src/main/java/org/ntlab/citrusserver/resources/FavoritedRest.java @@ -0,0 +1,52 @@ +package org.ntlab.citrusserver.resources; + +import jakarta.ws.rs.*; +import jakarta.ws.rs.core.MediaType; +import org.ntlab.citrusserver.repositories.AccountManager; +import org.ntlab.citrusserver.repositories.FavoriteManager; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import java.util.HashSet; + +@Path("/accounts") +@Component + +public class FavoritedRest { + private FavoriteManager favoriteManager; + private final AccountManager accountManager; + + @Autowired + public FavoritedRest(FavoriteManager favoriteManager, AccountManager accountManager){ + this.favoriteManager = favoriteManager; + this.accountManager = accountManager; + } + @Path("/{account_id}/books/{book_id}/favorited") + @GET + @Produces(MediaType.APPLICATION_JSON) + public HashSet getFavorited(@PathParam("account_id") String account_id, @PathParam("book_id") Integer book_id, @QueryParam("token") String token){ + if (accountManager.checkToken(account_id, token)) { + return favoriteManager.getFavorited(account_id, book_id); + } + return null; + } + + @Path("/{account_id}/books/{book_id}/favorited/{other_account_id}") + @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(account_id,token)) { + favoriteManager.putFavorited(account_id, book_id, other_account_id); + } + } + + @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, @FormParam("token") String token){ + if(accountManager.checkToken(account_id,token)) { + favoriteManager.removeFavorited(account_id, book_id, other_account_id); + } + } +} +