From f48cf17ecedade7502c4d09f6f7a90e84f3776f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adri=C3=A1n=20Oliva?= Date: Thu, 25 May 2023 19:29:46 -0600 Subject: POST to set done. POST on /todos/{id}/done to set the to do with {id} done. :3 --- src/main/java/com/encora/Main.java | 16 ++++++++++++++++ src/main/java/com/encora/ToDosRepository.java | 20 +++++++++++++++----- 2 files changed, 31 insertions(+), 5 deletions(-) (limited to 'src/main') diff --git a/src/main/java/com/encora/Main.java b/src/main/java/com/encora/Main.java index ec23015..108e657 100644 --- a/src/main/java/com/encora/Main.java +++ b/src/main/java/com/encora/Main.java @@ -54,4 +54,20 @@ public class Main { todo.setPriority(toDo.priority()); toDosRepository.save(todo); } + + // Update a to do with "done". + @ResponseStatus(value=HttpStatus.BAD_REQUEST, reason="No to do with such index.") + public static class toDoNotFound extends RuntimeException {} + + @PostMapping("/todos/{id}/done") + @ResponseStatus(value=HttpStatus.OK) + public void setDone(@PathVariable("id") Integer id) { + ToDos selectedToDo = toDosRepository.getById(id); + if (selectedToDo == null) throw new toDoNotFound(); + if (selectedToDo.isDone()) return; + + selectedToDo.setDone(true); + selectedToDo.setDoneDate(new Date()); + toDosRepository.save(selectedToDo); + } } diff --git a/src/main/java/com/encora/ToDosRepository.java b/src/main/java/com/encora/ToDosRepository.java index 5b8b292..f19d1ce 100644 --- a/src/main/java/com/encora/ToDosRepository.java +++ b/src/main/java/com/encora/ToDosRepository.java @@ -66,6 +66,21 @@ public class ToDosRepository implements JpaRepository { return null; } + // Retrieve a to do. + @Override + public ToDos getById(Integer integer) { + ToDos selectedToDo; + + for (int index = 0; index < this.todos.size(); index++) { + selectedToDo = this.todos.get(index); + if (Objects.equals(selectedToDo.getId(), integer)) { + return selectedToDo; + } + } + + return null; + } + /* * N O T Y E T D E F I N E D . */ @@ -104,11 +119,6 @@ public class ToDosRepository implements JpaRepository { return null; } - @Override - public ToDos getById(Integer integer) { - return null; - } - @Override public ToDos getReferenceById(Integer integer) { return null; -- cgit v1.2.3