Created
June 14, 2020 06:27
-
-
Save avinash10584/9ea3a74febc297697758c804326e830c to your computer and use it in GitHub Desktop.
TodoListController.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@RestController("/") | |
public class TodoListController { | |
private ToDoList sample = new ToDoList(1, Arrays.asList(new ToDo(1, "Comment", false), new ToDo(2, "Clap", false), | |
new ToDo(3, "Follow Author", false)), false); | |
@GetMapping | |
public ToDoList getList() { | |
return sample; | |
} | |
@GetMapping("/todo/{id}") | |
public Optional<ToDo> getToDo(@PathVariable("id") long id) { | |
return sample.getTasks().stream().filter(x -> x.getId() == id).findFirst(); | |
} | |
@PutMapping("/todo") | |
public ToDoList addToDo(@RequestBody ToDo toDo) { | |
sample.getTasks().add(toDo); | |
return sample; | |
} | |
@PostMapping("/todo/{id}") | |
public ToDo updateToDo(@PathVariable("id") long id, @RequestBody ToDo toDo) throws Exception { | |
Optional<ToDo> item = sample.getTasks().stream().filter(x -> x.getId() == id).findFirst(); | |
if (item.isPresent()) { | |
item.get().setCompleted(toDo.isCompleted()); | |
return item.get(); | |
} else { | |
throw new Exception("To Do item not found"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment