diff --git a/src/main/java/org/ntlab/citrusserver/resources/FavoritesRest.java b/src/main/java/org/ntlab/citrusserver/resources/FavoritesRest.java index 43d30db..396b7dc 100644 --- a/src/main/java/org/ntlab/citrusserver/resources/FavoritesRest.java +++ b/src/main/java/org/ntlab/citrusserver/resources/FavoritesRest.java @@ -4,7 +4,10 @@ import javax.ws.rs.*; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; + +import org.ntlab.citrusserver.entities.Book; 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; @@ -19,29 +22,43 @@ public class FavoritesRest { private final FavoriteManager favoriteManager; private final AccountManager accountManager; + private final BookManager bookManager; @Autowired - public FavoritesRest(FavoriteManager fm, AccountManager am){ + public FavoritesRest(FavoriteManager fm, AccountManager am, BookManager bm){ favoriteManager = fm; accountManager = am; + bookManager = bm; } @Path("/{account_id}/favorites") @GET @Produces(MediaType.APPLICATION_JSON) - public HashMap> getFavoritesBooks(@PathParam("account_id") String accountId, @QueryParam("token") String token){ - if(favoriteManager.getFavorites(accountId) == null){ + public HashMap> getFavoritesBooks(@PathParam("account_id") String myAccountId, @QueryParam("token") String token){ + HashMap> LoverMap = new HashMap<>(); + + if(favoriteManager.getFavorites(myAccountId) == null){ var response = Response.status(Response.Status.NOT_FOUND).entity("アカウントが見つかりません"); throw new WebApplicationException(response.build()); } else{ - if(!accountManager.checkToken(accountId, token)) { + if(!accountManager.checkToken(myAccountId, token)) { var response = Response.status(Response.Status.FORBIDDEN).entity("認証失敗"); throw new WebApplicationException(response.build()); } else{ - return favoriteManager.getFavorites(accountId); + HashMap> accountIdToBooks = favoriteManager.getFavorites(myAccountId); + for (String otherAccountId: accountIdToBooks.keySet()) { // いいねした人ごと + HashMap bookMap = new HashMap<>(); // 小さいほうのMapの空 + HashSet books = accountIdToBooks.get(otherAccountId); // いいねした人のValue = 本のIDの一覧 + for (Integer bookId : books) { //本のIDの一覧をbookIdで回して、見ていく + Book book = bookManager.getBook(otherAccountId, bookId); // 見ていっている本のIDをBookクラスから取ってくる + bookMap.put(bookId,book); // 小さいMapにキーとValueを追加していってる。bookMap完成 + } + LoverMap.put(otherAccountId, bookMap); + } } + return LoverMap; } }