Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Yee #2

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/pro/hexa/study/backend/todo/domain/ChildTodo.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,16 @@ public ParentTodo getParent() {
}

public void setParentTodo(ParentTodo parent) {
// todo: parentTodo를 지정하고, parentTodo의 details에 이 childTodo가 없으면 details에도 추가.
// parentTodo를 지정하고, parentTodo의 details에 이 childTodo가 없으면 details에도 추가.
this.parent = parent;
int DoesParentHaveMe = parent.getDetailsIndexbyChildTodo(this);
if (DoesParentHaveMe == -1) {
parent.addDetailTodo(this);
}
}

void completeTodo() {
// todo: 이 childTodo의 완료 처리
// 이 childTodo의 완료 처리
this.completeYn = true;
}
}
52 changes: 47 additions & 5 deletions src/pro/hexa/study/backend/todo/domain/ParentTodo.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.IntStream;
import pro.hexa.study.backend.todo.service.TodoService;

public class ParentTodo extends Todo {

Expand All @@ -18,16 +21,55 @@ public List<ChildTodo> getDetails() {
}

public void addDetailTodo(ChildTodo detail) {
// todo: details에 detail을 추가하고, detail의 parent로 지금 이 parentTodo를 지정하기(parent가 이 객체가 아닐때만).
// todo: parentTodo의 걸리는 시간은 details의 걸리는 시간의 합과 같도록 항상 유지해줘야한다.
// details에 detail을 추가하고, detail의 parent로 지금 이 parentTodo를 지정하기(parent가 이 객체가 아닐때만).
// parentTodo의 걸리는 시간은 details의 걸리는 시간의 합과 같도록 항상 유지해줘야한다.
detail.setParentTodo(this);
details.add(detail);
this.timeToTakeInMinutes = (short) (this.timeToTakeInMinutes + detail.timeToTakeInMinutes);
}

public void deleteDetailTodo(ChildTodo detail) {
// todo: detail을 details에서 삭제하고, detail의 parent에서도 지금 이 parentTodo를 삭제하기.
// todo: parentTodo의 걸리는 시간은 details의 걸리는 시간의 합과 같도록 항상 유지해줘야한다.
// detail을 details에서 삭제하고, detail의 parent에서도 지금 이 parentTodo를 삭제하기.
// parentTodo의 걸리는 시간은 details의 걸리는 시간의 합과 같도록 항상 유지해줘야한다.

int index = getDetailsIndexbyChildTodo(detail);

if (index == -1) {
return;
}

this.timeToTakeInMinutes = (short) (this.timeToTakeInMinutes - details.get(index).timeToTakeInMinutes);
details.remove(index);
}

public void completeTodo(ChildTodo childTodo) {
// todo: 입력 받은 childTodo의 완료 처리와 동시에 parentTodo의 모든 details 내에 있는 todo가 완료 되었으면 parentTodo도 완료 처리.
// 입력 받은 childTodo의 완료 처리와 동시에 parentTodo의 모든 details 내에 있는 todo가 완료 되었으면 parentTodo도 완료 처리.
int index = getDetailsIndexbyChildTodo(childTodo);

details.get(index).completeTodo();

boolean isAllCompleted = details.stream()
.allMatch(detail -> detail.completeYn);
if (isAllCompleted) {
this.completeYn = true;
}

}

public void updateTodo(String title, String content, Short timeToTakeInMinutes, LocalDateTime startAt, boolean completeYn) {
this.id = TodoService.ID_COUNT++;
this.title = title;
this.content = content;
this.timeToTakeInMinutes = timeToTakeInMinutes;
this.startAt = startAt;
this.completeYn = completeYn;
}

public int getDetailsIndexbyChildTodo(ChildTodo detail) {
int index = IntStream.range(0, details.size())
.filter(i -> Objects.equals(details.get(i), detail))
.findFirst()
.orElse(-1);
return index;
}
}
25 changes: 25 additions & 0 deletions src/pro/hexa/study/backend/todo/domain/Todo.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,29 @@ protected Todo(String title, String content, Short timeToTakeInMinutes, LocalDat
this.startAt = startAt;
this.completeYn = completeYn;
}

