diff options
author | Adrián Oliva <adrian.oliva@cimat.mx> | 2023-05-25 19:48:28 -0600 |
---|---|---|
committer | Adrián Oliva <adrian.oliva@cimat.mx> | 2023-05-25 19:48:28 -0600 |
commit | b51e47b7abfb8340ab4ee67a2fc95cb07fcbfbd1 (patch) | |
tree | 5c7a706316f3545bb457e6e23dd7ef730d129d76 /src/main/java/com/encora | |
parent | e366121acd2aeefd3ce70fd07c09d2bdbcfeb56b (diff) | |
download | ToDo-App-BE-b51e47b7abfb8340ab4ee67a2fc95cb07fcbfbd1.tar.gz ToDo-App-BE-b51e47b7abfb8340ab4ee67a2fc95cb07fcbfbd1.zip |
New PUT request to edit to dos.
Using PUT request on path /todos/{id}, the to do with id {id} will be
updated with the new information parsed as body.
Diffstat (limited to 'src/main/java/com/encora')
-rw-r--r-- | src/main/java/com/encora/Main.java | 23 |
1 files changed, 20 insertions, 3 deletions
diff --git a/src/main/java/com/encora/Main.java b/src/main/java/com/encora/Main.java index 97ad25f..63c6a48 100644 --- a/src/main/java/com/encora/Main.java +++ b/src/main/java/com/encora/Main.java @@ -35,7 +35,7 @@ public class Main { // Add a new to do. @ResponseStatus(value=HttpStatus.BAD_REQUEST, reason="Text is longer than 120 characters.") public static class longerThanMaxException extends RuntimeException {} - record NewToDo( + record toDoBody( String text, Date dueDate, Priority priority @@ -44,7 +44,7 @@ public class Main { } @PostMapping("/todos") @ResponseStatus(value=HttpStatus.OK) - public void addToDo(@RequestBody NewToDo toDo) { + public void addToDo(@RequestBody toDoBody toDo) { if (toDo.text().length() > 120) { throw new longerThanMaxException(); } @@ -55,10 +55,27 @@ public class Main { toDosRepository.save(todo); } - // Update a to do with "done". + // Updates to do with new information @ResponseStatus(value=HttpStatus.BAD_REQUEST, reason="No to do with such index.") public static class toDoNotFound extends RuntimeException {} + @PutMapping("/todos/{id}") + @ResponseStatus(value=HttpStatus.OK) + public void editToDo(@PathVariable("id") Integer id, @RequestBody toDoBody toDo) { + ToDos selectedToDo = toDosRepository.getById(id); + if (selectedToDo == null) throw new toDoNotFound(); + + if (toDo.text() != null) { + if (toDo.text().length() > 120) throw new longerThanMaxException(); + selectedToDo.setText(toDo.text()); + } + if (toDo.dueDate() != null) selectedToDo.setDueDate(toDo.dueDate()); + if (toDo.priority() != null) selectedToDo.setPriority(toDo.priority()); + } + + + // Update a to do with "done". + @PostMapping("/todos/{id}/done") @ResponseStatus(value=HttpStatus.OK) public void setDone(@PathVariable("id") Integer id) { |