private void validate(Long id, Todo todo) { if (isSameTodo(id, todo)) { throw new ForbiddenException("id does not match TODO"); } if (todoService.findOne(id) == null) { throw new NotFoundException(String.format("Todo with id %s not found", id)); } }
@RequestMapping(value = "/todos/{id}", method = RequestMethod.PUT) public ResponseEntity<Todo> update(@PathVariable Long id, @RequestBody Todo todo) { validate(id, todo); return new ResponseEntity<>(todoService.save(todo), OK); }
@RequestMapping(value = "/todos/{id}", method = RequestMethod.DELETE) public void delete(@PathVariable Long id) { todoService.delete(id); }
@RequestMapping(value = "/todos/{id}", method = RequestMethod.GET) public ResponseEntity<Todo> get(@PathVariable Long id) { final Todo todo = todoService.findOne(id); return new ResponseEntity<>(todo, todo == null ? NOT_FOUND : OK); }
@RequestMapping(value = "/todos", method = RequestMethod.POST) public ResponseEntity<Todo> create(@RequestBody @Valid Todo todo) { return new ResponseEntity<>(todoService.save(todo), HttpStatus.CREATED); }
@RequestMapping(value = "/todos", method = RequestMethod.GET) public List<Todo> findAll() { return todoService.find(); }