forked from CodeSoom/spring-week3-assignment-1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TaskService.java
54 lines (41 loc) · 1.19 KB
/
TaskService.java
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
package com.codesoom.assignment.application;
import com.codesoom.assignment.TaskNotFoundException;
import com.codesoom.assignment.models.Task;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
@Service
public class TaskService {
private List<Task> tasks = new ArrayList<>();
private Long newId = 0L;
public List<Task> getTasks() {
return tasks;
}
public Task getTask(Long id) {
return tasks.stream()
.filter(task -> task.getId().equals(id))
.findFirst()
.orElseThrow(() -> new TaskNotFoundException(id));
}
public Task createTask(Task source) {
Task task = new Task();
task.setId(generateId());
task.setTitle(source.getTitle());
tasks.add(task);
return task;
}
public Task updateTask(Long id, Task source) {
Task task = getTask(id);
task.setTitle(source.getTitle());
return task;
}
public Task deleteTask(Long id) {
Task task = getTask(id);
tasks.remove(task);
return task;
}
private Long generateId() {
newId += 1;
return newId;
}
}