Skip to content

Instantly share code, notes, and snippets.

@avinash10584
Created June 14, 2020 06:20
Show Gist options
  • Save avinash10584/9c2b8ae33fc16bf5858823304ed309c1 to your computer and use it in GitHub Desktop.
Save avinash10584/9c2b8ae33fc16bf5858823304ed309c1 to your computer and use it in GitHub Desktop.
TodoListController.java
@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