diff --git a/src/main/java/org/ntlab/citrusserver/entities/Schedule.java b/src/main/java/org/ntlab/citrusserver/entities/Schedule.java new file mode 100644 index 0000000..02cbcd3 --- /dev/null +++ b/src/main/java/org/ntlab/citrusserver/entities/Schedule.java @@ -0,0 +1,24 @@ +package org.ntlab.citrusserver.entities; + +public class Schedule { + String title; + String startTime; + String endTime; + Integer bookId; + + public Schedule(String t, String s, String e, Integer bid) { + title = t; + startTime = s; + endTime = e; + bookId = bid; + } + + public void setTitle(String t) {title = t;} + public String getTitle() {return title;} + public void setStartTime(String t) {startTime = t;} + public String getStartTime() {return startTime;} + public void setEndTime(String e) {endTime = e;} + public String getEndTime() {return endTime;} + public void setBookId(Integer bid) {bookId = bid;} + public Integer getBookId() {return bookId;} +} diff --git a/src/main/java/org/ntlab/citrusserver/repositories/AccountManager.java b/src/main/java/org/ntlab/citrusserver/repositories/AccountManager.java index e532b24..bd65aae 100644 --- a/src/main/java/org/ntlab/citrusserver/repositories/AccountManager.java +++ b/src/main/java/org/ntlab/citrusserver/repositories/AccountManager.java @@ -1,22 +1,16 @@ package org.ntlab.citrusserver.repositories; -import jakarta.ws.rs.*; -import jakarta.ws.rs.core.MediaType; import org.ntlab.citrusserver.entities.Account; -import org.ntlab.citrusserver.resources.AccountsRest; -import org.springframework.stereotype.Component; import org.springframework.stereotype.Repository; - -import java.lang.reflect.Array; import java.util.*; import java.util.HashMap; @Repository public class AccountManager { - private HashMap accounts = new HashMap(); //keyにaccountId,valueにaccount + private final HashMap accounts = new HashMap(); //keyにaccountId,valueにAccount - private HashMap accountToken = new HashMap<>(); //keyがaccountId,valueがtoken + private final HashMap accountToken = new HashMap<>(); //keyがaccountId,valueがtoken // アカウントの一覧をリストとして返す(GET) public Set getAccountsID() { @@ -37,7 +31,10 @@ //accountIdとtokenを比較してtrueかfalseを返す public boolean checkToken(String accountId, String token) { - return accountToken.get(accountId).equals(token); + if(accounts.containsKey(accountId)) { + return accountToken.get(accountId).equals(token); + } + return false; } // 指定されたアカウントの情報を返す(GET) @@ -46,18 +43,18 @@ } // アカウント情報を全削除する(DELETE) - public void deleteAccount(String accountId, String password, String token) { - if(accountToken.get(accountId).equals(token)) { - if(accounts.get(accountId).getPassword().equals(password)) { + public void deleteAccount(String accountId, String token, String password) { + if(accountToken.get(accountId).equals(token)) { //token比較 + if(accounts.get(accountId).getPassword().equals(password)) { //password比較 accounts.remove(accountId); } } } // 指定されたIDのパスワードを変更する (PUT) - public void changePassword(String accountId, String token, String oldPassword, String newPassword) { - if(accountToken.get(accountId).equals(token)) { - if(accounts.get(accountId).getPassword().equals(oldPassword)) { + public void changePassword(String accountId, String newPassword, String oldPassword, String token) { + if(accountToken.get(accountId).equals(token)) { //token比較 + if(accounts.get(accountId).getPassword().equals(oldPassword)) { //password比較 accounts.get(accountId).setPassword(newPassword); } } @@ -69,8 +66,8 @@ } // 指定されたIDの自己紹介を変更する (PUT) - public void changeIntroduction(String accountId, String token, String introduction) { - if(accountToken.get(accountId).equals(token)) { + public void changeIntroduction(String accountId, String introduction, String token) { + if(accountToken.get(accountId).equals(token)) { //token比較 accounts.get(accountId).setIntroduction(introduction); } } @@ -86,12 +83,12 @@ } // お気に入りの本のbook_idを削除する (DELETE) - public void deleteFavoriteBookId(String accountId, String token, String otherAccountId, Integer bookId) { + public void deleteFavoriteBookId(String token, String accountId, String otherAccountId, Integer bookId) { } // いいねした本のアカウントIDとbook_idを追加する(いいねした側に追加) (PUT) - public void putfavoriteid(String accountId, String token, String otherAccountId, Integer bookId) { + public void putFavoriteId(String accountId, String otherAccountId, Integer bookId, String token) { } diff --git a/src/main/java/org/ntlab/citrusserver/repositories/BookManager.java b/src/main/java/org/ntlab/citrusserver/repositories/BookManager.java index 122e63a..ba0980a 100644 --- a/src/main/java/org/ntlab/citrusserver/repositories/BookManager.java +++ b/src/main/java/org/ntlab/citrusserver/repositories/BookManager.java @@ -18,14 +18,18 @@ } //本の一覧を返す - public HashMap getBooks(String accountId) + public HashMap getBooks(String accountId, String token) { + if(!accountManager.checkToken(accountId, token)) return null; //tokenが違う時 + if(accountManager.getAccount(accountId) == null) return null; //アカウントが存在しない return booksMap.get(accountId); } //本の新規作成 - public int createBook(String accountId, String title, String color, Boolean publicity) + public Book createBook(String accountId, String title, String color, Boolean publicity, String token) { + if(!accountManager.checkToken(accountId, token)) return null; //tokenが違う時に返す + if(accountManager.getAccount(accountId) == null) return null; //アカウントが存在しない if(!booksMap.containsKey(accountId)){ booksMap.put(accountId, new HashMap<>()); } @@ -33,52 +37,66 @@ int newBookId = account.getNewBookId(); //新たに生成されたIdを取得(作成数もここで加算している) Book book = new Book(newBookId, title, publicity, color); //本の初期化 booksMap.get(accountId).put(newBookId, book); //ブックに追加 - return newBookId; + return booksMap.get(accountId).get(newBookId); //(int->Bookを返すように変更した) } //本の情報を取得 - public Book getBook(String accountId, Integer bookId) + public Book getBook(String accountId, Integer bookId, String token) { + if(!accountManager.checkToken(accountId, token)) return null; //tokenが違う時に返す + if(accountManager.getAccount(accountId) == null) return null; //アカウントが存在しない return booksMap.get(accountId).get(bookId); } //本の削除 - public void deleteBook(String accountId, Integer bookId) + public void deleteBook(String accountId, Integer bookId, String token) { + if(!accountManager.checkToken(accountId, token)) return; //tokenが違う時に返す + if(accountManager.getAccount(accountId) == null) return; //アカウントが存在しない booksMap.get(accountId).remove(bookId); } //((( いいねは省略 ))) //本のタイトルを返す - public String getTitle(String accountId, Integer bookId) + public String getTitle(String accountId, Integer bookId, String token) { + if(!accountManager.checkToken(accountId, token)) return null; //tokenが違う時に返す + if(accountManager.getAccount(accountId) == null) return null; //アカウントが存在しない return booksMap.get(accountId).get(bookId).getTitle(); } //本のタイトルを変更 - public void putTitle(String accountId, Integer bookId, String title) + public void putTitle(String accountId, Integer bookId, String title, String token) { + if(!accountManager.checkToken(accountId, token)) return; //tokenが違う時に返す + if(accountManager.getAccount(accountId) == null) return; //アカウントが存在しない booksMap.get(accountId).get(bookId).setTitle(title); } //本の公開情報を返す - public Boolean getPublicity(String accountId, Integer bookId) + public Boolean getPublicity(String accountId, Integer bookId, String token) { + if(!accountManager.checkToken(accountId, token)) return null; //tokenが違う時に返す + if(accountManager.getAccount(accountId) == null) return null; //アカウントが存在しない return booksMap.get(accountId).get(bookId).getPublicity(); } //公開情報を変更する - public void putPublicity(String accountId, Integer bookId, Boolean publicity) + public void putPublicity(String accountId, Integer bookId, Boolean publicity, String token) { + if(!accountManager.checkToken(accountId, token)) return; //tokenが違う時に返す + if(accountManager.getAccount(accountId) == null) return; //アカウントが存在しない booksMap.get(accountId).get(bookId).setPublicity(publicity); } //((( 目標・振り返りは省略 ))) //本の色を変更する - public void putColor(String accountId, Integer bookId, String color) + public void putColor(String accountId, Integer bookId, String color, String token) { + if(!accountManager.checkToken(accountId, token)) return; //tokenが違う時に返す + if(accountManager.getAccount(accountId) == null) return; //アカウントが存在しない booksMap.get(accountId).get(bookId).setColor(color); } } diff --git a/src/main/java/org/ntlab/citrusserver/repositories/TodoManager.java b/src/main/java/org/ntlab/citrusserver/repositories/TodoManager.java index 38d8fa5..3a0fcc5 100644 --- a/src/main/java/org/ntlab/citrusserver/repositories/TodoManager.java +++ b/src/main/java/org/ntlab/citrusserver/repositories/TodoManager.java @@ -1,7 +1,12 @@ package org.ntlab.citrusserver.repositories; import org.ntlab.citrusserver.entities.Todo; +import org.springframework.boot.autoconfigure.web.servlet.error.ErrorViewResolver; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Repository; +import org.springframework.web.client.HttpStatusCodeException; +import org.springframework.web.server.ResponseStatusException; import java.util.HashMap; @@ -17,14 +22,18 @@ HashMap>>>>> todos - = new HashMap<>(); + Todo>>>>>> todos = new HashMap<>(); /** * アカウントの本の次に与えるべきtodoのidを管理します */ private final HashMap nextTodoId = new HashMap<>(); + private final BookManager bookManager; + + public TodoManager(BookManager bookManager) { + this.bookManager = bookManager; + } /** * アカウントと本を指定してそれに所属するtodoをすべて返す @@ -44,6 +53,9 @@ */ public HashMap>>> getAllTodos(String accountId, int bookId, String token){ + if(!todos.containsKey(accountId)){ + return null; + } return todos.get(accountId).get(bookId); } @@ -57,6 +69,15 @@ * @return そのアカウントの本に所属するtodoのうち、指定した年月のtodoを返します */ public HashMap> getTodosByMonth(String accountId, int bookId, int year, int month, String token){ + if(!todos.containsKey(accountId)){ + return null; + } + if(!todos.get(accountId).get(bookId).containsKey(year)){ + return null; + } + if(!todos.get(accountId).get(bookId).get(year).containsKey(month)){ + return null; + } return todos.get(accountId).get(bookId).get(year).get(month); } @@ -86,6 +107,15 @@ * @return そのアカウントの本に所属するtodoのうち、指定した年月日のtodoを返します */ public HashMap getTodosByDay(String accountId, int bookId, int year, int month, int day, String token){ + if(!todos.get(accountId).get(bookId).containsKey(year)){ + return null; + } + if(!todos.get(accountId).get(bookId).get(year).containsKey(month)){ + return null; + } + if(!todos.get(accountId).get(bookId).get(year).get(month).containsKey(day)){ + return null; + } return todos.get(accountId).get(bookId).get(year).get(month).get(day); } @@ -118,6 +148,21 @@ * @return idを指定してtodoを返す */ public Todo getTodoById(String accountId, int bookId, int year, int month, int day, int todoId, String token){ + if(!todos.containsKey(accountId)){ + return null; + } + if(!todos.get(accountId).containsKey(bookId)){ + return null; + } + if(!todos.get(accountId).get(bookId).containsKey(year)){ + return null; + } + if(!todos.get(accountId).get(bookId).get(year).containsKey(month)){ + return null; + } + if(!todos.get(accountId).get(bookId).get(year).get(month).containsKey(day)){ + return null; + } return todos.get(accountId).get(bookId).get(year).get(month).get(day).get(todoId); } @@ -149,7 +194,10 @@ * @param title 追加するべきtodoのタイトル * @return 新しいtodoのid */ - public int createTodo(String accountId, int bookId, int year, int month, int day, String title, String token){ + public Todo createTodo(String accountId, int bookId, int year, int month, int day, String title, String token){ + if(bookManager.getBooks(accountId, token) == null){ + return null; + } if(!todos.containsKey(accountId)){ todos.put(accountId, new HashMap<>()); } @@ -165,6 +213,7 @@ if(!todos.get(accountId).get(bookId).get(year).get(month).containsKey(day)){ todos.get(accountId).get(bookId).get(year).get(month).put(day, new HashMap<>()); } + String accountBook = accountId + bookId + year + month + day; if(!nextTodoId.containsKey(accountBook)){ nextTodoId.put(accountBook, 0); @@ -174,7 +223,7 @@ Todo newTodo = new Todo(title, false, year, month, day, newTodoId); todos.get(accountId).get(bookId).get(year).get(month).get(day).put(newTodoId, newTodo); nextTodoId.put(accountBook, newTodoId + 1); - return newTodoId; + return newTodo; } /** @@ -186,7 +235,7 @@ * @param title 追加したいtodoのタイトル * @return 新しいtodoのid */ - public int createTodo(String accountId, int bookId, String yearMonthDay, String title, String token){ + public Todo createTodo(String accountId, int bookId, String yearMonthDay, String title, String token){ String[] yearMonthDays = yearMonthDay.split("-"); int year = Integer.parseInt(yearMonthDays[0]); int month = Integer.parseInt(yearMonthDays[1]); @@ -201,8 +250,27 @@ * @param bookId 本のid * @param todoId 削除したいtodoのid */ - public void deleteTodoById(String accountId, int bookId, int year, int month, int day, int todoId, String token){ + public int deleteTodoById(String accountId, int bookId, int year, int month, int day, int todoId, String token){ + if(!todos.containsKey(accountId)){ + return -1; + } + if(!todos.get(accountId).containsKey(bookId)){ + return -1; + } + if(!todos.get(accountId).get(bookId).containsKey(year)){ + return -1; + } + if(!todos.get(accountId).get(bookId).get(year).containsKey(month)){ + return -1; + } + if(!todos.get(accountId).get(bookId).get(year).get(month).containsKey(day)){ + return -1; + } + if(!todos.get(accountId).get(bookId).get(year).get(month).get(day).containsKey(todoId)){ + return -1; + } todos.get(accountId).get(bookId).get(year).get(month).get(day).remove(todoId); + return 1; } /** @@ -229,8 +297,27 @@ * @param todoId 変更したいtodoのid * @param check 変更後の達成状態 */ - public void setCheck(String accountId, int bookId, int year, int month, int day, int todoId, boolean check, String token){ + public int setCheck(String accountId, int bookId, int year, int month, int day, int todoId, boolean check, String token){ + if(!todos.containsKey(accountId)){ + return -1; + } + if(!todos.get(accountId).containsKey(bookId)){ + return -1; + } + if(!todos.get(accountId).get(bookId).containsKey(year)){ + return -1; + } + if(!todos.get(accountId).get(bookId).get(year).containsKey(month)){ + return -1; + } + if(!todos.get(accountId).get(bookId).get(year).get(month).containsKey(day)){ + return -1; + } + if(!todos.get(accountId).get(bookId).get(year).get(month).get(day).containsKey(todoId)){ + return -1; + } todos.get(accountId).get(bookId).get(year).get(month).get(day).get(todoId).setCheck(check); + return 1; } /** diff --git a/src/main/java/org/ntlab/citrusserver/resources/AccountsRest.java b/src/main/java/org/ntlab/citrusserver/resources/AccountsRest.java index b462fb7..3134994 100644 --- a/src/main/java/org/ntlab/citrusserver/resources/AccountsRest.java +++ b/src/main/java/org/ntlab/citrusserver/resources/AccountsRest.java @@ -28,6 +28,7 @@ } + // account_idとpasswordを設定し新しいアカウントを作成する(POST) @POST @Consumes(MediaType.APPLICATION_FORM_URLENCODED)//bodyに入力する値がある時 @@ -37,93 +38,110 @@ return token; } - - -////////// - @Path("/{account_id}") // 指定されたアカウントの情報を返す(GET) + @Path("/{account_id}") @GET @Produces(MediaType.APPLICATION_JSON) - public Account getAccountInfo(@PathParam("account_id") String accountId){ //account_idを渡してManegerから値が返ってくる - Account ac = accountManager.getAccount(accountId); - return ac; + public Account getAccountInfo(@PathParam("account_id") String accountId){ + Account ac = accountManager.getAccount(accountId); + return ac; } + // アカウント情報を全削除する(DELETE) - // @DELETE + @Path("/{account_id}") + @DELETE + public void deleteAccount(@PathParam("account_id") String accountId, + @QueryParam("token") String token, + @QueryParam("password")String password) { + if(accountManager.checkToken(accountId, token) == true) { + accountManager.deleteAccount(accountId, token, password); + } + } -// @Path("/{account_id}/password") -// //指定されたIDのパスワードを変更する (PUT) -// @PUT -// public void changePassword(@PathParam("account_id") String accountId, -// @QueryParam("token") String token, -// @PathParam("old_password")String oldPassword, -// @PathParam("new_password")String newPassword){ //account_idを渡してManegerから値が返ってくる -// return accountManager.changePassword(accountId,token,oldPassword,newPassword); -// -// } + //指定されたIDのパスワードを変更する (PUT) + @Path("/{account_id}/password") + @PUT + public void changePassword(@PathParam("account_id") String accountId, + @FormParam("new_password")String newPassword, + @FormParam("old_password")String oldPassword, + @FormParam("token") String token){ + if(accountManager.checkToken(accountId, token) == true) { + accountManager.changePassword(accountId, newPassword, oldPassword, token); + } + } -///////// - @Path("/accounts/{account_id}/introduction") // 指定されたIDの自己紹介を返す(GET) + @Path("/{account_id}/introduction") @GET @Produces(MediaType.APPLICATION_JSON) - public String getIntroduction(@PathParam("account_id") String accountId){ //account_idを渡してintroductionが返ってくる + public String getIntroduction(@PathParam("account_id") String accountId){ String ac = accountManager.AccountIntro(accountId); return ac; } - -// 指定されたIDの自己紹介を変更する (PUT) -// @PUT - + // 指定されたIDの自己紹介を変更する (PUT) + @Path("/{account_id}/introduction") + @PUT + public void changeIntroduction(@PathParam("account_id") String accountId, + @FormParam("introduction")String introduction, + @FormParam("token") String token){ + if(accountManager.checkToken(accountId, token) == true) { + accountManager.changeIntroduction(accountId, introduction, token); + } + } ///////// -// @Path("/accounts/{account_id}/photo") +// @Path("/{account_id}/photo") // //画像を返す // @GET -// public String getAccount(@PathParam("account_id") String accountId){ //account_idを渡してManegerから値が返ってくる +// public String getAccount(@PathParam("account_id") String accountId){ // Account ac = accountManager.getAccount(accountId); // return ac.getPhoto(); // } -// +// @Path("/{account_id}/photo") // @PUT -///////// - @Path("/accounts/{account_id}/favorites") + //指定されたIDのお気に入りの本のリストを返す + @Path("/accounts/{account_id}/favorites") @GET @Produces(MediaType.APPLICATION_JSON) - public ArrayList> favoriteBook(@PathParam("account_id") String accountId, @QueryParam("token")String token){ - return accountManager.Favorites(accountId,token); + public ArrayList> favoriteBook(@PathParam("account_id") String accountId, + @QueryParam("token")String token){ + if(accountManager.checkToken(accountId, token) == true) { + return accountManager.Favorites(accountId, token); + } + return null; } -//////// - @Path("/accounts/{account_id}/favorites/{other_account_id}") //指定されたIDのお気に入りの本のリストを返す(指定した人物) + @Path("/accounts/{account_id}/favorites/{other_account_id}") @GET @Produces(MediaType.APPLICATION_JSON) - public ArrayList FavoriteBook(@PathParam("account_id") String accountId,@PathParam("other_account_id") String otherAccountId,@QueryParam("token")String token){ //account_idを渡してManegerから値が返ってくる - return accountManager.FavoritesBookId(accountId,otherAccountId,token); + public ArrayList FavoriteBook(@PathParam("account_id") String accountId, + @PathParam("other_account_id") String otherAccountId, + @QueryParam("token")String token){ + if(accountManager.checkToken(accountId, token) == true) { + return accountManager.FavoritesBookId(accountId, otherAccountId, token); + } + return null; } ////////// - -// @Path("/accounts/{account_id}/favorites/{other_account_id}/{book_id}") - // お気に入りの本のbook_idを削除する (DELETE) +// お気に入りの本のbook_idを削除する (DELETE) +// @Path("/{account_id}/favorites/{other_account_id}/{book_id}") // @DELETE // いいねした本のアカウントIDとbook_idを追加する(いいねした側に追加) (PUT) -// @PUT + //@Path("/{account_id}/favorites/{other_account_id}/{book_id}") +// @PUT - -//////// // アカウントidとパスワードでログインし、tokenを返す (POST) -// @Path("/accounts/{account_id}/login") -// @POST -// @Consumes(MediaType.APPLICATION_JSON) -// public void login(@PathParam("account_id") String accountId,@FormParam("password") String password) { -// accountManager.put(accountId, password); -// } + @Path("/{account_id}/login") + @POST + public String login(@PathParam("account_id") String accountId,@FormParam("password") String password) { + return accountManager.login(accountId, password); + } } diff --git a/src/main/java/org/ntlab/citrusserver/resources/BooksRest.java b/src/main/java/org/ntlab/citrusserver/resources/BooksRest.java index badab34..99802ce 100644 --- a/src/main/java/org/ntlab/citrusserver/resources/BooksRest.java +++ b/src/main/java/org/ntlab/citrusserver/resources/BooksRest.java @@ -3,6 +3,7 @@ import jakarta.ws.rs.*; import jakarta.ws.rs.core.MediaType; import org.ntlab.citrusserver.entities.Book; +import org.ntlab.citrusserver.repositories.AccountManager; import org.ntlab.citrusserver.repositories.BookManager; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @@ -14,80 +15,102 @@ @Component -public class BooksRest {//BookRestはクラス +public class BooksRest { // BookRestはクラス private final BookManager bookManager; - @Autowired //スプリングブートにいうサイン - public BooksRest(BookManager bm){ //public クラス名()がコンストラクタ + private final AccountManager accountManager; + @Autowired // スプリングブートにいうサイン + public BooksRest(BookManager bm, AccountManager ac){//public クラス名()がコンストラクタ + bookManager = bm; + accountManager = ac; } -/// {account_id}/books private final HashMap> books = new HashMap<>(); - +/// {account_id}/books /// その人の本のタイトルとかを返す @Path("/{account_id}/books") @GET @Produces(MediaType.APPLICATION_JSON) - public HashMap getBooks(@PathParam("account_id") String account_id){ - return bookManager.getBooks(account_id); + public HashMap getBooks(@PathParam("account_id") String account_id, @QueryParam("token") String token){ + if(!accountManager.checkToken(account_id, token)) return null; + return bookManager.getBooks(account_id, token); } @Path("/{account_id}/books") @POST - @Consumes(MediaType.APPLICATION_FORM_URLENCODED)//bodyに入力する値がある時 - public int createBook(@PathParam("account_id") String account_id, @FormParam("title") String title, @FormParam("color") String color, @FormParam("publicity") Boolean publicity) { - return bookManager.createBook(account_id, title, color, publicity); + @Produces(MediaType.APPLICATION_JSON) // intとかstringとかがたくさん返ってくるから、json public voidじゃないときは、返さなあかんから、 @Produces(MediaType.APPLICATION_JSON) これがいる + @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) { + if(!accountManager.checkToken(account_id, token)) return null; + return bookManager.createBook(account_id, title, color, publicity, token); } /// {account_id}/books/{book_id} - @Path("/{account_id}/books/{book_id}") - /// 本の情報を取得 + @Path("/{account_id}/books/{book_id}") @GET @Produces(MediaType.APPLICATION_JSON) - public Book getBook(@PathParam("account_id") String account_id, @PathParam("book_id") Integer book_id){ - return bookManager.getBook(account_id, book_id); + public Book getBook(@PathParam("account_id") String account_id, @PathParam("book_id") Integer book_id, @QueryParam("token") String token){ + if(!accountManager.checkToken(account_id, token)) return null; + return bookManager.getBook(account_id, book_id, token); + } + /// 本の削除 + @Path("/{account_id}/books/{book_id}") + @DELETE + @Consumes(MediaType.APPLICATION_FORM_URLENCODED) + public void deleteTodoById(@PathParam("account_id") String account_id, @PathParam("book_id") Integer book_id, @QueryParam("token") String token){ + if(!accountManager.checkToken(account_id, token)) return; + bookManager.deleteBook(account_id, book_id, token); } -///// /{account_id}/books/{book_id}/favorited -// @Path("/{account_id}/books/{book_id}/favorited") -// -// /// いいねしたアカウントを返す -// @GET -// @Produces(MediaType.APPLICATION_JSON) -// public Book g(@PathParam("account_id") String account_id, @PathParam("book_id") Integer book_id){ -// Book book = bookManager.getBook(account_id, book_id); -// return book; -// } - - /// /{account_id}/books/{book_id}/title - @Path("/{account_id}/books/{book_id}/title") - /// 本のタイトルを返す + @Path("/{account_id}/books/{book_id}/title") @GET @Produces(MediaType.TEXT_PLAIN) - public String getTitle(@PathParam("account_id") String account_id, @PathParam("book_id") Integer book_id){ - return bookManager.getTitle(account_id, book_id); + public String getTitle(@PathParam("account_id") String account_id, @PathParam("book_id") Integer book_id, @QueryParam("token") String token){ + if(!accountManager.checkToken(account_id, token)) return null; + return bookManager.getTitle(account_id, book_id, token); + } + + /// 本のタイトル変更 + @Path("/{account_id}/books/{book_id}/title") + @PUT + @Consumes(MediaType.APPLICATION_FORM_URLENCODED) + public void putTitle(@PathParam("account_id") String account_id, @PathParam("book_id") Integer book_id, @FormParam("title") String title, @FormParam("token") String token){ + if(!accountManager.checkToken(account_id, token)) return; + bookManager.putTitle(account_id, book_id, title, token); } /// /accounts/{account_id}/books/{book_id}/public - @Path("/{account_id}/books/{book_id}/public") - /// 本の公開状態を返す + @Path("/{account_id}/books/{book_id}/public") @GET @Produces(MediaType.TEXT_PLAIN) - public Boolean getPublicity(@PathParam("account_id") String account_id, @PathParam("book_id") Integer book_id){ - return bookManager.getPublicity(account_id, book_id); + public Boolean getPublicity(@PathParam("account_id") String account_id, @PathParam("book_id") Integer book_id, @QueryParam("token") String token){ + if(!accountManager.checkToken(account_id, token)) return null; + return bookManager.getPublicity(account_id, book_id, token); } + /// 公開情報を変更する + @Path("/{account_id}/books/{book_id}/public") + @PUT + @Consumes(MediaType.APPLICATION_FORM_URLENCODED) + public void putPublicity(@PathParam("account_id") String account_id, @PathParam("book_id") Integer book_id, @FormParam("publicity") Boolean publicity, @FormParam("token") String token){ + if(!accountManager.checkToken(account_id, token)) return; + bookManager.putPublicity(account_id, book_id, publicity, token); + } - - - - - +/// /accounts/{account_id}/books/{book_id}/color + /// 公開情報を変更する + @Path("/{account_id}/books/{book_id}/color") + @PUT + @Consumes(MediaType.APPLICATION_FORM_URLENCODED) + public void putColor(@PathParam("account_id") String account_id, @PathParam("book_id") Integer book_id, @FormParam("color") String color, @FormParam("token") String token){ + if(!accountManager.checkToken(account_id, token)) return; + bookManager.putColor(account_id, book_id, color, token); + } } \ No newline at end of file diff --git a/src/main/java/org/ntlab/citrusserver/resources/TodoRest.java b/src/main/java/org/ntlab/citrusserver/resources/TodoRest.java index 27d89be..5dc5ccf 100644 --- a/src/main/java/org/ntlab/citrusserver/resources/TodoRest.java +++ b/src/main/java/org/ntlab/citrusserver/resources/TodoRest.java @@ -4,6 +4,7 @@ import jakarta.ws.rs.core.MediaType; import org.apache.coyote.http11.upgrade.UpgradeServletOutputStream; import org.ntlab.citrusserver.entities.Todo; +import org.ntlab.citrusserver.repositories.AccountManager; import org.ntlab.citrusserver.repositories.TodoManager; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @@ -15,12 +16,14 @@ public class TodoRest { private final TodoManager todoManager; + private final AccountManager accountManager; @Autowired - public TodoRest(TodoManager tm, TodoManager todoManager) { + public TodoRest(TodoManager todoManager, AccountManager accountManager) { this.todoManager = todoManager; - todoManager = tm; + this.accountManager = accountManager; } + //test用 @Path("/TodoTest") @GET @@ -35,16 +38,21 @@ @GET @Produces(MediaType.APPLICATION_JSON) public HashMap>>> getAllTodos(@PathParam("account_id") String account_id, @PathParam("book_id") Integer book_id, @QueryParam("token") String token) { - return todoManager.getAllTodos(account_id, book_id, token); - + if(accountManager.checkToken(account_id,token)==true){ + return todoManager.getAllTodos(account_id, book_id, token); + } + return null; } //指定された本の指定された年と月のtodoをすべて返す - @Path("/accounts/{account_id}/books/{book_id}/todos/{year}/{month}") + @Path("/{account_id}/books/{book_id}/todos/{year}/{month}") @GET @Produces(MediaType.APPLICATION_JSON) public HashMap> getTodosByMonth(@PathParam("account_id") String account_id, @PathParam("book_id") Integer book_id, @PathParam("year") Integer year, @PathParam("month") Integer month, @QueryParam("token") String token) { - return todoManager.getTodosByMonth(account_id, book_id, year, month,token); + if(accountManager.checkToken(account_id,token)==true) { + return todoManager.getTodosByMonth(account_id, book_id, year, month, token); + } + return null; } //指定された本の指定された年と月と日のtodoをすべて返す @@ -52,8 +60,10 @@ @GET @Produces(MediaType.APPLICATION_JSON) public HashMap getTodosByDay(@PathParam("account_id") String account_id, @PathParam("book_id") Integer book_id, @PathParam("year") Integer year, @PathParam("month") Integer month, @PathParam("day") Integer day, @QueryParam("token") String token) { - return todoManager.getTodosByDay(account_id, book_id, year, month, day, token); - + if(accountManager.checkToken(account_id,token)==true) { + return todoManager.getTodosByDay(account_id, book_id, year, month, day, token); + } + return null; } //本のtodoを年月日とtodo_idを指定してtodoを一つ返す @@ -61,39 +71,49 @@ @GET @Produces(MediaType.APPLICATION_JSON) public Todo getTodoById(@PathParam("account_id") String account_id, @PathParam("book_id") Integer book_id, @PathParam("year") Integer year, @PathParam("month") Integer month, @PathParam("day") Integer day, @PathParam("todo_id") Integer todo_id, @QueryParam("token") String token) { - Todo todo = todoManager.getTodoById(account_id, book_id, year, month, day, todo_id, token); - return todo; + if(accountManager.checkToken(account_id,token)==true) { + Todo todo = todoManager.getTodoById(account_id, book_id, year, month, day, todo_id, token); + return todo; + } + return null; } + //指定した本と年月日にtodoを新しく追加する @POST - @Path("/accounts/{account_id}/books/{book_id}/todos/{year}/{month}/{day}") + @Path("/{account_id}/books/{book_id}/todos/{year}/{month}/{day}") @Consumes(MediaType.APPLICATION_FORM_URLENCODED) - public void createTodo(@PathParam("account_id") String account_id, @PathParam("book_id") Integer book_id, @PathParam("year") Integer year, @PathParam("month") Integer month, @PathParam("day") Integer day, @FormParam("title") String title,@QueryParam("token") String token) { - todoManager.createTodo(account_id, book_id, year, month, day, title,token); - + @Produces(MediaType.APPLICATION_JSON) + public Todo createTodo(@PathParam("account_id") String account_id, @PathParam("book_id") Integer book_id, @PathParam("year") Integer year, @PathParam("month") Integer month, @PathParam("day") Integer day, @FormParam("title") String title,@FormParam("token") String token) { + if(accountManager.checkToken(account_id,token)==true) { + return todoManager.createTodo(account_id, book_id, year, month, day, title, token); + } + return null; } //todoを選んで達成状態を変更する //フォームパラメータでチェック状況 @PUT - @Path("/accounts/{account_id}/books/{book_id}/todos/{year}/{month}/{day}/{todo_id}/check") + @Path("/{account_id}/books/{book_id}/todos/{year}/{month}/{day}/{todo_id}/check") @Consumes(MediaType.APPLICATION_FORM_URLENCODED) - public void setCheck(@PathParam("account_id") String account_id, @PathParam("book_id") Integer book_id, @PathParam("year") Integer year, @PathParam("month") Integer month, @PathParam("day") Integer day,@PathParam("todo_id") Integer todo_id, @FormParam("check") boolean check, @QueryParam("token") String token){ - todoManager.setCheck(account_id, book_id, year, month, day, todo_id,check,token); + public void setCheck(@PathParam("account_id") String account_id, @PathParam("book_id") Integer book_id, @PathParam("year") Integer year, @PathParam("month") Integer month, @PathParam("day") Integer day,@PathParam("todo_id") Integer todo_id, @FormParam("check") boolean check, @FormParam("token") String token){ + if(accountManager.checkToken(account_id,token)==true) { + todoManager.setCheck(account_id, book_id, year, month, day, todo_id, check, token); + } } - //delete追加必要 + //本のtodoを年月日とtodo_idを指定してそのtodoを削除する @DELETE - @Path("/accounts/{account_id}/books/{book_id}/todos/{year}/{month}/{day}/{todo_id}") + @Path("/{account_id}/books/{book_id}/todos/{year}/{month}/{day}/{todo_id}") @Consumes(MediaType.APPLICATION_FORM_URLENCODED) public void deleteTodoById(@PathParam("account_id") String account_id, @PathParam("book_id") Integer book_id, @PathParam("year") Integer year, @PathParam("month") Integer month, @PathParam("day") Integer day, @PathParam("todo_id") Integer todo_id, @QueryParam("token") String token){ - todoManager.deleteTodoById(account_id, book_id, year, month, day, todo_id,token); + if(accountManager.checkToken(account_id,token)==true) { + todoManager.deleteTodoById(account_id, book_id, year, month, day, todo_id, token); + } } - } \ No newline at end of file