diff --git a/src/main/java/org/ntlab/citrusserver/repositories/FavoriteManager.java b/src/main/java/org/ntlab/citrusserver/repositories/FavoriteManager.java new file mode 100644 index 0000000..3649989 --- /dev/null +++ b/src/main/java/org/ntlab/citrusserver/repositories/FavoriteManager.java @@ -0,0 +1,50 @@ +package org.ntlab.citrusserver.repositories; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Repository; + +import java.util.HashMap; +import java.util.HashSet; + + +@Repository +public class FavoriteManager { + + HashMap>> favoritesMap = new HashMap(); + private final AccountManager accountManager; //仮 + + @Autowired + public FavoriteManager(AccountManager accountManager) { + this.accountManager = accountManager; + } + + //いいねした本の一覧を返す + public HashMap> getFavorites(String accountId) { + if(accountManager.getAccount(accountId) == null) return null; + return favoritesMap.get(accountId); + } + + //取得した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); + } + + //いいねした本を追加する + public void putFavorites(String accountId, String otherAccountId, Integer bookId) { + if(!favoritesMap.containsKey(accountId)){ + favoritesMap.put(accountId, new HashMap<>()); + } + if(!favoritesMap.get(accountId).containsKey(otherAccountId)){ + favoritesMap.get(accountId).put(otherAccountId, new HashSet<>()); + } + favoritesMap.get(accountId).get(otherAccountId).add(bookId); + + } + //いいねした本を消去する + public void removeFavorites(String accountId, String otherAccountId, Integer bookId) { + favoritesMap.get(accountId).get(otherAccountId).remove(bookId); + } + +}