diff --git a/src/main/java/org/ntlab/citrusserver/repositories/FavoriteManager.java b/src/main/java/org/ntlab/citrusserver/repositories/FavoriteManager.java index 3f801b0..1340359 100644 --- a/src/main/java/org/ntlab/citrusserver/repositories/FavoriteManager.java +++ b/src/main/java/org/ntlab/citrusserver/repositories/FavoriteManager.java @@ -14,92 +14,127 @@ 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; //アカウントが存在しない + if (favoritedMap.get(accountId).get(bookId) == null) return null; //bookIdが存在しない + 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); } +// //delete +// //accountIdが本(bookId)を消した時、otherAccountIdが消える +// public void removeFavoritedByBookId(String accountId, int bookId) { +// for (String otherAccountId: favoritedMap.keySet()) { +// favoritedMap.get(accountId).remove(bookId); +// } +// } +// +// //delete +// //accountIdが消えた時、bookIdとotherAccountIdが消える +// public void removeFavoritedByAccountId(String accountId) { +// favoritedMap.remove(accountId); +// } + //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 (String otherAccountId : favoritesMap.get(accountId).keySet()) { //favoritedの削除の処理 for (Integer bookId : favoritesMap.get(accountId).get(otherAccountId)) { favoritedMap.get(otherAccountId).get(bookId).remove(accountId); } } - favoritesMap.remove(accountId); + favoritesMap.remove(accountId); //favoritesの削除の処理 } + //delete //本が消されたときに一緒に消す public void removeFavoriteByBookID(String accountId, Integer bookId) { - for (String otherAccountId : favoritedMap.get(accountId).get(bookId)) { + for (String otherAccountId : favoritedMap.get(accountId).get(bookId)) { //favoritesの処理 favoritesMap.get(otherAccountId).get(accountId).remove(bookId); } - favoritedMap.get(accountId).remove(bookId); + favoritedMap.get(accountId).remove(bookId); //favoritedの処理 } } diff --git a/src/main/java/org/ntlab/citrusserver/repositories/ScheduleManager.java b/src/main/java/org/ntlab/citrusserver/repositories/ScheduleManager.java index 13a5982..313d487 100644 --- a/src/main/java/org/ntlab/citrusserver/repositories/ScheduleManager.java +++ b/src/main/java/org/ntlab/citrusserver/repositories/ScheduleManager.java @@ -9,7 +9,7 @@ import java.util.HashMap; @Repository -public class ScheduleManager implements IBookListener{ +public class ScheduleManager implements IBookListener { private final BookManager bookManager; @@ -19,55 +19,60 @@ public ScheduleManager(BookManager bookManager) { this.bookManager = bookManager; this.bookManager.addListener(this); + this.addSchedule("fish", 2024, 5, 28, "cinema", "10;00", "12:00", 0); + this.addSchedule("fish", 2024, 5, 28, "test", "15;00", "18:00", 1); + this.addSchedule("bird", 2024, 5, 28, "cinema", "10;00", "12:00", 0); + this.addSchedule("bird", 2024, 5, 28, "test", "15;00", "18:00", 1); + } //管理 private final HashMap>>>> schedules + HashMap>>>> schedules = new HashMap<>(); - public HashMap>>> getScheduleAll(String accountId) { + public HashMap>>> getScheduleAll(String accountId) { return schedules.get(accountId); } - public HashMap>> getScheduleYear(String accountId, int year){ + public HashMap>> getScheduleYear(String accountId, int year) { return schedules.get(accountId).get(year); } - public HashMap> getScheduleMonth(String accountId, int year, int month){ + public HashMap> getScheduleMonth(String accountId, int year, int month) { return schedules.get(accountId).get(year).get(month); } - public HashMap getScheduleDay(String accountId, int year, int month, int day){ + public HashMap getScheduleDay(String accountId, int year, int month, int day) { return schedules.get(accountId).get(year).get(month).get(day); } - public Schedule addSchedule(String accountId, int year, int month, int day, String title, String startTime, String endTime, Integer bookId){ - if(bookId == null || bookId.equals(0)){ + public Schedule addSchedule(String accountId, int year, int month, int day, String title, String startTime, String endTime, Integer bookId) { + if (bookId == null || bookId.equals(0)) { bookId = 0; - } else if (bookManager.getBooks(accountId)==null) { + } else if (bookManager.getBooks(accountId) == null) { return null; - } else if(!bookManager.getBooks(accountId).containsKey(bookId))return null; + } else if (!bookManager.getBooks(accountId).containsKey(bookId)) return null; - if(!schedules.containsKey(accountId)){ + if (!schedules.containsKey(accountId)) { schedules.put(accountId, new HashMap<>()); } - if(!schedules.get(accountId).containsKey(year)){ + if (!schedules.get(accountId).containsKey(year)) { schedules.get(accountId).put(year, new HashMap<>()); } - if(!schedules.get(accountId).get(year).containsKey(month)){ + if (!schedules.get(accountId).get(year).containsKey(month)) { schedules.get(accountId).get(year).put(month, new HashMap<>()); } - if(!schedules.get(accountId).get(year).get(month).containsKey(day)){ + if (!schedules.get(accountId).get(year).get(month).containsKey(day)) { schedules.get(accountId).get(year).get(month).put(day, new HashMap<>()); } String accountSchedule = accountId + year + month + day;// - if(!nextScheduleId.containsKey(accountSchedule)){ + if (!nextScheduleId.containsKey(accountSchedule)) { nextScheduleId.put(accountSchedule, 0); } @@ -78,48 +83,48 @@ return newSchedule; } - public Schedule getScheduleId(String accountId, int year, int month, int day, int scheduleId){ + public Schedule getScheduleId(String accountId, int year, int month, int day, int scheduleId) { return schedules.get(accountId).get(year).get(month).get(day).get(scheduleId); } - public void deleteSchedule(String accountId, int year, int month, int day, int scheduleId){ + public void deleteSchedule(String accountId, int year, int month, int day, int scheduleId) { schedules.get(accountId).get(year).get(month).get(day).remove(scheduleId); } - public void setScheduleStartTime(String accountId, int year, int month, int day, int scheduleId, String newTime){ + public void setScheduleStartTime(String accountId, int year, int month, int day, int scheduleId, String newTime) { schedules.get(accountId).get(year).get(month).get(day).get(scheduleId).setStartTime(newTime); } - public void setScheduleEndTime(String accountId, int year, int month, int day, int scheduleId, String newTime){ + public void setScheduleEndTime(String accountId, int year, int month, int day, int scheduleId, String newTime) { schedules.get(accountId).get(year).get(month).get(day).get(scheduleId).setEndTime(newTime); } - public void changeScheduleTitle(String accountId, int year, int month, int day, int scheduleId, String newTitle){ + public void changeScheduleTitle(String accountId, int year, int month, int day, int scheduleId, String newTitle) { schedules.get(accountId).get(year).get(month).get(day).get(scheduleId).setTitle(newTitle); } //voidからintに変更 1で成功 -1でエラー処理 - public int setSchedulesBookId(String accountId, int year, int month, int day, int scheduleId, Integer newBookId){ - if(newBookId == null || newBookId.equals(0)){ + public int setSchedulesBookId(String accountId, int year, int month, int day, int scheduleId, Integer newBookId) { + if (newBookId == null || newBookId.equals(0)) { newBookId = 0; - }else if (bookManager.getBooks(accountId)==null) { + } else if (bookManager.getBooks(accountId) == null) { return -1; - }else if(!bookManager.getBooks(accountId).containsKey(newBookId))return -1; + } else if (!bookManager.getBooks(accountId).containsKey(newBookId)) return -1; schedules.get(accountId).get(year).get(month).get(day).get(scheduleId).setBookId(newBookId); return 1; } - public void deleteSchedules(String accountId){ + public void deleteSchedules(String accountId) { schedules.remove(accountId); } - public void deleteScheduleBookId(String accountId, Integer bookId){ - for(int year : schedules.get(accountId).keySet()){ - for (int month : schedules.get(accountId).get(year).keySet()){ - for(int day : schedules.get(accountId).get(year).get(month).keySet()){ - for (int scheduleId : schedules.get(accountId).get(year).get(month).get(day).keySet()){ - if(schedules.get(accountId).get(year).get(month).get(day).get(scheduleId).getBookId().equals(bookId)){ + public void deleteScheduleBookId(String accountId, Integer bookId) { + for (int year : schedules.get(accountId).keySet()) { + for (int month : schedules.get(accountId).get(year).keySet()) { + for (int day : schedules.get(accountId).get(year).get(month).keySet()) { + for (int scheduleId : schedules.get(accountId).get(year).get(month).get(day).keySet()) { + if (schedules.get(accountId).get(year).get(month).get(day).get(scheduleId).getBookId().equals(bookId)) { schedules.get(accountId).get(year).get(month).get(day).get(scheduleId).setBookId(0); } } @@ -136,11 +141,11 @@ @Override public void bookDeleted(Account accountIn, Book book) { String account = accountIn.getId(); - for(int year : schedules.get(account).keySet()){ - for (int month : schedules.get(account).get(year).keySet()){ - for(int day : schedules.get(account).get(year).get(month).keySet()){ - for (int scheduleId : schedules.get(account).get(year).get(month).get(day).keySet()){ - if(schedules.get(account).get(year).get(month).get(day).get(scheduleId).getBookId().equals(book.getBookId())){ + for (int year : schedules.get(account).keySet()) { + for (int month : schedules.get(account).get(year).keySet()) { + for (int day : schedules.get(account).get(year).get(month).keySet()) { + for (int scheduleId : schedules.get(account).get(year).get(month).get(day).keySet()) { + if (schedules.get(account).get(year).get(month).get(day).get(scheduleId).getBookId().equals(book.getBookId())) { schedules.get(account).get(year).get(month).get(day).get(scheduleId).setBookId(0); } } @@ -148,82 +153,4 @@ } } } - - public void autoAddSchedule(){ - String accountId1 = "fish"; - int year = 2024; - int month = 5; - int day = 28; - String title1 = "cinema"; - String startTime1 = "13:00"; - String endTime1 = "14:00"; - Integer bookId1 = 0; - - String accountId2 = "bird"; - - String title2 = "test"; - String startTime2 = "10:00"; - String endTime2 = "12:00"; - Integer bookId2 = 1; - - //account1 - if(!schedules.containsKey(accountId1)){ - schedules.put(accountId1, new HashMap<>()); - } - if(!schedules.get(accountId1).containsKey(year)){ - schedules.get(accountId1).put(year, new HashMap<>()); - } - if(!schedules.get(accountId1).get(year).containsKey(month)){ - schedules.get(accountId1).get(year).put(month, new HashMap<>()); - } - if(!schedules.get(accountId1).get(year).get(month).containsKey(day)){ - schedules.get(accountId1).get(year).get(month).put(day, new HashMap<>()); - } - - String accountSchedule1 = accountId1 + year + month + day;// - - if(!nextScheduleId.containsKey(accountSchedule1)){ - nextScheduleId.put(accountSchedule1, 0); - } - - int newScheduleId1 = nextScheduleId.get(accountSchedule1); - Schedule newSchedule11 = new Schedule(title1, startTime1, endTime1, bookId1, newScheduleId1); - Schedule newSchedule12 = new Schedule(title2, startTime2, endTime2, bookId2, newScheduleId1); - - schedules.get(accountId1).get(year).get(month).get(day).put(newScheduleId1, newSchedule11); - nextScheduleId.put(accountSchedule1, newScheduleId1 + 1); - - schedules.get(accountId1).get(year).get(month).get(day).put(newScheduleId1, newSchedule12); - nextScheduleId.put(accountSchedule1, newScheduleId1 + 1); - - //account2 - - if(!schedules.containsKey(accountId2)){ - schedules.put(accountId2, new HashMap<>()); - } - if(!schedules.get(accountId2).containsKey(year)){ - schedules.get(accountId2).put(year, new HashMap<>()); - } - if(!schedules.get(accountId2).get(year).containsKey(month)){ - schedules.get(accountId2).get(year).put(month, new HashMap<>()); - } - if(!schedules.get(accountId2).get(year).get(month).containsKey(day)){ - schedules.get(accountId2).get(year).get(month).put(day, new HashMap<>()); - } - - String accountSchedule2 = accountId2 + year + month + day;// - if(!nextScheduleId.containsKey(accountSchedule2)){ - nextScheduleId.put(accountSchedule2, 0); - } - - int newScheduleId2 = nextScheduleId.get(accountSchedule2); - Schedule newSchedule21 = new Schedule(title1, startTime1, endTime1 , bookId1, newScheduleId2); - Schedule newSchedule22 = new Schedule(title2, startTime2, endTime2 , bookId2, newScheduleId2); - - schedules.get(accountId2).get(year).get(month).get(day).put(newScheduleId2, newSchedule21); - nextScheduleId.put(accountSchedule2, newScheduleId2 + 1); - - schedules.get(accountId2).get(year).get(month).get(day).put(newScheduleId2, newSchedule22); - nextScheduleId.put(accountSchedule2, newScheduleId2 + 1); - } } diff --git a/src/main/java/org/ntlab/citrusserver/resources/FavoritedRest.java b/src/main/java/org/ntlab/citrusserver/resources/FavoritedRest.java index 3609d53..b1e72fa 100644 --- a/src/main/java/org/ntlab/citrusserver/resources/FavoritedRest.java +++ b/src/main/java/org/ntlab/citrusserver/resources/FavoritedRest.java @@ -44,7 +44,7 @@ @Path("/{account_id}/books/{book_id}/favorited/{other_account_id}") @DELETE @Produces(MediaType.APPLICATION_FORM_URLENCODED) - public void removeFavorited(@PathParam("account_id") String account_id, @PathParam("book_id") Integer book_id, @PathParam("other_account_id") String other_account_id, @FormParam("token") String token){ + public void removeFavorited(@PathParam("account_id") String account_id, @PathParam("book_id") Integer book_id, @PathParam("other_account_id") String other_account_id, @QueryParam("token") String token){ if(accountManager.checkToken(account_id,token)) { favoriteManager.removeFavorited(account_id, book_id, other_account_id); favoriteManager.removeFavorites(other_account_id, account_id, book_id);//変更点(要検討)