diff --git a/src/main/java/org/ntlab/citrusserver/repositories/TodoManager.java b/src/main/java/org/ntlab/citrusserver/repositories/TodoManager.java index 6115fd1..2bb0edc 100644 --- a/src/main/java/org/ntlab/citrusserver/repositories/TodoManager.java +++ b/src/main/java/org/ntlab/citrusserver/repositories/TodoManager.java @@ -11,7 +11,13 @@ /** * todoをすべて管理します */ - private final HashMap>> todos + private final HashMap>>>>> todos = new HashMap<>(); /** @@ -33,8 +39,18 @@ * @param bookId 本のid * @return そのアカウントの本に所属するtodoをすべて返します */ - public HashMap getAllTodos(String accountId, Integer bookId){ - return todos.get(accountId).get(bookId); + 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; } /** @@ -46,17 +62,15 @@ * @param month 月 * @return そのアカウントの本に所属するtodoのうち、指定した年月のtodoを返します */ - public HashMap getTodosByMonth(String accountId, Integer bookId, int year, int month){ - HashMap todos = getAllTodos(accountId, bookId); - HashMap resultTodos = new HashMap<>(); - for(Todo todo : todos.values()){ - if(todo.getYear() == year){ - if (todo.getMonth() == month){ - resultTodos.put(todo.getTodoId(), 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 resultTodos; + return result; } /** @@ -64,10 +78,10 @@ * * @param accountId アカウントのid * @param bookId 本のid - * @param yearMonth 年月を-で区切った文字列 + * @param yearMonth 年月を-で区切った文字列(yyyy-mm) * @return そのアカウントの本に所属するtodoのうち、指定した年月のtodoを返します */ - public HashMap getTodosByMonth(String accountId, Integer bookId, String yearMonth){ + public HashMap getTodosByMonth(String accountId, int bookId, String yearMonth){ String[] yearMonths = yearMonth.split("-"); int year = Integer.parseInt(yearMonths[0]); int month = Integer.parseInt(yearMonths[1]); @@ -84,15 +98,8 @@ * @param day 日 * @return そのアカウントの本に所属するtodoのうち、指定した年月日のtodoを返します */ - public HashMap getTodosByDay(String accountId, Integer bookId, int year, int month, int day){ - HashMap todos = getTodosByMonth(accountId, bookId, year, month); - HashMap resultTodos = new HashMap<>(); - for(Todo todo : todos.values()){ - if(todo.getDay() == day){ - resultTodos.put(todo.getTodoId(), todo); - } - } - return resultTodos; + public HashMap getTodosByDay(String accountId, int bookId, int year, int month, int day){ + return todos.get(accountId).get(bookId).get(year).get(month).get(day); } /** @@ -100,10 +107,10 @@ * * @param accountId アカウントのid * @param bookId 本のid - * @param yearMonthDay 年月日を-で区切った文字列 + * @param yearMonthDay 年月日を-で区切った文字列(yyyy-mm-dd) * @return そのアカウントの本に所属するtodoのうち、指定した年月日のtodoを返します */ - public HashMap getTodosByDay(String accountId, Integer bookId, String yearMonthDay){ + public HashMap getTodosByDay(String accountId, int bookId, String yearMonthDay){ String[] yearMonthDays = yearMonthDay.split("-"); int year = Integer.parseInt(yearMonthDays[0]); int month = Integer.parseInt(yearMonthDays[1]); @@ -123,8 +130,24 @@ * @param todoId todoのid * @return idを指定してtodoを返す */ - public Todo getTodoById(String accountId, Integer bookId, int year, int month, int day, int todoId){ - return todos.get(accountId).get(bookId).get(todoId); + public Todo getTodoById(String accountId, int bookId, int year, int month, int day, int todoId){ + return todos.get(accountId).get(bookId).get(year).get(month).get(day).get(todoId); + } + + /** + * + * @param accountId アカウントのid + * @param bookId 本のid + * @param yearMonthDay 年月日を-で区切った文字列(yyyy-mm-dd) + * @param todoId todoのid + * @return 指定したtodo + */ + public Todo getTodoById(String accountId, int bookId, String yearMonthDay, int todoId){ + 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); } /** @@ -132,25 +155,51 @@ * * @param accountId アカウントのid * @param bookId 本のid + * @param year 年 + * @param month 月 + * @param day 日 * @param todo 追加するべきtodo */ - public void addDayTodo(String accountId, Integer bookId, Todo todo){ + public int addTodo(String accountId, int bookId, int year, int month, int day, Todo todo){ if(!todos.containsKey(accountId)){ todos.put(accountId, new HashMap<>()); - } if(!todos.get(accountId).containsKey(bookId)){ todos.get(accountId).put(bookId, new HashMap<>()); } - String accountBook = accountId + bookId; + if(!todos.get(accountId).get(bookId).containsKey(year)){ + todos.get(accountId).get(bookId).put(year, new HashMap<>()); + } + if(!todos.get(accountId).get(bookId).get(year).containsKey(month)){ + todos.get(accountId).get(bookId).get(year).put(month, new HashMap<>()); + } + 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); } int tmp = nextTodoId.get(accountBook); - todos.get(accountId).get(bookId).put(tmp, todo); + todos.get(accountId).get(bookId).get(year).get(month).get(day).put(tmp, todo); nextTodoId.put(accountBook, tmp + 1); + return tmp; + } + /** + * + * @param accountId アカウントのid + * @param bookId 本のid + * @param yearMonthDay 年月日を-で区切った文字列(yyyy-mm-dd) + * @param todo 追加したいtodo + */ + public int addTodo(String accountId, int bookId, String yearMonthDay, Todo todo){ + 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, todo); } /** @@ -158,10 +207,25 @@ * * @param accountId アカウントのid * @param bookId 本のid - * @param todoId 削除すべきtodoのid + * @param todoId 削除したいtodoのid */ - public void deleteTodoById(String accountId, Integer bookId, Integer todoId){ - todos.get(accountId).get(bookId).remove(todoId); + public void deleteTodoById(String accountId, int bookId, int year, int month, int day, int todoId){ + todos.get(accountId).get(bookId).get(year).get(month).get(day).remove(todoId); + } + + /** + * + * @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){ + 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); } /** @@ -172,7 +236,23 @@ * @param todoId 変更したいtodoのid * @param check 変更後の達成状態 */ - public void setCheck(String accountId, Integer bookId, Integer todoId, boolean check){ - todos.get(accountId).get(bookId).get(todoId).setCheck(check); + public void setCheck(String accountId, int bookId, int year, int month, int day, int todoId, boolean check){ + todos.get(accountId).get(bookId).get(year).get(month).get(day).get(todoId).setCheck(check); + } + + /** + * + * @param accountId アカウントのid + * @param bookId 本のid + * @param yearMonthDay 年月日を-で区切った文字列(yyyy-mm-dd) + * @param todoId 変更したいtodoのid + * @param check 変更後の達成状態 + */ + public void setCheck(String accountId, int bookId, String yearMonthDay, int todoId, boolean check){ + 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); } }