-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTodoController.java
More file actions
82 lines (61 loc) · 1.76 KB
/
TodoController.java
File metadata and controls
82 lines (61 loc) · 1.76 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package com.ll.demo02;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList;
import java.util.List;
@RestController
@RequestMapping("/todos")
public class TodoController {
private long todoLastId;
private List<Todo> todos;
public TodoController(){todos = new ArrayList<>();}
@GetMapping("")
public List<Todo> getTodos(){
return todos;
}
@GetMapping("/{id}")
public Todo getTodo2(
@PathVariable long id
){
return todos
.stream()
.filter(todo -> todo.getId() == id)
.findFirst()
.orElse(null);
}
@GetMapping("/add")
public Todo add(
String body
){
Todo todo = Todo
.builder()
.id(++todoLastId)
.body(body)
.build();
todos.add(todo);
return todo;
}
@GetMapping("/remove/{id}")
public boolean remove(
@PathVariable long id
){
boolean removed = todos.removeIf((todo -> todo.getId() == id));
return removed;
}
@GetMapping("/modify/{id}")
public boolean modify(
@PathVariable long id,
String body
){
Todo todo = todos
.stream()
.filter(_todo -> _todo.getId() == id)
.findFirst()
.orElse(null);
if(todo == null) return false;
todo.setBody(body);
return true;
}
}