-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathTodoService.java
More file actions
114 lines (105 loc) · 4.37 KB
/
Copy pathTodoService.java
File metadata and controls
114 lines (105 loc) · 4.37 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package pro.hexa.study.backend.todo.service;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;
import pro.hexa.study.backend.todo.domain.ChildTodo;
import pro.hexa.study.backend.todo.domain.ParentTodo;
import pro.hexa.study.backend.todo.domain.Todo;
import pro.hexa.study.backend.todo.dto.SaveTodoRequest;
import pro.hexa.study.backend.todo.dto.TodoInquiryDto;
import pro.hexa.study.backend.todo.dto.TodoResponse;
import pro.hexa.study.backend.todo.dto.TodoUpdateDto;
/*
* todoList를 위한 변환 로직
*/
public class TodoService {
public static Long ID_COUNT;
private List<Todo> todoEntities; // 저장되어있는 todo객체 전체
/*
* don't touch this function.
*/
public TodoService() {
ID_COUNT = 0L;
this.todoEntities = Collections.emptyList();
}
/*
* don't touch this function.
*/
public void saveTodoEntities(List<Todo> todoEntities) {
this.todoEntities = todoEntities;
}
/*
* todoList를 todoResponse로 변환
*/
public TodoResponse getTodoResponse() {
//
List<TodoInquiryDto> todoList = this.todoEntities.stream()
.filter(ParentTodo.class::isInstance)
.map(ParentTodo.class::cast)
.map(this::createTodoInquiryDtoFromParentTodo)
.collect(Collectors.toList());
return new TodoResponse(todoList);
}
private TodoInquiryDto createTodoInquiryDtoFromParentTodo(ParentTodo parentTodo) {
List<Long> id = Stream.concat(
Stream.of(parentTodo.getId()),
parentTodo.getDetails().stream()
.map(Todo::getId)
).collect(Collectors.toList());
String title = parentTodo.getTitle();
String content = parentTodo.getContent();
List<String> subTitles = parentTodo.getDetails().stream()
.map(Todo::getTitle)
.collect(Collectors.toList());
List<String> detailContents = parentTodo.getDetails().stream()
.map(Todo::getContent)
.collect(Collectors.toList());
short remainingTime = parentTodo.getTimeToTakeInMinutes();
LocalDateTime startAt = parentTodo.getStartAt();
boolean completeYn = parentTodo.getCompleteYn();
return new TodoInquiryDto(id, title, content, subTitles, detailContents, remainingTime, startAt, completeYn);
}
/*
* todoList 수정 요청 받아서 처리한 뒤 저장
*/
public void saveTodo(SaveTodoRequest request) {
// done
for (Long todoDeleteId : request.getTodoDeleteIds()) {
IntStream.range(0, todoEntities.size()).forEach(i -> {
if (Objects.equals(todoEntities.get(i).getId(), todoDeleteId)) {
todoEntities.remove(i);
return;
}
});
}
for (TodoUpdateDto todoUpdateDto : request.getTodoUpdateList()) {
boolean isTodoInTheList = this.todoEntities.stream()
.anyMatch(todo -> Objects.equals(todo.getId(), todoUpdateDto.getId()));
if (isTodoInTheList) {
int index = IntStream.range(0, todoEntities.size())
.filter(i -> Objects.equals(todoEntities.get(i).getId(), todoUpdateDto.getId()))
.findFirst()
.orElse(-1);
((ParentTodo) todoEntities.get(index)).updateTodo(todoUpdateDto.getTitle(),
todoUpdateDto.getDetail(),
(short) ChronoUnit.MINUTES.between(todoUpdateDto.getEndAt(), todoUpdateDto.getStartAt()),
todoUpdateDto.getStartAt(),
todoUpdateDto.getCompleteYn());
} else if (todoUpdateDto.getId() == null) {
ParentTodo newParentTodo = new ParentTodo(todoUpdateDto.getTitle(),
todoUpdateDto.getDetail(),
(short) ChronoUnit.MINUTES.between(todoUpdateDto.getEndAt(), todoUpdateDto.getStartAt()),
todoUpdateDto.getStartAt(),
todoUpdateDto.getCompleteYn());
this.todoEntities.add(newParentTodo);
} else {
throw new RuntimeException("존재하지 않는 Todo Id");
}
}
}
}