diff --git a/src/main/java/org/ntlab/citrusserver/repositories/TodoManager.java b/src/main/java/org/ntlab/citrusserver/repositories/TodoManager.java index 519fcfa..550e06a 100644 --- a/src/main/java/org/ntlab/citrusserver/repositories/TodoManager.java +++ b/src/main/java/org/ntlab/citrusserver/repositories/TodoManager.java @@ -1,8 +1,11 @@ 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; @@ -32,13 +35,6 @@ this.accountManager = accountManager; } - - private void checkToken(String accountId, String token){ -// if(!accountManager.checkToken(accountId, token)){ -// throw new ResponseStatusException(HttpStatus.FORBIDDEN); -// } - } - /** * アカウントと本を指定してそれに所属するtodoをすべて返す * @@ -57,13 +53,9 @@ */ public HashMap>>> getAllTodos(String accountId, int bookId, String token){ - if(!todos.containsKey(accountId)){ - throw new ResponseStatusException(HttpStatus.NOT_FOUND); + if(!accountManager.checkToken(accountId, token)){ + return null; } - if(!todos.get(accountId).containsKey(bookId)){ - throw new ResponseStatusException(HttpStatus.NOT_FOUND); - } - checkToken(accountId, token); return todos.get(accountId).get(bookId); } @@ -77,19 +69,15 @@ * @return そのアカウントの本に所属するtodoのうち、指定した年月のtodoを返します */ public HashMap> getTodosByMonth(String accountId, int bookId, int year, int month, String token){ - if(!todos.containsKey(accountId)){ - throw new ResponseStatusException(HttpStatus.NOT_FOUND); - } - if(!todos.get(accountId).containsKey(bookId)){ - throw new ResponseStatusException(HttpStatus.NOT_FOUND); - } if(!todos.get(accountId).get(bookId).containsKey(year)){ return new HashMap>(); } if(!todos.get(accountId).get(bookId).get(year).containsKey(month)){ return new HashMap>(); } - checkToken(accountId, token); + if(!accountManager.checkToken(accountId, token)){ + return null; + } return todos.get(accountId).get(bookId).get(year).get(month); } @@ -119,12 +107,6 @@ * @return そのアカウントの本に所属するtodoのうち、指定した年月日のtodoを返します */ public HashMap getTodosByDay(String accountId, int bookId, int year, int month, int day, String token){ - if(!todos.containsKey(accountId)){ - throw new ResponseStatusException(HttpStatus.NOT_FOUND); - } - if(!todos.get(accountId).containsKey(bookId)){ - throw new ResponseStatusException(HttpStatus.NOT_FOUND); - } if(!todos.get(accountId).get(bookId).containsKey(year)){ return new HashMap(); } @@ -134,7 +116,9 @@ if(!todos.get(accountId).get(bookId).get(year).get(month).containsKey(day)){ return new HashMap(); } - checkToken(accountId, token); + if(!accountManager.checkToken(accountId, token)){ + return null; + } return todos.get(accountId).get(bookId).get(year).get(month).get(day); } @@ -167,25 +151,9 @@ * @return idを指定してtodoを返す */ public Todo getTodoById(String accountId, int bookId, int year, int month, int day, int todoId, String token){ - if(!todos.containsKey(accountId)){ - throw new ResponseStatusException(HttpStatus.NOT_FOUND); + if(!accountManager.checkToken(accountId, token)){ + return null; } - if(!todos.get(accountId).containsKey(bookId)){ - throw new ResponseStatusException(HttpStatus.NOT_FOUND); - } - if(!todos.get(accountId).get(bookId).containsKey(year)){ - throw new ResponseStatusException(HttpStatus.NOT_FOUND); - } - if(!todos.get(accountId).get(bookId).get(year).containsKey(month)){ - throw new ResponseStatusException(HttpStatus.NOT_FOUND); - } - if(!todos.get(accountId).get(bookId).get(year).get(month).containsKey(day)){ - throw new ResponseStatusException(HttpStatus.NOT_FOUND); - } - if(!todos.get(accountId).get(bookId).get(year).get(month).get(day).containsKey(todoId)){ - throw new ResponseStatusException(HttpStatus.NOT_FOUND); - } - checkToken(accountId, token); return todos.get(accountId).get(bookId).get(year).get(month).get(day).get(todoId); } @@ -218,7 +186,9 @@ * @return 新しいtodoのid */ public int createTodo(String accountId, int bookId, int year, int month, int day, String title, String token){ - + if(!accountManager.checkToken(accountId, token)){ + return -1; + } if(!todos.containsKey(accountId)){ todos.put(accountId, new HashMap<>()); } @@ -234,7 +204,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<>()); } - checkToken(accountId, token); + String accountBook = accountId + bookId + year + month + day; if(!nextTodoId.containsKey(accountBook)){ nextTodoId.put(accountBook, 0); @@ -272,25 +242,9 @@ * @param todoId 削除したいtodoのid */ public void deleteTodoById(String accountId, int bookId, int year, int month, int day, int todoId, String token){ - if(!todos.containsKey(accountId)){ - throw new ResponseStatusException(HttpStatus.NOT_FOUND); + if(!accountManager.checkToken(accountId, token)){ + return; } - if(!todos.get(accountId).containsKey(bookId)){ - throw new ResponseStatusException(HttpStatus.NOT_FOUND); - } - if(!todos.get(accountId).get(bookId).containsKey(year)){ - throw new ResponseStatusException(HttpStatus.NOT_FOUND); - } - if(!todos.get(accountId).get(bookId).get(year).containsKey(month)){ - throw new ResponseStatusException(HttpStatus.NOT_FOUND); - } - if(!todos.get(accountId).get(bookId).get(year).get(month).containsKey(day)){ - throw new ResponseStatusException(HttpStatus.NOT_FOUND); - } - if(!todos.get(accountId).get(bookId).get(year).get(month).get(day).containsKey(todoId)){ - throw new ResponseStatusException(HttpStatus.NOT_FOUND); - } - checkToken(accountId, token); todos.get(accountId).get(bookId).get(year).get(month).get(day).remove(todoId); } @@ -319,25 +273,9 @@ * @param check 変更後の達成状態 */ public void setCheck(String accountId, int bookId, int year, int month, int day, int todoId, boolean check, String token){ - if(!todos.containsKey(accountId)){ - throw new ResponseStatusException(HttpStatus.NOT_FOUND); + if(!accountManager.checkToken(accountId, token)){ + return; } - if(!todos.get(accountId).containsKey(bookId)){ - throw new ResponseStatusException(HttpStatus.NOT_FOUND); - } - if(!todos.get(accountId).get(bookId).containsKey(year)){ - throw new ResponseStatusException(HttpStatus.NOT_FOUND); - } - if(!todos.get(accountId).get(bookId).get(year).containsKey(month)){ - throw new ResponseStatusException(HttpStatus.NOT_FOUND); - } - if(!todos.get(accountId).get(bookId).get(year).get(month).containsKey(day)){ - throw new ResponseStatusException(HttpStatus.NOT_FOUND); - } - if(!todos.get(accountId).get(bookId).get(year).get(month).get(day).containsKey(todoId)){ - throw new ResponseStatusException(HttpStatus.NOT_FOUND); - } - checkToken(accountId, token); todos.get(accountId).get(bookId).get(year).get(month).get(day).get(todoId).setCheck(check); }