diff --git a/src/main/java/org/ntlab/citrusserver/entities/Account.java b/src/main/java/org/ntlab/citrusserver/entities/Account.java index 6aac4b0..a5aa746 100644 --- a/src/main/java/org/ntlab/citrusserver/entities/Account.java +++ b/src/main/java/org/ntlab/citrusserver/entities/Account.java @@ -6,6 +6,11 @@ String accountId; String password; + public Account(String aid, String pass) { + accountId = aid; + password = pass; + } + public void setIntroduction(String i) {introduction = i;} public String getIntroduction() {return introduction;} public int getNewBookId() { diff --git a/src/main/java/org/ntlab/citrusserver/entities/Book.java b/src/main/java/org/ntlab/citrusserver/entities/Book.java index f9aec9b..dc010ff 100644 --- a/src/main/java/org/ntlab/citrusserver/entities/Book.java +++ b/src/main/java/org/ntlab/citrusserver/entities/Book.java @@ -2,18 +2,21 @@ public class Book { + private int bookId; private String title; private boolean publicity; private String color; - private int bookId; - private int todoId = 0; - public Book(String title, boolean publicity, String color) { + + public Book(Integer bookid, String title, boolean publicity, String color) { + this.bookId = bookid; + this.title = title; this.publicity = publicity; this.color = color; } + public void setTitle(String t) {title = t;} public String getTitle() {return title;} public void setPublicity(boolean p) {publicity = p;} @@ -22,8 +25,4 @@ public String getColor() {return color;} public void setBookId(int id) {bookId = id;} public int getBookId() {return bookId;} - public int getTodoId() { - todoId += 1; - return todoId; - } } diff --git a/src/main/java/org/ntlab/citrusserver/repositories/AccountManager.java b/src/main/java/org/ntlab/citrusserver/repositories/AccountManager.java index 04c0eba..4278aac 100644 --- a/src/main/java/org/ntlab/citrusserver/repositories/AccountManager.java +++ b/src/main/java/org/ntlab/citrusserver/repositories/AccountManager.java @@ -13,7 +13,7 @@ @Repository public class AccountManager { - private HashMap accounts = new HashMap(); + private HashMap accounts = new HashMap(); //keyにaccountId,valueにaccount private HashMap accountToken = new HashMap<>(); //keyがaccountId,valueがtoken @@ -26,9 +26,11 @@ public String newAccount(String accountId, String password) { UUID str = UUID.randomUUID(); String token = str.toString(); - //accounts.setId(accountId); - //accounts.setPassword(password); - accountToken.put(accountId, token); //accountIDとtokenをHashMapに入れる + Account account = new Account(accountId, password); + if(!accounts.containsKey(accountId)) { + accounts.put(accountId, account); + accountToken.put(accountId, token); //accountIDとtokenをHashMapに入れる + } return token; } @@ -43,18 +45,18 @@ } // 指定されたIDの自己紹介を返す(GET) - public void AccountIntro() { - + public String AccountIntro(String accountId) { + return accounts.get(accountId).getIntroduction(); } // 指定されたIDのお気に入りの本のリストを返す(GET) - public void favoriteBook() { - + public ArrayList> favoriteBook(String accountId, String token) { + return null; } // 指定されたIDのお気に入りの本のリストを返す(指定した人物) (GET) - public void FavoriteparBook() { - + public ArrayList FavoriteBook(String accountId, String otherAccountId, String token) { + return null; } // 指定されたIDのアカウントを変更する (PUT) diff --git a/src/main/java/org/ntlab/citrusserver/repositories/BookManager.java b/src/main/java/org/ntlab/citrusserver/repositories/BookManager.java index cadd0f9..af3fb10 100644 --- a/src/main/java/org/ntlab/citrusserver/repositories/BookManager.java +++ b/src/main/java/org/ntlab/citrusserver/repositories/BookManager.java @@ -2,15 +2,17 @@ import org.ntlab.citrusserver.entities.Account; import org.ntlab.citrusserver.entities.Book; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Repository; import java.util.HashMap; @Repository public class BookManager { - private HashMap> booksMap = new HashMap<>(); + private final HashMap> booksMap = new HashMap<>(); private final AccountManager accountManager; //仮 + @Autowired public BookManager(AccountManager accountManager) { this.accountManager = accountManager; } @@ -25,8 +27,8 @@ public int createBook(String accountId, String title, String color, Boolean publicity) { Account account = accountManager.getAccount(accountId); //アカウントの取得 - Book book = new Book(title, publicity, color); //本の初期化 int newBookId = account.getNewBookId(); //新たに生成されたIdを取得(作成数もここで加算している) + Book book = new Book(newBookId, title, publicity, color); //本の初期化 booksMap.get(accountId).put(newBookId, book); //ブックに追加 return newBookId; } diff --git a/src/main/java/org/ntlab/citrusserver/repositories/TodoManager.java b/src/main/java/org/ntlab/citrusserver/repositories/TodoManager.java index a60d082..38d8fa5 100644 --- a/src/main/java/org/ntlab/citrusserver/repositories/TodoManager.java +++ b/src/main/java/org/ntlab/citrusserver/repositories/TodoManager.java @@ -25,32 +25,26 @@ */ private final HashMap nextTodoId = new HashMap<>(); -// private final BookManager bookManager; -// -// TodoManger(BookManager bookManager){ -// this.bookManager = bookManager; -// } /** - * * アカウントと本を指定してそれに所属するtodoをすべて返す * * @param accountId アカウントのid * @param bookId 本のid - * @return そのアカウントの本に所属するtodoをすべて返します + * @return そのアカウントの本に所属するtodoをすべて返します
+ * { + * year:{ + * month:{ + * day:{ + * todo_id: Todo + * } + * } + * } + * } */ - public HashMap getAllTodos(String accountId, int bookId){ - HashMap result = new HashMap<>(); - for(var yearValues : todos.get(accountId).get(bookId).values()){ - for(var monthValues : yearValues.values()){ - for(var dayValues : monthValues.values()){ - for(int todoId : dayValues.keySet()){ - result.put(todoId, dayValues.get(todoId)); - } - } - } - } - return result; + public HashMap>>> + getAllTodos(String accountId, int bookId, String token){ + return todos.get(accountId).get(bookId); } /** @@ -62,15 +56,8 @@ * @param month 月 * @return そのアカウントの本に所属するtodoのうち、指定した年月のtodoを返します */ - public HashMap getTodosByMonth(String accountId, int bookId, int year, int month){ - HashMap result = new HashMap<>(); - var yearMonthMap = todos.get(accountId).get(bookId).get(year).get(month); - for(var dayValues : yearMonthMap.values()){ - for(int todoId : dayValues.keySet()){ - result.put(todoId, dayValues.get(todoId)); - } - } - return result; + public HashMap> getTodosByMonth(String accountId, int bookId, int year, int month, String token){ + return todos.get(accountId).get(bookId).get(year).get(month); } /** @@ -81,11 +68,11 @@ * @param yearMonth 年月を-で区切った文字列(yyyy-mm) * @return そのアカウントの本に所属するtodoのうち、指定した年月のtodoを返します */ - public HashMap getTodosByMonth(String accountId, int bookId, String yearMonth){ + public HashMap> getTodosByMonth(String accountId, int bookId, String yearMonth, String token){ String[] yearMonths = yearMonth.split("-"); int year = Integer.parseInt(yearMonths[0]); int month = Integer.parseInt(yearMonths[1]); - return getTodosByMonth(accountId, bookId, year, month); + return getTodosByMonth(accountId, bookId, year, month, token); } /** @@ -98,7 +85,7 @@ * @param day 日 * @return そのアカウントの本に所属するtodoのうち、指定した年月日のtodoを返します */ - public HashMap getTodosByDay(String accountId, int bookId, int year, int month, int day){ + public HashMap getTodosByDay(String accountId, int bookId, int year, int month, int day, String token){ return todos.get(accountId).get(bookId).get(year).get(month).get(day); } @@ -110,12 +97,12 @@ * @param yearMonthDay 年月日を-で区切った文字列(yyyy-mm-dd) * @return そのアカウントの本に所属するtodoのうち、指定した年月日のtodoを返します */ - public HashMap getTodosByDay(String accountId, int bookId, String yearMonthDay){ + public HashMap getTodosByDay(String accountId, int bookId, String yearMonthDay, String token){ String[] yearMonthDays = yearMonthDay.split("-"); int year = Integer.parseInt(yearMonthDays[0]); int month = Integer.parseInt(yearMonthDays[1]); int day = Integer.parseInt(yearMonthDays[2]); - return getTodosByDay(accountId, bookId, year, month, day); + return getTodosByDay(accountId, bookId, year, month, day, token); } @@ -130,11 +117,12 @@ * @param todoId todoのid * @return idを指定してtodoを返す */ - public Todo getTodoById(String accountId, int bookId, int year, int month, int day, int todoId){ + public Todo getTodoById(String accountId, int bookId, int year, int month, int day, int todoId, String token){ return todos.get(accountId).get(bookId).get(year).get(month).get(day).get(todoId); } /** + *todo_idを指定してtodoを返す * * @param accountId アカウントのid * @param bookId 本のid @@ -142,12 +130,12 @@ * @param todoId todoのid * @return 指定したtodo */ - public Todo getTodoById(String accountId, int bookId, String yearMonthDay, int todoId){ + public Todo getTodoById(String accountId, int bookId, String yearMonthDay, int todoId, String token){ String[] yearMonthDays = yearMonthDay.split("-"); int year = Integer.parseInt(yearMonthDays[0]); int month = Integer.parseInt(yearMonthDays[1]); int day = Integer.parseInt(yearMonthDays[2]); - return getTodoById(accountId, bookId, year, month, day, todoId); + return getTodoById(accountId, bookId, year, month, day, todoId, token); } /** @@ -159,8 +147,9 @@ * @param month 月 * @param day 日 * @param title 追加するべきtodoのタイトル + * @return 新しいtodoのid */ - public int addTodo(String accountId, int bookId, int year, int month, int day, String title){ + public int createTodo(String accountId, int bookId, int year, int month, int day, String title, String token){ if(!todos.containsKey(accountId)){ todos.put(accountId, new HashMap<>()); } @@ -181,26 +170,28 @@ nextTodoId.put(accountBook, 0); } - int todoId = nextTodoId.get(accountBook); - Todo newTodo = new Todo(title, false, year, month, day, todoId); - todos.get(accountId).get(bookId).get(year).get(month).get(day).put(todoId, newTodo); - nextTodoId.put(accountBook, todoId + 1); - return todoId; + int newTodoId = nextTodoId.get(accountBook); + 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; } /** + *todoを追加する * * @param accountId アカウントのid * @param bookId 本のid * @param yearMonthDay 年月日を-で区切った文字列(yyyy-mm-dd) * @param title 追加したいtodoのタイトル + * @return 新しいtodoのid */ - public int addTodo(String accountId, int bookId, String yearMonthDay, String title){ + public int 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]); int day = Integer.parseInt(yearMonthDays[2]); - return addTodo(accountId, bookId, year, month, day, title); + return createTodo(accountId, bookId, year, month, day, title, token); } /** @@ -210,23 +201,24 @@ * @param bookId 本のid * @param todoId 削除したいtodoのid */ - public void deleteTodoById(String accountId, int bookId, int year, int month, int day, int todoId){ + public void deleteTodoById(String accountId, int bookId, int year, int month, int day, int todoId, String token){ todos.get(accountId).get(bookId).get(year).get(month).get(day).remove(todoId); } /** + *idを指定してtodoを削除 * * @param accountId アカウントのid * @param bookId 本のid * @param yearMonthDay 年月日を-で区切った文字列(yyyy-mm-dd) * @param todoId 削除したいtodoのid */ - public void deleteTodoById(String accountId, int bookId, String yearMonthDay, int todoId){ + public void deleteTodoById(String accountId, int bookId, String yearMonthDay, int todoId, String token){ String[] yearMonthDays = yearMonthDay.split("-"); int year = Integer.parseInt(yearMonthDays[0]); int month = Integer.parseInt(yearMonthDays[1]); int day = Integer.parseInt(yearMonthDays[2]); - deleteTodoById(accountId, bookId, year, month, day, todoId); + deleteTodoById(accountId, bookId, year, month, day, todoId, token); } /** @@ -237,11 +229,12 @@ * @param todoId 変更したいtodoのid * @param check 変更後の達成状態 */ - public void setCheck(String accountId, int bookId, int year, int month, int day, int todoId, boolean check){ + public void setCheck(String accountId, int bookId, int year, int month, int day, int todoId, boolean check, String token){ todos.get(accountId).get(bookId).get(year).get(month).get(day).get(todoId).setCheck(check); } /** + *todo_idを指定してtodoの達成状態を変更 * * @param accountId アカウントのid * @param bookId 本のid @@ -249,11 +242,11 @@ * @param todoId 変更したいtodoのid * @param check 変更後の達成状態 */ - public void setCheck(String accountId, int bookId, String yearMonthDay, int todoId, boolean check){ + public void setCheck(String accountId, int bookId, String yearMonthDay, int todoId, boolean check, String token){ String[] yearMonthDays = yearMonthDay.split("-"); int year = Integer.parseInt(yearMonthDays[0]); int month = Integer.parseInt(yearMonthDays[1]); int day = Integer.parseInt(yearMonthDays[2]); - setCheck(accountId, bookId, year, month, day, todoId, check); + setCheck(accountId, bookId, year, month, day, todoId, check, token); } } diff --git a/src/main/java/org/ntlab/citrusserver/resources/AccountsRest.java b/src/main/java/org/ntlab/citrusserver/resources/AccountsRest.java index 34687a5..7b1759c 100644 --- a/src/main/java/org/ntlab/citrusserver/resources/AccountsRest.java +++ b/src/main/java/org/ntlab/citrusserver/resources/AccountsRest.java @@ -1,17 +1,112 @@ package org.ntlab.citrusserver.resources; import jakarta.ws.rs.*; import jakarta.ws.rs.core.MediaType; +import org.ntlab.citrusserver.entities.Account; +import org.ntlab.citrusserver.repositories.AccountManager; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Set; @Path("/accounts") -@Component +@Component //accountRestのインスタンスを一個作る public class AccountsRest { - @GET - @Produces(MediaType.TEXT_PLAIN) - public String getMessage() { - return null; + private final AccountManager accountManager; //finalは書き換えられない + @Autowired//springbootの決まり + public AccountsRest(AccountManager am) { + accountManager = am; + } +///accountの一覧を返す + @GET + @Produces(MediaType.APPLICATION_JSON) + public Set getAccount(){ + return accountManager.getAccountsID(); + } + + +//accountの新規作成 + @POST + @Consumes(MediaType.APPLICATION_FORM_URLENCODED)//bodyに入力する値がある時 + public String signup(@FormParam("account_id") String account_id, @FormParam("password") String password) { + String token; + token = accountManager.newAccount(account_id, password); + return token; + } + +////////// + @Path("/{account_id}") + +//account_idの情報を返す”introduction”と[本] + @GET + @Produces(MediaType.APPLICATION_JSON) + public Account getAccountInfo(@PathParam("account_id") String accountId){ //account_idを渡してManegerから値が返ってくる + Account ac = accountManager.getAccount(accountId); + return ac; + } + + @DELETE + + +/////// + @Path("/{account_id}/password") +//// +// @PUT +// +// +///////// + @Path("/accounts/{account_id}/introduction") +//自己紹介を返す + @GET + @Produces(MediaType.APPLICATION_JSON) + public String getAccount(@PathParam("account_id") String account_id){ //account_idを渡してintroductionが返ってくる + String ac = accountManager.AccountIntro(account_id); + return ac; + } + + @PUT + +///////// +// @Path("/accounts/{account_id}/photo") +////画像を返す +// @GET +// public String getAccount(@PathParam("account_id") String account_id){ //account_idを渡してManegerから値が返ってくる +// Account ac = accountManager.getAccount(account_id); +// return ac.getPhoto(); +// } +// +// @PUT + +///////// + @Path("/accounts/{account_id}/favorites") +//お気に入りの本のリストを返す + @GET + @Produces(MediaType.APPLICATION_JSON) + public ArrayList> favoriteBook(@PathParam("account_id") String account_id, @QueryParam("token")String token){ + return accountManager.favoriteBook(account_id,token); + } + +//////// + @Path("/accounts/{account_id}/favorites/{other_account_id}") +//お気に入りの本のリストを返す + @GET + @Produces(MediaType.APPLICATION_JSON) + public ArrayList FavoriteBook(@PathParam("account_id") String account_id,@PathParam("other_account_id") String other_account_id,@QueryParam("token")String token){ //account_idを渡してManegerから値が返ってくる + return accountManager.FavoriteBook(account_id,other_account_id,token); + } + +//////// + @Path("/accounts/{account_id}/favorites/{other_account_id}/{book_id}") + @DELETE + @PUT +//////// + @Path("/accounts/{account_id}/login") + @POST + @Consumes(MediaType.APPLICATION_JSON) + public void login(@PathParam("account_id") String account_id),@FormParam("password") String password) { + accounts.put(account_id, password); } } diff --git a/src/main/java/org/ntlab/citrusserver/resources/TodoRest.java b/src/main/java/org/ntlab/citrusserver/resources/TodoRest.java index dfd750d..5990081 100644 --- a/src/main/java/org/ntlab/citrusserver/resources/TodoRest.java +++ b/src/main/java/org/ntlab/citrusserver/resources/TodoRest.java @@ -1,18 +1,96 @@ package org.ntlab.citrusserver.resources; -import jakarta.ws.rs.GET; -import jakarta.ws.rs.PUT; -import jakarta.ws.rs.Path; -import jakarta.ws.rs.Produces; +import jakarta.ws.rs.*; import jakarta.ws.rs.core.MediaType; +import org.apache.coyote.http11.upgrade.UpgradeServletOutputStream; +import org.ntlab.citrusserver.entities.Todo; +import org.ntlab.citrusserver.repositories.TodoManager; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; +import java.util.HashMap; + @Path("/accounts") @Component public class TodoRest { + + private final TodoManager todoManager; + + @Autowired + public TodoRest(TodoManager tm, TodoManager todoManager) { + this.todoManager = todoManager; + todoManager = tm; + } + //test用 + @Path("/TodoTest") @GET @Produces(MediaType.TEXT_PLAIN) - public String getMessage(){ - return null; + public String testHello() { + return "hello"; } + + //指定された本のtodoをすべて返す + //変更↓ + @Path("/{account_id}/books/{book_id}/todos") + @GET + @Produces(MediaType.APPLICATION_JSON) + public HashMap>>> getName(@PathParam("account_id") String account_id, @PathParam("book_id") Integer book_id, @QueryParam("token") String token) { + return todoManager.getAllTodos(account_id, book_id, token); + //account部分のリスト名要変更 + //ドット部分以降打合せ(相手の関数名に合わせる) + } + + //指定された本の指定された年と月のtodoをすべて返す + //yearとmonthはintなのか!!! + @Path("/accounts/{account_id}/books/{book_id}/todos/{year}/{month}") + @GET + @Produces(MediaType.APPLICATION_JSON) + public HashMap getTodoMonth(@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); + } + + //指定された本の指定された年と月と日のtodoをすべて返す + @Path("/{account_id}/books/{book_id}/todos/{year}/{month}/{day}") + @GET + @Produces(MediaType.APPLICATION_JSON) + public HashMap getTodo(@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); + + } + + //本のtodoを年月日とtodo_idを指定してtodoを一つ返す + @Path("/{account_id}/books/{book_id}/todos/{year}/{month}/{day}/{todo_id}") + @GET + @Produces(MediaType.APPLICATION_JSON) + public Todo getTodo(@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; + } + + //指定した本と年月日にtodoを新しく追加する + @POST + @Path("/accounts/{account_id}/books/{book_id}/todos/{year}/{month}/{day}") + @Consumes(MediaType.APPLICATION_FORM_URLENCODED) + public void putTodo(@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); + + } + + //todoを選んで達成状態を変更する + //フォームパラメータでチェック状況 + @PUT + @Path("/accounts/{account_id}/books/{book_id}/todos/{year}/{month}/{day}/{todo_id}/check") + @Consumes(MediaType.APPLICATION_FORM_URLENCODED) + public void putTodo(@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); + } + + //delete追加必要 + + + + + + + } \ No newline at end of file