| | 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<String, HashSet<Integer>> 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<Integer> 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); |
---|
| | } |
---|
| | } |
---|
| | } |
---|
| | } |
---|
| | |
---|
| | |