Merge pull request #153 from nitta-lab-2024/FavoritesRest
FavoritesRestを追加しました。
commit 6372cf750bd71f4cc07c2a9f1fe0f60ed5054b03
2 parents 0a40e09 + 30bb66f
渡辺智裕 authored on 23 May
Showing 1 changed file
View
67
src/main/java/org/ntlab/citrusserver/resources/FavoritesRest.java 0 → 100644
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);
}
}
}
}