-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTodoController.java
More file actions
51 lines (44 loc) · 1.47 KB
/
TodoController.java
File metadata and controls
51 lines (44 loc) · 1.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package com.example.demotodo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.NoSuchElementException;
@RestController
public class TodoController {
@Autowired
private TodoService service;
// RESTful API methods for Retrieval operations
@GetMapping("/todos")
public List<Todo> list(){
return service.listAll();
}
@GetMapping("/todos/{id}")
public ResponseEntity<Todo> get(@PathVariable Integer id) {
try {
Todo todo = service.get(id);
return new ResponseEntity<Todo>(todo, HttpStatus.OK);
} catch (NoSuchElementException e) {
return new ResponseEntity<Todo>(HttpStatus.NOT_FOUND);
}
}
@PostMapping("/todos")
public void add(@RequestBody Todo todo){
service.save(todo);
}
@PutMapping("/todos/{id}")
public ResponseEntity<?> update(@RequestBody Todo todo, @PathVariable Integer id) {
try {
Todo existProduct = service.get(id);
service.save(todo);
return new ResponseEntity<>(HttpStatus.OK);
} catch (NoSuchElementException e) {
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
}
@DeleteMapping("/todos/{id}")
public void delete(@PathVariable Integer id) {
service.delete(id);
}
}