diff --git a/src/main/java/org/ntlab/citrusserver/repositories/AccountManager.java b/src/main/java/org/ntlab/citrusserver/repositories/AccountManager.java index 602395f..bd65aae 100644 --- a/src/main/java/org/ntlab/citrusserver/repositories/AccountManager.java +++ b/src/main/java/org/ntlab/citrusserver/repositories/AccountManager.java @@ -19,16 +19,14 @@ // account_idとpasswordを設定し新しいアカウントを作成する(POST) public String createAccount(String accountId, String password) { + UUID str = UUID.randomUUID(); + String token = str.toString(); + Account account = new Account(accountId, password); if(!accounts.containsKey(accountId)) { - UUID str = UUID.randomUUID(); - String token = str.toString(); - Account account = new Account(accountId, password); accounts.put(accountId, account); accountToken.put(accountId, token); //accountIDとtokenをHashMapに入れる - return token; - } else { - return null; } + return token; } //accountIdとtokenを比較してtrueかfalseを返す 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..b3b0e12 --- /dev/null +++ b/src/main/java/org/ntlab/citrusserver/repositories/FavoriteManager.java @@ -0,0 +1,79 @@ +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; + + +import java.util.HashMap; +import java.util.HashSet; + +@Repository +public class FavoriteManager { + private final AccountManager accountManager; + + @Autowired + public FavoriteManager(AccountManager accountManager) { + this.accountManager = accountManager; + } + + private final HashMap>> //otheraccounts + favoritedMap = new HashMap<>(); + + + //book_idをいいねしたaccount_idをリストとして返す + public HashSet getFavorited(String accountId, int bookId) { + if(accountManager.getAccount(accountId) == null) return null; //アカウントが存在しない + + return favoritedMap.get(accountId).get(bookId); + } + + //other_account_idの人がaccount_idのbook_idにいいねをした時 + public void putFavorited(String accountId, int bookId, String otherAccountId) { + favoritedMap.get(accountId).get(bookId).add(otherAccountId); + } + + //イイねしたaccount_idをイイねした人リストから削除 + public void removeFavorited(String accountId, int bookId, String otherAccountId) { + favoritedMap.get(accountId).get(bookId).remove(otherAccountId); + } + + + + + HashMap>> favoritesMap = new HashMap(); + + + //いいねした本の一覧を返す + 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); + } + +} diff --git a/src/main/java/org/ntlab/citrusserver/resources/AccountsRest.java b/src/main/java/org/ntlab/citrusserver/resources/AccountsRest.java index 1a430e6..ed7b686 100644 --- a/src/main/java/org/ntlab/citrusserver/resources/AccountsRest.java +++ b/src/main/java/org/ntlab/citrusserver/resources/AccountsRest.java @@ -47,10 +47,6 @@ public String signup(@FormParam("account_id") String accountId, @FormParam("password") String password) { String token; token = accountManager.createAccount(accountId, password); - if (token == null){ - var response = Response.status(Response.Status.CONFLICT).entity("id '" + accountId + "' は既に存在します");//404 - throw new WebApplicationException(response.build()); - } return token; } diff --git a/src/main/java/org/ntlab/citrusserver/resources/BooksRest.java b/src/main/java/org/ntlab/citrusserver/resources/BooksRest.java index 6e8982c..3398b0b 100644 --- a/src/main/java/org/ntlab/citrusserver/resources/BooksRest.java +++ b/src/main/java/org/ntlab/citrusserver/resources/BooksRest.java @@ -39,9 +39,19 @@ @Produces(MediaType.APPLICATION_JSON) public HashMap getBooks(@PathParam("account_id") String account_id, @QueryParam("token") String token){ - accountCheck(account_id); - tokenCheck(account_id, token); - return bookManager.getBooks(account_id); + if(accountManager.getAccount(account_id) == null){ + var response = Response.status(Response.Status.NOT_FOUND).entity("アカウントが見つかりません"); + throw new WebApplicationException(response.build()); + } + else{ + if(!accountManager.checkToken(account_id, token)) { + var response = Response.status(Response.Status.FORBIDDEN).entity("認証失敗"); + throw new WebApplicationException(response.build()); + } + else{ + return bookManager.getBooks(account_id); + } + } } @Path("/{account_id}/books") @@ -50,9 +60,19 @@ @Consumes(MediaType.APPLICATION_FORM_URLENCODED) // postmanのbodyに入力する値がある時 public Book createBook(@PathParam("account_id") String account_id, @FormParam("title") String title, @FormParam("color") String color, @FormParam("publicity") Boolean publicity, @FormParam("token") String token) { - accountCheck(account_id); - tokenCheck(account_id, token); - return bookManager.createBook(account_id, title, color, publicity); + if (accountManager.getAccount(account_id) == null){ + var response = Response.status(Response.Status.NOT_FOUND).entity("アカウントが見つかりません"); + throw new WebApplicationException(response.build()); + } + else{ + if(!accountManager.checkToken(account_id, token)) { + var response = Response.status(Response.Status.FORBIDDEN).entity("認証失敗"); + throw new WebApplicationException(response.build()); + } + else { + return bookManager.createBook(account_id, title, color, publicity); + } + } } @@ -63,9 +83,19 @@ @Produces(MediaType.APPLICATION_JSON) public Book getBook(@PathParam("account_id") String account_id, @PathParam("book_id") Integer book_id, @QueryParam("token") String token){ - accountCheck(account_id); - tokenCheck(account_id, token); - return bookManager.getBook(account_id, book_id); + if (accountManager.getAccount(account_id) == null){ + var response = Response.status(Response.Status.NOT_FOUND).entity("アカウントが見つかりません"); + throw new WebApplicationException(response.build()); + } + else{ + if(!accountManager.checkToken(account_id, token)) { + var response = Response.status(Response.Status.FORBIDDEN).entity("認証失敗"); + throw new WebApplicationException(response.build()); + } + else{ + return bookManager.getBook(account_id, book_id); + } + } } /// 本の削除 @Path("/{account_id}/books/{book_id}") @@ -74,11 +104,21 @@ @Consumes(MediaType.APPLICATION_FORM_URLENCODED) public String deleteBook(@PathParam("account_id") String account_id, @PathParam("book_id") Integer book_id, @QueryParam("token") String token){ - accountCheck(account_id); - tokenCheck(account_id, token); - todoManager.deleteAllTodosByBookId(account_id, book_id);//削除時、Todoも消す - bookManager.deleteBook(account_id, book_id); - return "success"; + if(accountManager.getAccount(account_id) == null){ + var response = Response.status(Response.Status.NOT_FOUND).entity("アカウントが見つかりません"); + throw new WebApplicationException(response.build()); + } + else{ + if(!accountManager.checkToken(account_id, token)) { + var response = Response.status(Response.Status.FORBIDDEN).entity("認証失敗"); + throw new WebApplicationException(response.build()); + } + else{ + todoManager.deleteAllTodosByBookId(account_id, book_id);//削除時、Todoも消す + bookManager.deleteBook(account_id, book_id); + return "success"; + } + } } /// /{account_id}/books/{book_id}/title @@ -88,9 +128,19 @@ @Produces(MediaType.TEXT_PLAIN) public String getTitle(@PathParam("account_id") String account_id, @PathParam("book_id") Integer book_id, @QueryParam("token") String token){ - accountCheck(account_id); - tokenCheck(account_id, token); - return bookManager.getTitle(account_id, book_id); + if (accountManager.getAccount(account_id) == null) { + var response = Response.status(Response.Status.NOT_FOUND).entity("アカウントが見つかりません"); + throw new WebApplicationException(response.build()); + } + else{ + if(!accountManager.checkToken(account_id, token)) { + var response = Response.status(Response.Status.FORBIDDEN).entity("認証失敗"); + throw new WebApplicationException(response.build()); + } + else{ + return bookManager.getTitle(account_id, book_id); + } + } } /// 本のタイトル変更 @@ -100,10 +150,20 @@ @Consumes(MediaType.APPLICATION_FORM_URLENCODED) public String putTitle(@PathParam("account_id") String account_id, @PathParam("book_id") Integer book_id, @FormParam("title") String title, @FormParam("token") String token){ - accountCheck(account_id); - tokenCheck(account_id, token); - bookManager.putTitle(account_id, book_id, title); - return "success"; + if(accountManager.getAccount(account_id) == null){ + var response = Response.status(Response.Status.NOT_FOUND).entity("アカウントが見つかりません"); + throw new WebApplicationException(response.build()); + } + else{ + if(!accountManager.checkToken(account_id, token)) { + var response = Response.status(Response.Status.FORBIDDEN).entity("認証失敗"); + throw new WebApplicationException(response.build()); + } + else{ + bookManager.putTitle(account_id, book_id, title); + return "success"; + } + } } /// /accounts/{account_id}/books/{book_id}/public @@ -113,9 +173,19 @@ @Produces(MediaType.TEXT_PLAIN) public Boolean getPublicity(@PathParam("account_id") String account_id, @PathParam("book_id") Integer book_id, @QueryParam("token") String token){ - accountCheck(account_id); - tokenCheck(account_id, token); - return bookManager.getPublicity(account_id, book_id); + if (accountManager.getAccount(account_id) == null) { + var response = Response.status(Response.Status.NOT_FOUND).entity("アカウントが見つかりません"); + throw new WebApplicationException(response.build()); + } + else{ + if(!accountManager.checkToken(account_id, token)) { + var response = Response.status(Response.Status.FORBIDDEN).entity("認証失敗"); + throw new WebApplicationException(response.build()); + } + else{ + return bookManager.getPublicity(account_id, book_id); + } + } } /// 公開情報を変更する @@ -125,10 +195,20 @@ @Consumes(MediaType.APPLICATION_FORM_URLENCODED) public String putPublicity(@PathParam("account_id") String account_id, @PathParam("book_id") Integer book_id, @FormParam("publicity") Boolean publicity, @FormParam("token") String token){ - accountCheck(account_id); - tokenCheck(account_id, token); - bookManager.putPublicity(account_id, book_id, publicity); - return "success"; + if(accountManager.getAccount(account_id) == null){ + var response = Response.status(Response.Status.NOT_FOUND).entity("アカウントが見つかりません"); + throw new WebApplicationException(response.build()); + } + else{ + if(!accountManager.checkToken(account_id, token)) { + var response = Response.status(Response.Status.FORBIDDEN).entity("認証失敗"); + throw new WebApplicationException(response.build()); + } + else{ + bookManager.putPublicity(account_id, book_id, publicity); + return "success"; + } + } } /// /accounts/{account_id}/books/{book_id}/color @@ -138,27 +218,24 @@ @Consumes(MediaType.APPLICATION_FORM_URLENCODED) public String putColor(@PathParam("account_id") String account_id, @PathParam("book_id") Integer book_id, @FormParam("color") String color, @FormParam("token") String token){ - accountCheck(account_id); - tokenCheck(account_id, token); - bookManager.putColor(account_id, book_id, color); - return "success"; + if(accountManager.getAccount(account_id) == null){ + var response = Response.status(Response.Status.NOT_FOUND).entity("アカウントが見つかりません"); + throw new WebApplicationException(response.build()); + } + else{ + if(!accountManager.checkToken(account_id, token)) { + var response = Response.status(Response.Status.FORBIDDEN).entity("認証失敗"); + throw new WebApplicationException(response.build()); + } + else{ + bookManager.putColor(account_id, book_id, color); + return "success"; + } + } } ///--------------------------------------------------------------------- ///private ///--------------------------------------------------------------------- - private void accountCheck(String account_id){ - if(accountManager.getAccount(account_id) == null){ - var response = Response.status(Response.Status.NOT_FOUND).entity("アカウントが見つかりません"); - throw new WebApplicationException(response.build()); - } - } - - private void tokenCheck(String account_id, String token){ - if(!accountManager.checkToken(account_id, token)) { - var response = Response.status(Response.Status.FORBIDDEN).entity("認証失敗"); - throw new WebApplicationException(response.build()); - } - } } \ No newline at end of file diff --git a/src/main/java/org/ntlab/citrusserver/resources/PublicBooksRest.java b/src/main/java/org/ntlab/citrusserver/resources/PublicBooksRest.java index 0ca017f..8f5aac4 100644 --- a/src/main/java/org/ntlab/citrusserver/resources/PublicBooksRest.java +++ b/src/main/java/org/ntlab/citrusserver/resources/PublicBooksRest.java @@ -27,30 +27,30 @@ } - //検索条件を指定して本を検索(アカウントかタイトルか両方 ソート可能) + //検索条件を指定して本を検索(アカウントかタイトルか両方) @Path("/search") @GET @Produces(MediaType.APPLICATION_JSON) public ArrayList searchBooksByTitleAndAccount(@QueryParam("search_title") String search_title, @QueryParam("search_account_id") String search_account_id, @QueryParam("sort_by") Integer sort_by) { - if(sort_by == null) { //ソートしない場合 - if (search_title != null && search_account_id != null) { //タイトルとアカウントIDでの検索 + if(sort_by == null) { + if (search_title != null && search_account_id != null) { return publicBookManager.searchBooksByTitleAndAccount(search_title, search_account_id); - } else if (search_title != null) { //タイトルのみでの検索 + } else if (search_title != null) { return publicBookManager.searchBooksByTitle(search_title); - } else if (search_account_id != null) { //アカウントIDのみでの検索 + } else if (search_account_id != null) { return publicBookManager.searchBooksByAccount(search_account_id); - } else { //タイトルもアカウントIDもない場合(すべての本を返す) + } else { return publicBookManager.getAllPublicBooks(); } - } else { //ソートする場合 - if(search_title != null && search_account_id != null) { //タイトルとアカウントIDでの検索 + } else { + if(search_title != null && search_account_id != null) { return publicBookManager.searchBooksByTitleAndAccount(search_title, search_account_id, sort_by); - } else if(search_title != null) { //タイトルのみでの検索 + } else if(search_title != null) { return publicBookManager.searchBooksByTitle(search_title, sort_by); - } else if(search_account_id != null) { //アカウントIDのみでの検索 + } else if(search_account_id != null) { return publicBookManager.searchBooksByAccount(search_account_id, sort_by); - } else { //タイトルもアカウントIDもない場合(すべての本を返す) + } else { return publicBookManager.getAllPublicBooks(); } }