diff --git a/src/main/java/org/ntlab/citrusserver/entities/BookModel.java b/src/main/java/org/ntlab/citrusserver/entities/BookModel.java new file mode 100644 index 0000000..24e1c09 --- /dev/null +++ b/src/main/java/org/ntlab/citrusserver/entities/BookModel.java @@ -0,0 +1,92 @@ +package org.ntlab.citrusserver.entities; + +public class BookModel { + + private int bookId; + private String title; + private boolean publicity; + private String color; + private String accountId; + private String time; + private String accountColor; + private int favoriteCount; + + public BookModel(){} + + public BookModel(int bookId, String title, boolean publicity, String color, String accountId, String time, String accountColor) { + this.bookId = bookId; + this.title = title; + this.publicity = publicity; + this.color = color; + this.accountId = accountId; + this.time = time; + this.accountColor = accountColor; + } + + public BookModel(Book book, String accountId, String accountColor) { + this.bookId = book.getBookId(); + this.title = book.getTitle(); + this.publicity = book.getPublicity(); + this.color = book.getColor(); + this.time = book.getTime(); + this.favoriteCount = book.getFavoritedCount(); + this.accountColor = accountColor; + this.accountId = accountId; + } + + public int getBookId() { + return bookId; + } + + public void setBookId(int bookId) { + this.bookId = bookId; + } + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + public boolean isPublicity() { + return publicity; + } + + public void setPublicity(boolean publicity) { + this.publicity = publicity; + } + + public String getColor() { + return color; + } + + public void setColor(String color) { + this.color = color; + } + + public String getAccountId() { + return accountId; + } + + public void setAccountId(String accountId) { + this.accountId = accountId; + } + + public String getTime() { + return time; + } + + public void setTime(String time) { + this.time = time; + } + + public String getAccountColor() { + return accountColor; + } + + public void setAccountColor(String accountColor) { + this.accountColor = accountColor; + } +} diff --git a/src/main/java/org/ntlab/citrusserver/repositories/FavoriteManager.java b/src/main/java/org/ntlab/citrusserver/repositories/FavoriteManager.java index fd8d085..f9b07c5 100644 --- a/src/main/java/org/ntlab/citrusserver/repositories/FavoriteManager.java +++ b/src/main/java/org/ntlab/citrusserver/repositories/FavoriteManager.java @@ -1,18 +1,27 @@ package org.ntlab.citrusserver.repositories; import org.ntlab.citrusserver.entities.Account; +import org.ntlab.citrusserver.entities.Book; +import org.ntlab.citrusserver.entities.BookModel; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Repository; +import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; +import java.util.List; @Repository public class FavoriteManager implements IAccountListener { + private final BookManager bookManager; + private final AccountManager accountManager; + @Autowired - public FavoriteManager(AccountManager accountManager) { + public FavoriteManager(AccountManager accountManager, BookManager bookManager) { accountManager.addListener(this); // bookManager.addListener(this); + this.accountManager = accountManager; + this.bookManager = bookManager; } @@ -74,6 +83,19 @@ return favoritesMap.get(accountId); //accountIdをgetすればそれ以降のotherAccountIDとbookIdが返ってくる } + public List getFavoriteList(String accountId) { + if (!favoritesMap.containsKey(accountId)) return null; + List res = new ArrayList<>(); + for(String aid: favoritesMap.get(accountId).keySet()) { + String color = accountManager.getAccountColor(aid); + for(int bid : favoritesMap.get(accountId).get(aid)) { + Book book = bookManager.getBook(aid, bid); + res.add(new BookModel(book, aid, color)); + } + } + return res; + } + //get //取得したotherAccountIdのいいねした本を返す: otherAccountIdがいいねしたAccountIdの本(bookId)の一覧 public HashSet getFavoritesByID(String accountId, String otherAccountId) { //このHashSetはbookId(int型)の集合 diff --git a/src/main/java/org/ntlab/citrusserver/resources/FavoritesRest.java b/src/main/java/org/ntlab/citrusserver/resources/FavoritesRest.java index 396b7dc..d496895 100644 --- a/src/main/java/org/ntlab/citrusserver/resources/FavoritesRest.java +++ b/src/main/java/org/ntlab/citrusserver/resources/FavoritesRest.java @@ -6,6 +6,7 @@ import javax.ws.rs.core.Response; import org.ntlab.citrusserver.entities.Book; +import org.ntlab.citrusserver.entities.BookModel; import org.ntlab.citrusserver.repositories.AccountManager; import org.ntlab.citrusserver.repositories.BookManager; import org.ntlab.citrusserver.repositories.FavoriteManager; @@ -14,6 +15,7 @@ import java.util.HashMap; import java.util.HashSet; +import java.util.List; import java.util.Set; @Path("/accounts") @@ -34,9 +36,7 @@ @Path("/{account_id}/favorites") @GET @Produces(MediaType.APPLICATION_JSON) - public HashMap> getFavoritesBooks(@PathParam("account_id") String myAccountId, @QueryParam("token") String token){ - HashMap> LoverMap = new HashMap<>(); - + public List getFavoritesBooks(@PathParam("account_id") String myAccountId, @QueryParam("token") String token){ if(favoriteManager.getFavorites(myAccountId) == null){ var response = Response.status(Response.Status.NOT_FOUND).entity("アカウントが見つかりません"); throw new WebApplicationException(response.build()); @@ -46,19 +46,7 @@ var response = Response.status(Response.Status.FORBIDDEN).entity("認証失敗"); throw new WebApplicationException(response.build()); } - else{ - 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; + return favoriteManager.getFavoriteList(myAccountId); } }