Created
January 3, 2018 06:07
-
-
Save slmanju/7c906d03c9975866a137480ba4a52d63 to your computer and use it in GitHub Desktop.
This file contains 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 | |
@RequestMapping("/todos") | |
public class TodoController { | |
@Autowired | |
private TodoService todoService; | |
@GetMapping | |
public ResponseEntity<?> getAll() { | |
List<TodoDto> todos = todoService.findAll(); | |
if (todos == null || todos.isEmpty()) { | |
return ResponseEntity.status(HttpStatus.NOT_FOUND).build(); | |
} | |
return ResponseEntity.ok(todos); | |
} | |
@GetMapping("/{id}") | |
public ResponseEntity<?> getById(@PathVariable Long id) { | |
TodoDto dto = todoService.findById(id); | |
if (dto == null) { | |
return ResponseEntity.status(HttpStatus.NOT_FOUND).build(); | |
} | |
return ResponseEntity.ok(dto); | |
} | |
@PostMapping | |
public ResponseEntity<?> create(@RequestBody TodoDto todoDto) { | |
Long id = todoService.save(todoDto); | |
URI location = ServletUriComponentsBuilder.fromCurrentRequest() | |
.path("/{id}").buildAndExpand(id).toUri(); | |
return ResponseEntity.created(location).build(); | |
} | |
@PutMapping("/{id}") | |
public ResponseEntity<?> update(@PathVariable Long id, @RequestBody TodoDto todoDto) { | |
TodoDto dto = todoService.findById(id); | |
if (dto == null) { | |
return ResponseEntity.status(HttpStatus.NOT_FOUND).build(); | |
} | |
todoDto.setId(id); | |
todoService.update(todoDto); | |
return ResponseEntity.ok().build(); | |
} | |
@DeleteMapping("/{id}") | |
public ResponseEntity<?> delete(@PathVariable Long id) { | |
todoService.delete(id); | |
return ResponseEntity.ok().build(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment