From b51e47b7abfb8340ab4ee67a2fc95cb07fcbfbd1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adri=C3=A1n=20Oliva?= Date: Thu, 25 May 2023 19:48:28 -0600 Subject: 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. --- src/main/java/com/encora/Main.java | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) (limited to 'src/main/java') 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) { -- cgit v1.2.3