diff --git a/src/main/java/org/ntlab/citrusserver/resources/FavoritesRest.java b/src/main/java/org/ntlab/citrusserver/resources/FavoritesRest.java new file mode 100644 index 0000000..5dd76fc --- /dev/null +++ b/src/main/java/org/ntlab/citrusserver/resources/FavoritesRest.java @@ -0,0 +1,66 @@ +package org.ntlab.citrusserver.resources; + + +import jakarta.ws.rs.*; +import jakarta.ws.rs.core.MediaType; +import jakarta.ws.rs.core.Response; +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.HashMap; +import java.util.HashSet; +import java.util.Set; + +@Path("/accounts") +@Component //accountRestのインスタンスを一個作る + +public class FavoritesRest { + private final FavoriteManager favoriteManager; + private final AccountManager accountManager; + + @Autowired + public FavoritesRest(FavoriteManager fm, AccountManager am){ + favoriteManager = fm; + accountManager = am; + } + + @Path("/{account_id}/favorites") + @GET + @Produces(MediaType.APPLICATION_JSON) + public HashMap> getFavoritesBooks(@PathParam("account_id") String accountId, @QueryParam("token") String token){ + if(FavoriteManager.getFavoritesBooks(accountId) == null){ + var response = Response.status(Response.Status.NOT_FOUND).entity("アカウントが見つかりません"); + throw new WebApplicationException(response.build()); + } + else{ + if(!accountManager.checkToken(accountId, token)) { + var response = Response.status(Response.Status.FORBIDDEN).entity("認証失敗"); + throw new WebApplicationException(response.build()); + } + else{ + return FavoriteManager.getFavoritesBooks(accountId); + } + } + } + + @Path("/{account_id}/favorites/{other_account_id}") + @GET + @Produces(MediaType.APPLICATION_JSON) + public HashSet getFavoritesBooksById(@PathParam("account_id") String accountId, @PathParam("other_account_id") String otherAccountId, @QueryParam("token") String token){ + if(FavoriteManager.getFavoritesBooksById(accountId) == null){ + var response = Response.status(Response.Status.NOT_FOUND).entity("アカウントが見つかりません"); + throw new WebApplicationException(response.build()); + } + else{ + if(!accountManager.checkToken(accountId, token)) { + var response = Response.status(Response.Status.FORBIDDEN).entity("認証失敗"); + throw new WebApplicationException(response.build()); + } + else{ + return FavoriteManager.getFavoritesBooks(accountId); + } + } + } +}