public Long getId() {
return this.id;
}

public String getTitle() {
return this.title;
}

public String getContent() {
return this.content;
}

public Short getTimeToTakeInMinutes() {
return this.timeToTakeInMinutes;
}

public LocalDateTime getStartAt() {
return this.startAt;
}

public boolean getCompleteYn() {
return this.completeYn;
}

}
9 changes: 9 additions & 0 deletions src/pro/hexa/study/backend/todo/dto/SaveTodoRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,13 @@ public class SaveTodoRequest {

private List<Long> todoDeleteIds;
private List<TodoUpdateDto> todoUpdateList;

public List<Long> getTodoDeleteIds() {
return this.todoDeleteIds;
}

public List<TodoUpdateDto> getTodoUpdateList() {
return this.todoUpdateList;
}

}
17 changes: 17 additions & 0 deletions src/pro/hexa/study/backend/todo/dto/TodoInquiryDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*/
public class TodoInquiryDto {


private List<Long> id; // 맨 첫 id가 parent, 그 뒤로 따라오는건 child
private String title; // 제목
private String content; // 상세 내용
Expand All @@ -16,4 +17,20 @@ public class TodoInquiryDto {
private Short remainingTime; // 예상되는 남은 시간(분)
private LocalDateTime startAt; // 시작 예정시간
private boolean completeYn; // 완료 여부

public TodoInquiryDto(List<Long> id, String title, String content, List<String> subTitles, List<String> detailContents,
Short remainingTime, LocalDateTime startAt, boolean completeYn) {
this.id = id;
this.title = title;
this.content = content;
this.subTitles = subTitles;
this.detailContents = detailContents;
this.remainingTime = remainingTime;
this.startAt = startAt;
this.completeYn = completeYn;
}

public boolean getCompleteYn() {
return this.completeYn;
}
}
11 changes: 11 additions & 0 deletions src/pro/hexa/study/backend/todo/dto/TodoResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*/
public class TodoResponse {


private List<TodoInquiryDto> todoList;
private Integer completedCount; // 완료한 todo객체 개수
private Integer totalCount; // 전체 todo객체 개수
Expand All @@ -23,4 +24,14 @@ public int getCompletedCount() {
public int getTotalCount() {
return GetUtils.getIntegerAsInt(this.totalCount);
}

public TodoResponse(List<TodoInquiryDto> todoList){
this.totalCount=todoList.size();
this.completedCount=0;
for(TodoInquiryDto todoInquiryDto:todoList ){
if (todoInquiryDto.getCompleteYn()){
this.completedCount+=1;
}
}
}
}
25 changes: 25 additions & 0 deletions src/pro/hexa/study/backend/todo/dto/TodoUpdateDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,29 @@ public class TodoUpdateDto {
private LocalDateTime startAt; // 시작 예정시간
private LocalDateTime endAt; // 완료 예정시간
private boolean completeYn; // 완료 여부

public Long getId() {
return this.id;
}

public String getTitle() {
return this.title;
}

public String getDetail() {
return this.detail;
}

public LocalDateTime getStartAt() {
return this.startAt;
}

public LocalDateTime getEndAt() {
return this.endAt;
}

public boolean getCompleteYn() {
return this.completeYn;
}

}
74 changes: 71 additions & 3 deletions src/pro/hexa/study/backend/todo/service/TodoService.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
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를 위한 변환 로직
Expand Down Expand Up @@ -33,14 +43,72 @@ public void saveTodoEntities(List<Todo> todoEntities) {
* todoList를 todoResponse로 변환
*/
public TodoResponse getTodoResponse() {
// todo
return null;
//
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) {
// todo
// 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");
}
}
}
}