diff --git a/build.gradle b/build.gradle index 3d91a94..297866c 100644 --- a/build.gradle +++ b/build.gradle @@ -1,7 +1,7 @@ plugins { id 'java' - id 'org.springframework.boot' version '3.2.5' - id 'io.spring.dependency-management' version '1.1.4' + id 'org.springframework.boot' version '2.6.7' + id 'io.spring.dependency-management' version '1.1.0' id 'war' } @@ -9,7 +9,7 @@ version = '0.0.1-SNAPSHOT' java { - sourceCompatibility = '21' + sourceCompatibility = '17' } repositories { diff --git a/src/main/java/org/ntlab/citrusserver/repositories/FavoriteManager.java b/src/main/java/org/ntlab/citrusserver/repositories/FavoriteManager.java index 3f801b0..63b7611 100644 --- a/src/main/java/org/ntlab/citrusserver/repositories/FavoriteManager.java +++ b/src/main/java/org/ntlab/citrusserver/repositories/FavoriteManager.java @@ -14,92 +14,128 @@ 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の削除の処理 + favoritedMap.remove(accountId); } + //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 cd816c4..7f6718d 100644 --- a/src/main/java/org/ntlab/citrusserver/repositories/ScheduleManager.java +++ b/src/main/java/org/ntlab/citrusserver/repositories/ScheduleManager.java @@ -19,9 +19,9 @@ public ScheduleManager(BookManager bookManager) { this.bookManager = bookManager; this.bookManager.addListener(this); - this.addSchedule("fish", 2024, 5, 28, "chinema", "10;00", "12:00", 0); + 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, "chinema", "10;00", "12:00", 0); + this.addSchedule("bird", 2024, 5, 28, "cinema", "10;00", "12:00", 0); this.addSchedule("bird", 2024, 5, 28, "test", "15;00", "18:00", 1); } @@ -141,12 +141,14 @@ @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())) { - schedules.get(account).get(year).get(month).get(day).get(scheduleId).setBookId(0); + if (schedules.containsKey(account)) { + 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); + } } } } diff --git a/src/main/java/org/ntlab/citrusserver/resources/AccountsRest.java b/src/main/java/org/ntlab/citrusserver/resources/AccountsRest.java index a305d5a..53c77bc 100644 --- a/src/main/java/org/ntlab/citrusserver/resources/AccountsRest.java +++ b/src/main/java/org/ntlab/citrusserver/resources/AccountsRest.java @@ -1,12 +1,9 @@ package org.ntlab.citrusserver.resources; -import jakarta.ws.rs.*; -import jakarta.ws.rs.core.MediaType; -import jakarta.ws.rs.core.Response; +import javax.ws.rs.*; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; import org.ntlab.citrusserver.entities.Account; -import org.ntlab.citrusserver.repositories.AccountManager; -import org.ntlab.citrusserver.repositories.BookManager; -import org.ntlab.citrusserver.repositories.ScheduleManager; -import org.ntlab.citrusserver.repositories.TodoManager; +import org.ntlab.citrusserver.repositories.*; import org.springframework.beans.factory.annotation.Autowired; //import org.springframework.http.HttpStatus; import org.springframework.stereotype.Component; @@ -17,19 +14,20 @@ @Path("/accounts") @Component //accountRestのインスタンスを一個作る - public class AccountsRest { private final AccountManager accountManager; //finalは書き換えられない private final BookManager bookManager; private final TodoManager todoManager; private final ScheduleManager scheduleManager; + private final FavoriteManager favoriteManager; @Autowired//springbootの決まり - public AccountsRest(AccountManager am, BookManager bm, TodoManager tm, ScheduleManager sm) { + public AccountsRest(AccountManager am, BookManager bm, TodoManager tm, ScheduleManager sm, FavoriteManager favoriteManager) { accountManager = am; bookManager = bm; todoManager = tm; scheduleManager = sm; + this.favoriteManager = favoriteManager; } // アカウントの一覧をリストとして返す(GET) @@ -82,6 +80,7 @@ bookManager.deleteAllBooks(accountId); todoManager.deleteAllTodosByAccountId(accountId); scheduleManager.deleteSchedules(accountId); + favoriteManager.removeFavoriteById(accountId); return; } //404 diff --git a/src/main/java/org/ntlab/citrusserver/resources/BooksRest.java b/src/main/java/org/ntlab/citrusserver/resources/BooksRest.java index 6e8982c..1a65b7b 100644 --- a/src/main/java/org/ntlab/citrusserver/resources/BooksRest.java +++ b/src/main/java/org/ntlab/citrusserver/resources/BooksRest.java @@ -1,11 +1,12 @@ package org.ntlab.citrusserver.resources; -import jakarta.ws.rs.*; -import jakarta.ws.rs.core.MediaType; -import jakarta.ws.rs.core.Response; +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.ntlab.citrusserver.repositories.TodoManager; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @@ -22,12 +23,14 @@ private final BookManager bookManager; private final AccountManager accountManager; private final TodoManager todoManager; + private final FavoriteManager favoriteManager; @Autowired // スプリングブートにいうサイン - public BooksRest(BookManager bm, AccountManager ac, TodoManager tm){//public クラス名()がコンストラクタ + public BooksRest(BookManager bm, AccountManager ac, TodoManager tm, FavoriteManager fm){//public クラス名()がコンストラクタ bookManager = bm; accountManager = ac; todoManager = tm; + favoriteManager = fm; } @@ -77,6 +80,7 @@ accountCheck(account_id); tokenCheck(account_id, token); todoManager.deleteAllTodosByBookId(account_id, book_id);//削除時、Todoも消す + favoriteManager.removeFavoriteByBookID(account_id, book_id);//削除時、Favoriteも消す bookManager.deleteBook(account_id, book_id); return "success"; } diff --git a/src/main/java/org/ntlab/citrusserver/resources/FavoritedRest.java b/src/main/java/org/ntlab/citrusserver/resources/FavoritedRest.java index 621ebf0..5a5a176 100644 --- a/src/main/java/org/ntlab/citrusserver/resources/FavoritedRest.java +++ b/src/main/java/org/ntlab/citrusserver/resources/FavoritedRest.java @@ -1,7 +1,9 @@ package org.ntlab.citrusserver.resources; -import jakarta.ws.rs.*; -import jakarta.ws.rs.core.MediaType; +import javax.ws.rs.*; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; + import org.ntlab.citrusserver.repositories.AccountManager; import org.ntlab.citrusserver.repositories.FavoriteManager; import org.springframework.beans.factory.annotation.Autowired; @@ -37,17 +39,25 @@ public void putFavorited(@PathParam("account_id") String account_id, @PathParam("book_id") Integer book_id, @PathParam("other_account_id") String other_account_id, @FormParam("token") String token){ if(accountManager.checkToken(other_account_id,token)) { favoriteManager.putFavorited(account_id, book_id, other_account_id); - favoriteManager.putFavorites(account_id, other_account_id, book_id);//変更点(要検討) + favoriteManager.putFavorites(other_account_id, account_id, book_id);//変更点(要検討) + }else{ + var response = Response.status(Response.Status.FORBIDDEN).entity("認証失敗"); + throw new WebApplicationException(response.build()); } } @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){ - if(accountManager.checkToken(account_id,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(other_account_id,token)) { favoriteManager.removeFavorited(account_id, book_id, other_account_id); - favoriteManager.removeFavorites(account_id, other_account_id, book_id);//変更点(要検討) + favoriteManager.removeFavorites(other_account_id, account_id, book_id);//変更点(要検討) + }else{ + var response = Response.status(Response.Status.FORBIDDEN).entity("認証失敗"); + throw new WebApplicationException(response.build()); } } } + + diff --git a/src/main/java/org/ntlab/citrusserver/resources/FavoritesRest.java b/src/main/java/org/ntlab/citrusserver/resources/FavoritesRest.java index c90074a..43d30db 100644 --- a/src/main/java/org/ntlab/citrusserver/resources/FavoritesRest.java +++ b/src/main/java/org/ntlab/citrusserver/resources/FavoritesRest.java @@ -1,9 +1,9 @@ package org.ntlab.citrusserver.resources; -import jakarta.ws.rs.*; -import jakarta.ws.rs.core.MediaType; -import jakarta.ws.rs.core.Response; +import javax.ws.rs.*; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; import org.ntlab.citrusserver.repositories.AccountManager; import org.ntlab.citrusserver.repositories.FavoriteManager; import org.springframework.beans.factory.annotation.Autowired; diff --git a/src/main/java/org/ntlab/citrusserver/resources/HelloWorldRest.java b/src/main/java/org/ntlab/citrusserver/resources/HelloWorldRest.java index d612750..2e97c7f 100644 --- a/src/main/java/org/ntlab/citrusserver/resources/HelloWorldRest.java +++ b/src/main/java/org/ntlab/citrusserver/resources/HelloWorldRest.java @@ -1,7 +1,7 @@ package org.ntlab.citrusserver.resources; -import jakarta.ws.rs.*; -import jakarta.ws.rs.core.MediaType; +import javax.ws.rs.*; +import javax.ws.rs.core.MediaType; import org.springframework.stereotype.Component; @Path("/helloworld") diff --git a/src/main/java/org/ntlab/citrusserver/resources/PublicBooksRest.java b/src/main/java/org/ntlab/citrusserver/resources/PublicBooksRest.java index 192022f..4820154 100644 --- a/src/main/java/org/ntlab/citrusserver/resources/PublicBooksRest.java +++ b/src/main/java/org/ntlab/citrusserver/resources/PublicBooksRest.java @@ -1,7 +1,7 @@ package org.ntlab.citrusserver.resources; -import jakarta.ws.rs.*; -import jakarta.ws.rs.core.MediaType; +import javax.ws.rs.*; +import javax.ws.rs.core.MediaType; import org.ntlab.citrusserver.entities.Book; import org.ntlab.citrusserver.repositories.PublicBookManager; import org.springframework.beans.factory.annotation.Autowired; diff --git a/src/main/java/org/ntlab/citrusserver/resources/ScheduleRest.java b/src/main/java/org/ntlab/citrusserver/resources/ScheduleRest.java index 5d08c8d..f880c8a 100644 --- a/src/main/java/org/ntlab/citrusserver/resources/ScheduleRest.java +++ b/src/main/java/org/ntlab/citrusserver/resources/ScheduleRest.java @@ -1,11 +1,11 @@ package org.ntlab.citrusserver.resources; -import jakarta.ws.rs.core.Response; +import javax.ws.rs.core.Response; import org.ntlab.citrusserver.entities.Schedule; import org.ntlab.citrusserver.repositories.AccountManager; import org.ntlab.citrusserver.repositories.ScheduleManager; -import jakarta.ws.rs.*; -import jakarta.ws.rs.core.MediaType; +import javax.ws.rs.*; +import javax.ws.rs.core.MediaType; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; diff --git a/src/main/java/org/ntlab/citrusserver/resources/TodoRest.java b/src/main/java/org/ntlab/citrusserver/resources/TodoRest.java index 22c1f7d..b94a8bc 100644 --- a/src/main/java/org/ntlab/citrusserver/resources/TodoRest.java +++ b/src/main/java/org/ntlab/citrusserver/resources/TodoRest.java @@ -1,8 +1,8 @@ package org.ntlab.citrusserver.resources; -import jakarta.ws.rs.*; -import jakarta.ws.rs.core.MediaType; -import jakarta.ws.rs.core.Response; +import javax.ws.rs.*; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; import org.apache.coyote.http11.upgrade.UpgradeServletOutputStream; import org.ntlab.citrusserver.entities.Todo; import org.ntlab.citrusserver.repositories.AccountManager;