diff --git a/src/main/java/org/ntlab/citrusserver/repositories/TodoManager.java b/src/main/java/org/ntlab/citrusserver/repositories/TodoManager.java index 38d8fa5..b3a1fa5 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,21 @@ HashMap>>>>> todos - = new HashMap<>(); + Todo>>>>>> todos = new HashMap<>(); /** * アカウントの本の次に与えるべきtodoのidを管理します */ private final HashMap nextTodoId = new HashMap<>(); + private final AccountManager accountManager; + + private final BookManager bookManager; + + public TodoManager(AccountManager accountManager, BookManager bookManager) { + this.accountManager = accountManager; + this.bookManager = bookManager; + } /** * アカウントと本を指定してそれに所属するtodoをすべて返す @@ -44,6 +56,9 @@ */ public HashMap>>> getAllTodos(String accountId, int bookId, String token){ + if(!accountManager.checkToken(accountId, token)){ + return null; + } return todos.get(accountId).get(bookId); } @@ -57,6 +72,16 @@ * @return そのアカウントの本に所属するtodoのうち、指定した年月のtodoを返します */ public HashMap> getTodosByMonth(String accountId, int bookId, int year, int month, String token){ + if(!accountManager.checkToken(accountId, token)){ + return null; + } + + if(!todos.get(accountId).get(bookId).containsKey(year)){ + return new HashMap>(); + } + if(!todos.get(accountId).get(bookId).get(year).containsKey(month)){ + return new HashMap>(); + } return todos.get(accountId).get(bookId).get(year).get(month); } @@ -86,6 +111,18 @@ * @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 new HashMap(); + } + if(!todos.get(accountId).get(bookId).get(year).containsKey(month)){ + return new HashMap(); + } + if(!todos.get(accountId).get(bookId).get(year).get(month).containsKey(day)){ + return new HashMap(); + } + if(!accountManager.checkToken(accountId, token)){ + return null; + } return todos.get(accountId).get(bookId).get(year).get(month).get(day); } @@ -118,6 +155,9 @@ * @return idを指定してtodoを返す */ public Todo getTodoById(String accountId, int bookId, int year, int month, int day, int todoId, String token){ + if(!accountManager.checkToken(accountId, token)){ + return null; + } return todos.get(accountId).get(bookId).get(year).get(month).get(day).get(todoId); } @@ -149,7 +189,13 @@ * @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(!accountManager.checkToken(accountId, token)){ + return null; + } + if(!bookManager.getBooks(accountId, token).containsKey(bookId)){ + return null; + } if(!todos.containsKey(accountId)){ todos.put(accountId, new HashMap<>()); } @@ -165,6 +211,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 +221,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 +233,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]); @@ -202,6 +249,27 @@ * @param todoId 削除したいtodoのid */ public void deleteTodoById(String accountId, int bookId, int year, int month, int day, int todoId, String token){ + if(!accountManager.checkToken(accountId, token)){ + return; + } + if(!todos.containsKey(accountId)){ + return; + } + if(!todos.get(accountId).containsKey(bookId)){ + return; + } + if(!todos.get(accountId).get(bookId).containsKey(year)){ + return; + } + if(!todos.get(accountId).get(bookId).get(year).containsKey(month)){ + return; + } + if(!todos.get(accountId).get(bookId).get(year).get(month).containsKey(day)){ + return; + } + if(!todos.get(accountId).get(bookId).get(year).get(month).get(day).containsKey(todoId)){ + return; + } todos.get(accountId).get(bookId).get(year).get(month).get(day).remove(todoId); } @@ -230,6 +298,30 @@ * @param check 変更後の達成状態 */ public void setCheck(String accountId, int bookId, int year, int month, int day, int todoId, boolean check, String token){ + if(!accountManager.checkToken(accountId, token)){ + return; + } + if(!accountManager.checkToken(accountId, token)){ + return; + } + if(!todos.containsKey(accountId)){ + return; + } + if(!todos.get(accountId).containsKey(bookId)){ + return; + } + if(!todos.get(accountId).get(bookId).containsKey(year)){ + return; + } + if(!todos.get(accountId).get(bookId).get(year).containsKey(month)){ + return; + } + if(!todos.get(accountId).get(bookId).get(year).get(month).containsKey(day)){ + return; + } + if(!todos.get(accountId).get(bookId).get(year).get(month).get(day).containsKey(todoId)){ + return; + } todos.get(accountId).get(bookId).get(year).get(month).get(day).get(todoId).setCheck(check); }