diff --git a/src/main/java/org/ntlab/citrusserver/repositories/FavoriteManager.java b/src/main/java/org/ntlab/citrusserver/repositories/FavoriteManager.java index 3f801b0..99afc17 100644 --- a/src/main/java/org/ntlab/citrusserver/repositories/FavoriteManager.java +++ b/src/main/java/org/ntlab/citrusserver/repositories/FavoriteManager.java @@ -14,77 +14,96 @@ this.accountManager = accountManager; } - private final HashMap>> //otheraccounts - favoritedMap = new HashMap<>(); + HashSet>> //otherAccountId + favoritedMap = new HashMap<>(); //favorited + // HashMap< String , HashMap< Integer , HashSet + // accountId(key) value + // bookId(key) value + // otherAccountId + + //favorites HashMap>> //bookId favoritesMap = new HashMap<>(); - //get - //book_idをいいねしたaccount_idをリストとして返す - public HashSet getFavorited(String accountId, int bookId) { - if (accountManager.getAccount(accountId) == null) return null; //アカウントが存在しない - return favoritedMap.get(accountId).get(bookId); + //favorited + + //get + //本をいいねしたアカウントをリストとして返す: accountIdの本(bookId)にいいねした人(otherAccount)を返す + public HashSet getFavorited(String accountId, int bookId) { //このHashSetはotherAccountId(string)の集合 + if (accountManager.getAccount(accountId) == null) return null; //アカウントが存在しない + return favoritedMap.get(accountId).get(bookId);//accountIdをgetして、bookIdまでgetしたら次otherAccount } //put - //other_account_idの人がaccount_idのbook_idにいいねをした時 + //otherAccountIdの人が本の持ち主(accountId)の本(bookId)にいいねをした時 public void putFavorited(String accountId, int bookId, String otherAccountId) { - if (!favoritedMap.containsKey(accountId)) { //accountがなかったらbookidとotheraccountsの空のハッシュマップを用意 + //accountIdに対して人が初めていいねする時 + if (!favoritedMap.containsKey(accountId)) { // 本の持ち主のaccountIdがなかったらbookidとotheraccountIdの空のハッシュマップを用意 favoritedMap.put(accountId, new HashMap<>()); } - if (!favoritedMap.get(accountId).containsKey(bookId)) { //bookIdがなかったらotheraccountsの空のハッシュセットを用意 + //本に対して初めていいねする時 + if (!favoritedMap.get(accountId).containsKey(bookId)) { //bookIdがなかったらotherAccountsの空のハッシュセットを用意 favoritedMap.get(accountId).put(bookId, new HashSet<>()); } - favoritedMap.get(accountId).get(bookId).add(otherAccountId); + //すでにいいねされているならこの処理 + favoritedMap.get(accountId).get(bookId).add(otherAccountId);//本の持ち主のaccountIdがあって本(bookId)があればotherAccountに追加 } //delete - //イイねしたaccount_idをイイねした人リストから削除 + //いいねした人(otherAccount)をいいねした人欄から削除: otherAccountをいいねした人欄から消すとき public void removeFavorited(String accountId, int bookId, String otherAccountId) { favoritedMap.get(accountId).get(bookId).remove(otherAccountId); } + //favorites - - //いいねした本の一覧を返す - public HashMap> getFavorites(String accountId) { + //get + //いいねした人の一覧を返す: accountIdの本(bookId)にいいねしたotherAccountIdとその本の一覧 + public HashMap> getFavorites(String accountId) { //otherAccountIDといいねしたbookIdの集合(本はaccountIdのもの限定) if (accountManager.getAccount(accountId) == null) return null; - return favoritesMap.get(accountId); + return favoritesMap.get(accountId); //accountIdをgetすればそれ以降のotherAccountIDとbookIdが返ってくる } - //取得したotherAccountIdのいいねした本を返す - public HashSet getFavoritesByID(String accountId, String otherAccountId) { - if (accountManager.getAccount(accountId) == null) return null; - if (!favoritesMap.get(accountId).containsKey(otherAccountId)) return null; - return favoritesMap.get(accountId).get(otherAccountId); + //get + //取得したotherAccountIdのいいねした本を返す: otherAccountIdがいいねしたAccountIdの本(bookId)の一覧 + public HashSet getFavoritesByID(String accountId, String otherAccountId) { //このHashSetはbookId(int型)の集合 + if (accountManager.getAccount(accountId) == null) return null; //accountIdがなかったらnull + if (!favoritesMap.get(accountId).containsKey(otherAccountId)) return null; // //otherAccountIdがなかったらnull + + return favoritesMap.get(accountId).get(otherAccountId);//otherAccountIdまでgetすればbookIdの集合が得られる } - //いいねした本を追加する + //put + //いいねした本を追加する: otherAccountIdがaccountIDの違う本にいいねしたときbookIdの集合にその本を追加 public void putFavorites(String accountId, String otherAccountId, Integer bookId) { - if (!favoritesMap.containsKey(accountId)) { - favoritesMap.put(accountId, new HashMap<>()); + if (!favoritesMap.containsKey(accountId)) { //まだだれもAccountIdの本にいいねしていないとき + favoritesMap.put(accountId, new HashMap<>()); //accountIdとhashmap(otherAccountIdとbookId)を用意 } - if (!favoritesMap.get(accountId).containsKey(otherAccountId)) { - favoritesMap.get(accountId).put(otherAccountId, new HashSet<>()); + if (!favoritesMap.get(accountId).containsKey(otherAccountId)) { //otherAccountIdが初めてaccountIdの本にいいねしたとき + favoritesMap.get(accountId).put(otherAccountId, new HashSet<>()); //otherAccountIdのbookIdのhashsetを用意 } favoritesMap.get(accountId).get(otherAccountId).add(bookId); } - //いいねした本を消去する + //delete + //いいねした本を消去する: otherAccountIdがbookIdのいいねを外した時そのbookIdが消える public void removeFavorites(String accountId, String otherAccountId, Integer bookId) { favoritesMap.get(accountId).get(otherAccountId).remove(bookId); } - //アカウントが消去されたときに一緒に消す + //delete + //アカウントが消去されたときに一緒に消す: すべて消える public void removeFavoriteById(String accountId) { for (String otherAccountId : favoritesMap.get(accountId).keySet()) { for (Integer bookId : favoritesMap.get(accountId).get(otherAccountId)) { @@ -94,6 +113,7 @@ favoritesMap.remove(accountId); } + //delete //本が消されたときに一緒に消す public void removeFavoriteByBookID(String accountId, Integer bookId) { for (String otherAccountId : favoritedMap.get(accountId).get(bookId)) {