22
33import java .util .List ;
44
5- import com .codeherk .taskapi .exception .TaskNotFoundException ;
65import com .codeherk .taskapi .model .Task ;
7- import com .codeherk .taskapi .repo .TaskRepository ;
6+ import com .codeherk .taskapi .service .TaskService ;
7+ import org .springframework .beans .factory .annotation .Autowired ;
88import org .springframework .web .bind .annotation .DeleteMapping ;
99import org .springframework .web .bind .annotation .GetMapping ;
1010import org .springframework .web .bind .annotation .PathVariable ;
1616@ RestController
1717public class TaskController {
1818
19- private final TaskRepository repository ;
19+ @ Autowired
20+ private final TaskService taskService ;
2021
21- TaskController (TaskRepository repository ) {
22- this .repository = repository ;
22+ TaskController (TaskService taskService ) {
23+ this .taskService = taskService ;
2324 }
2425
2526
2627 // Aggregate root
2728 // tag::get-aggregate-root[]
2829 @ GetMapping ("/tasks" )
29- List <Task > all () {
30- return repository . findAll ();
30+ List <Task > getTasks () {
31+ return taskService . getTasks ();
3132 }
3233 // end::get-aggregate-root[]
3334
3435 @ PostMapping ("/tasks" )
35- Task newTask (@ RequestBody Task newTask ) {
36- return repository . save (newTask );
36+ Task createTask (@ RequestBody Task newTask ) {
37+ return taskService . createTask (newTask );
3738 }
3839
39- // Single item
40-
40+ // Single task
4141 @ GetMapping ("/tasks/{id}" )
42- Task one (@ PathVariable Long id ) {
43-
44- return repository .findById (id )
45- .orElseThrow (() -> new TaskNotFoundException (id ));
42+ Task getTaskById (@ PathVariable Long id ) {
43+ return taskService .getTaskById (id );
4644 }
4745
4846 @ PutMapping ("/tasks/{id}" )
49- Task replaceTask (@ RequestBody Task newTask , @ PathVariable Long id ) {
50-
51- return repository .findById (id )
52- .map (Task -> {
53- Task .setDescription (newTask .getDescription ());
54- return repository .save (Task );
55- })
56- .orElseGet (() -> {
57- newTask .setId (id );
58- return repository .save (newTask );
59- });
47+ Task updateTask (@ RequestBody Task newTask , @ PathVariable Long id ) {
48+ return taskService .updateTask (newTask , id );
6049 }
6150
6251 @ DeleteMapping ("/tasks/{id}" )
6352 void deleteTask (@ PathVariable Long id ) {
64- repository . deleteById (id );
53+ taskService . deleteTask (id );
6554 }
6655}
0 commit comments