From d4548b16e6eea47c0715ea4a78a3b8392ae0ed19 Mon Sep 17 00:00:00 2001 From: eumppe Date: Thu, 27 Apr 2023 20:47:57 +0900 Subject: [PATCH 1/3] Yee --- .../study/backend/todo/domain/ChildTodo.java | 10 +++- .../study/backend/todo/domain/ParentTodo.java | 52 ++++++++++++++-- .../hexa/study/backend/todo/domain/Todo.java | 20 +++++++ .../backend/todo/dto/SaveTodoRequest.java | 7 +++ .../backend/todo/dto/TodoInquiryDto.java | 16 +++++ .../study/backend/todo/dto/TodoResponse.java | 10 ++++ .../study/backend/todo/dto/TodoUpdateDto.java | 25 ++++++++ .../backend/todo/service/TodoService.java | 59 ++++++++++++++++++- 8 files changed, 189 insertions(+), 10 deletions(-) diff --git a/src/pro/hexa/study/backend/todo/domain/ChildTodo.java b/src/pro/hexa/study/backend/todo/domain/ChildTodo.java index 2816497..2c7cbec 100644 --- a/src/pro/hexa/study/backend/todo/domain/ChildTodo.java +++ b/src/pro/hexa/study/backend/todo/domain/ChildTodo.java @@ -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; } } diff --git a/src/pro/hexa/study/backend/todo/domain/ParentTodo.java b/src/pro/hexa/study/backend/todo/domain/ParentTodo.java index 19c0cc4..8105b92 100644 --- a/src/pro/hexa/study/backend/todo/domain/ParentTodo.java +++ b/src/pro/hexa/study/backend/todo/domain/ParentTodo.java @@ -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 { @@ -18,16 +21,55 @@ public List 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; } } diff --git a/src/pro/hexa/study/backend/todo/domain/Todo.java b/src/pro/hexa/study/backend/todo/domain/Todo.java index 83e47d4..7970c5b 100644 --- a/src/pro/hexa/study/backend/todo/domain/Todo.java +++ b/src/pro/hexa/study/backend/todo/domain/Todo.java @@ -23,4 +23,24 @@ 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; + } + } diff --git a/src/pro/hexa/study/backend/todo/dto/SaveTodoRequest.java b/src/pro/hexa/study/backend/todo/dto/SaveTodoRequest.java index 5a2d358..63a910e 100644 --- a/src/pro/hexa/study/backend/todo/dto/SaveTodoRequest.java +++ b/src/pro/hexa/study/backend/todo/dto/SaveTodoRequest.java @@ -9,4 +9,11 @@ public class SaveTodoRequest { private List todoDeleteIds; private List todoUpdateList; + + public List getTodoDeleteIds() { + return this.todoDeleteIds; + } + public List getTodoUpdateList(){ + return this.todoUpdateList; + } } diff --git a/src/pro/hexa/study/backend/todo/dto/TodoInquiryDto.java b/src/pro/hexa/study/backend/todo/dto/TodoInquiryDto.java index 5fd0b84..8b3fabf 100644 --- a/src/pro/hexa/study/backend/todo/dto/TodoInquiryDto.java +++ b/src/pro/hexa/study/backend/todo/dto/TodoInquiryDto.java @@ -8,6 +8,18 @@ */ public class TodoInquiryDto { + public TodoInquiryDto(List id, String title, String content, List subTitles, List 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; + } + private List id; // 맨 첫 id가 parent, 그 뒤로 따라오는건 child private String title; // 제목 private String content; // 상세 내용 @@ -16,4 +28,8 @@ public class TodoInquiryDto { private Short remainingTime; // 예상되는 남은 시간(분) private LocalDateTime startAt; // 시작 예정시간 private boolean completeYn; // 완료 여부 + + public boolean getCompleteYn(){ + return this.completeYn; + } } diff --git a/src/pro/hexa/study/backend/todo/dto/TodoResponse.java b/src/pro/hexa/study/backend/todo/dto/TodoResponse.java index b473938..8d289ca 100644 --- a/src/pro/hexa/study/backend/todo/dto/TodoResponse.java +++ b/src/pro/hexa/study/backend/todo/dto/TodoResponse.java @@ -8,6 +8,16 @@ */ public class TodoResponse { + public TodoResponse(List todoList){ + this.totalCount=todoList.size(); + this.completedCount=0; + for(TodoInquiryDto todoInquiryDto:todoList ){ + if (todoInquiryDto.getCompleteYn()){ + this.completedCount+=1; + } + } + } + private List todoList; private Integer completedCount; // 완료한 todo객체 개수 private Integer totalCount; // 전체 todo객체 개수 diff --git a/src/pro/hexa/study/backend/todo/dto/TodoUpdateDto.java b/src/pro/hexa/study/backend/todo/dto/TodoUpdateDto.java index 66d52d6..a6abc5a 100644 --- a/src/pro/hexa/study/backend/todo/dto/TodoUpdateDto.java +++ b/src/pro/hexa/study/backend/todo/dto/TodoUpdateDto.java @@ -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; + } + } diff --git a/src/pro/hexa/study/backend/todo/service/TodoService.java b/src/pro/hexa/study/backend/todo/service/TodoService.java index 122e75e..a606046 100644 --- a/src/pro/hexa/study/backend/todo/service/TodoService.java +++ b/src/pro/hexa/study/backend/todo/service/TodoService.java @@ -1,10 +1,18 @@ 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.IntStream; +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를 위한 변환 로직 @@ -33,14 +41,59 @@ public void saveTodoEntities(List todoEntities) { * todoList를 todoResponse로 변환 */ public TodoResponse getTodoResponse() { - // todo - return null; + // + List todoList=Collections.emptyList(); + for(Todo todo:this.todoEntities){ + Collections.emptyList().add(createTodoInquiryDtoFromParentTodo((ParentTodo) todo)); + } + + return new TodoResponse(todoList); + } + + private TodoInquiryDto createTodoInquiryDtoFromParentTodo(ParentTodo parentTodo) { + List id = Collections.emptyList(); + List subTitles = Collections.emptyList(); + List detailContents = Collections.emptyList(); + id.add(parentTodo.getId()); + String title = parentTodo.getTitle(); + String content = parentTodo.getContent(); + for (ChildTodo childTodo : parentTodo.getDetails()) { + id.add(childTodo.getId()); + subTitles.add(childTodo.getTitle()); + detailContents.add(childTodo.getContent()); + } + 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 (TodoUpdateDto todoUpdateDto : request.getTodoUpdateList()) { + boolean isTodoInTheList = this.todoEntities.stream() + .anyMatch(todo -> todo.getId() == todoUpdateDto.getId()); + if (isTodoInTheList) { + int index = IntStream.range(0, todoEntities.size()) + .filter(i -> 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 { + ParentTodo newParentTodo = new ParentTodo(todoUpdateDto.getTitle(), + todoUpdateDto.getDetail(), + (short) ChronoUnit.MINUTES.between(todoUpdateDto.getEndAt(), todoUpdateDto.getStartAt()), + todoUpdateDto.getStartAt(), + todoUpdateDto.getCompleteYn()); + this.todoEntities.add(newParentTodo); + } + } } } From 077b43e2a12be97bfd2fe1bb4010c35068ea83dd Mon Sep 17 00:00:00 2001 From: eumppe Date: Fri, 28 Apr 2023 22:55:35 +0900 Subject: [PATCH 2/3] yee with delete --- src/pro/hexa/study/backend/todo/dto/SaveTodoRequest.java | 1 + src/pro/hexa/study/backend/todo/service/TodoService.java | 8 ++++++++ 2 files changed, 9 insertions(+) diff --git a/src/pro/hexa/study/backend/todo/dto/SaveTodoRequest.java b/src/pro/hexa/study/backend/todo/dto/SaveTodoRequest.java index 63a910e..bd075de 100644 --- a/src/pro/hexa/study/backend/todo/dto/SaveTodoRequest.java +++ b/src/pro/hexa/study/backend/todo/dto/SaveTodoRequest.java @@ -16,4 +16,5 @@ public List getTodoDeleteIds() { public List getTodoUpdateList(){ return this.todoUpdateList; } + } diff --git a/src/pro/hexa/study/backend/todo/service/TodoService.java b/src/pro/hexa/study/backend/todo/service/TodoService.java index a606046..dd2a434 100644 --- a/src/pro/hexa/study/backend/todo/service/TodoService.java +++ b/src/pro/hexa/study/backend/todo/service/TodoService.java @@ -95,5 +95,13 @@ public void saveTodo(SaveTodoRequest request) { this.todoEntities.add(newParentTodo); } } + for (Long todoDeleteId : request.getTodoDeleteIds()) { + IntStream.range(0,todoEntities.size()).forEach(i -> { + if(todoEntities.get(i).getId()==todoDeleteId){ + todoEntities.remove(i); + return; + } + }); + } } } From b8a0ab4095052c24dd91faa880e0715ac7380ab7 Mon Sep 17 00:00:00 2001 From: eumppe Date: Thu, 4 May 2023 08:12:17 +0900 Subject: [PATCH 3/3] yee with fixed update --- .../study/backend/todo/domain/ChildTodo.java | 8 +-- .../study/backend/todo/domain/ParentTodo.java | 12 ++-- .../hexa/study/backend/todo/domain/Todo.java | 17 +++-- .../backend/todo/dto/SaveTodoRequest.java | 3 +- .../backend/todo/dto/TodoInquiryDto.java | 25 ++++---- .../study/backend/todo/dto/TodoResponse.java | 19 +++--- .../study/backend/todo/dto/TodoUpdateDto.java | 12 ++-- .../backend/todo/service/TodoService.java | 63 ++++++++++--------- 8 files changed, 87 insertions(+), 72 deletions(-) diff --git a/src/pro/hexa/study/backend/todo/domain/ChildTodo.java b/src/pro/hexa/study/backend/todo/domain/ChildTodo.java index 2c7cbec..855221d 100644 --- a/src/pro/hexa/study/backend/todo/domain/ChildTodo.java +++ b/src/pro/hexa/study/backend/todo/domain/ChildTodo.java @@ -17,15 +17,15 @@ public ParentTodo getParent() { public void setParentTodo(ParentTodo parent) { // parentTodo를 지정하고, parentTodo의 details에 이 childTodo가 없으면 details에도 추가. - this.parent=parent; - int DoesParentHaveMe=parent.getDetailsIndexbyChildTodo(this); - if(DoesParentHaveMe==-1){ + this.parent = parent; + int DoesParentHaveMe = parent.getDetailsIndexbyChildTodo(this); + if (DoesParentHaveMe == -1) { parent.addDetailTodo(this); } } void completeTodo() { // 이 childTodo의 완료 처리 - this.completeYn=true; + this.completeYn = true; } } diff --git a/src/pro/hexa/study/backend/todo/domain/ParentTodo.java b/src/pro/hexa/study/backend/todo/domain/ParentTodo.java index 8105b92..fa197be 100644 --- a/src/pro/hexa/study/backend/todo/domain/ParentTodo.java +++ b/src/pro/hexa/study/backend/todo/domain/ParentTodo.java @@ -44,19 +44,19 @@ public void deleteDetailTodo(ChildTodo detail) { public void completeTodo(ChildTodo childTodo) { // 입력 받은 childTodo의 완료 처리와 동시에 parentTodo의 모든 details 내에 있는 todo가 완료 되었으면 parentTodo도 완료 처리. - int index=getDetailsIndexbyChildTodo(childTodo); + int index = getDetailsIndexbyChildTodo(childTodo); details.get(index).completeTodo(); - boolean isAllCompleted=details.stream() + boolean isAllCompleted = details.stream() .allMatch(detail -> detail.completeYn); - if(isAllCompleted){ - this.completeYn=true; + if (isAllCompleted) { + this.completeYn = true; } } - public void updateTodo(String title, String content, Short timeToTakeInMinutes, LocalDateTime startAt, boolean completeYn){ + public void updateTodo(String title, String content, Short timeToTakeInMinutes, LocalDateTime startAt, boolean completeYn) { this.id = TodoService.ID_COUNT++; this.title = title; this.content = content; @@ -65,7 +65,7 @@ public void updateTodo(String title, String content, Short timeToTakeInMinutes, this.completeYn = completeYn; } - public int getDetailsIndexbyChildTodo(ChildTodo detail){ + public int getDetailsIndexbyChildTodo(ChildTodo detail) { int index = IntStream.range(0, details.size()) .filter(i -> Objects.equals(details.get(i), detail)) .findFirst() diff --git a/src/pro/hexa/study/backend/todo/domain/Todo.java b/src/pro/hexa/study/backend/todo/domain/Todo.java index 7970c5b..f3acf96 100644 --- a/src/pro/hexa/study/backend/todo/domain/Todo.java +++ b/src/pro/hexa/study/backend/todo/domain/Todo.java @@ -24,22 +24,27 @@ protected Todo(String title, String content, Short timeToTakeInMinutes, LocalDat this.completeYn = completeYn; } - public Long getId(){ + public Long getId() { return this.id; } - public String getTitle(){ + + public String getTitle() { return this.title; } - public String getContent(){ + + public String getContent() { return this.content; } - public Short getTimeToTakeInMinutes(){ + + public Short getTimeToTakeInMinutes() { return this.timeToTakeInMinutes; } - public LocalDateTime getStartAt(){ + + public LocalDateTime getStartAt() { return this.startAt; } - public boolean getCompleteYn(){ + + public boolean getCompleteYn() { return this.completeYn; } diff --git a/src/pro/hexa/study/backend/todo/dto/SaveTodoRequest.java b/src/pro/hexa/study/backend/todo/dto/SaveTodoRequest.java index bd075de..e7dbce2 100644 --- a/src/pro/hexa/study/backend/todo/dto/SaveTodoRequest.java +++ b/src/pro/hexa/study/backend/todo/dto/SaveTodoRequest.java @@ -13,7 +13,8 @@ public class SaveTodoRequest { public List getTodoDeleteIds() { return this.todoDeleteIds; } - public List getTodoUpdateList(){ + + public List getTodoUpdateList() { return this.todoUpdateList; } diff --git a/src/pro/hexa/study/backend/todo/dto/TodoInquiryDto.java b/src/pro/hexa/study/backend/todo/dto/TodoInquiryDto.java index 8b3fabf..8989a9b 100644 --- a/src/pro/hexa/study/backend/todo/dto/TodoInquiryDto.java +++ b/src/pro/hexa/study/backend/todo/dto/TodoInquiryDto.java @@ -8,17 +8,6 @@ */ public class TodoInquiryDto { - public TodoInquiryDto(List id, String title, String content, List subTitles, List 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; - } private List id; // 맨 첫 id가 parent, 그 뒤로 따라오는건 child private String title; // 제목 @@ -29,7 +18,19 @@ public TodoInquiryDto(List id, String title, String content, List private LocalDateTime startAt; // 시작 예정시간 private boolean completeYn; // 완료 여부 - public boolean getCompleteYn(){ + public TodoInquiryDto(List id, String title, String content, List subTitles, List 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; } } diff --git a/src/pro/hexa/study/backend/todo/dto/TodoResponse.java b/src/pro/hexa/study/backend/todo/dto/TodoResponse.java index 8d289ca..c209f05 100644 --- a/src/pro/hexa/study/backend/todo/dto/TodoResponse.java +++ b/src/pro/hexa/study/backend/todo/dto/TodoResponse.java @@ -8,15 +8,6 @@ */ public class TodoResponse { - public TodoResponse(List todoList){ - this.totalCount=todoList.size(); - this.completedCount=0; - for(TodoInquiryDto todoInquiryDto:todoList ){ - if (todoInquiryDto.getCompleteYn()){ - this.completedCount+=1; - } - } - } private List todoList; private Integer completedCount; // 완료한 todo객체 개수 @@ -33,4 +24,14 @@ public int getCompletedCount() { public int getTotalCount() { return GetUtils.getIntegerAsInt(this.totalCount); } + + public TodoResponse(List todoList){ + this.totalCount=todoList.size(); + this.completedCount=0; + for(TodoInquiryDto todoInquiryDto:todoList ){ + if (todoInquiryDto.getCompleteYn()){ + this.completedCount+=1; + } + } + } } diff --git a/src/pro/hexa/study/backend/todo/dto/TodoUpdateDto.java b/src/pro/hexa/study/backend/todo/dto/TodoUpdateDto.java index a6abc5a..55e19b0 100644 --- a/src/pro/hexa/study/backend/todo/dto/TodoUpdateDto.java +++ b/src/pro/hexa/study/backend/todo/dto/TodoUpdateDto.java @@ -14,27 +14,27 @@ public class TodoUpdateDto { private LocalDateTime endAt; // 완료 예정시간 private boolean completeYn; // 완료 여부 - public Long getId(){ + public Long getId() { return this.id; } - public String getTitle(){ + public String getTitle() { return this.title; } - public String getDetail(){ + public String getDetail() { return this.detail; } - public LocalDateTime getStartAt(){ + public LocalDateTime getStartAt() { return this.startAt; } - public LocalDateTime getEndAt(){ + public LocalDateTime getEndAt() { return this.endAt; } - public boolean getCompleteYn(){ + public boolean getCompleteYn() { return this.completeYn; } diff --git a/src/pro/hexa/study/backend/todo/service/TodoService.java b/src/pro/hexa/study/backend/todo/service/TodoService.java index dd2a434..4825de1 100644 --- a/src/pro/hexa/study/backend/todo/service/TodoService.java +++ b/src/pro/hexa/study/backend/todo/service/TodoService.java @@ -5,7 +5,9 @@ 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; @@ -42,30 +44,33 @@ public void saveTodoEntities(List todoEntities) { */ public TodoResponse getTodoResponse() { // - List todoList=Collections.emptyList(); - for(Todo todo:this.todoEntities){ - Collections.emptyList().add(createTodoInquiryDtoFromParentTodo((ParentTodo) todo)); - } + List 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 id = Collections.emptyList(); - List subTitles = Collections.emptyList(); - List detailContents = Collections.emptyList(); - id.add(parentTodo.getId()); + List id = Stream.concat( + Stream.of(parentTodo.getId()), + parentTodo.getDetails().stream() + .map(Todo::getId) + ).collect(Collectors.toList()); String title = parentTodo.getTitle(); String content = parentTodo.getContent(); - for (ChildTodo childTodo : parentTodo.getDetails()) { - id.add(childTodo.getId()); - subTitles.add(childTodo.getTitle()); - detailContents.add(childTodo.getContent()); - } - short remainingTime=parentTodo.getTimeToTakeInMinutes(); - LocalDateTime startAt=parentTodo.getStartAt(); - boolean completeYn= parentTodo.getCompleteYn(); - return new TodoInquiryDto(id, title,content,subTitles,detailContents,remainingTime,startAt,completeYn); + List subTitles = parentTodo.getDetails().stream() + .map(Todo::getTitle) + .collect(Collectors.toList()); + List 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); } /* @@ -73,12 +78,20 @@ private TodoInquiryDto createTodoInquiryDtoFromParentTodo(ParentTodo parentTodo) */ 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 -> todo.getId() == todoUpdateDto.getId()); + .anyMatch(todo -> Objects.equals(todo.getId(), todoUpdateDto.getId())); if (isTodoInTheList) { int index = IntStream.range(0, todoEntities.size()) - .filter(i -> todoEntities.get(i).getId() == todoUpdateDto.getId()) + .filter(i -> Objects.equals(todoEntities.get(i).getId(), todoUpdateDto.getId())) .findFirst() .orElse(-1); ((ParentTodo) todoEntities.get(index)).updateTodo(todoUpdateDto.getTitle(), @@ -86,22 +99,16 @@ public void saveTodo(SaveTodoRequest request) { (short) ChronoUnit.MINUTES.between(todoUpdateDto.getEndAt(), todoUpdateDto.getStartAt()), todoUpdateDto.getStartAt(), todoUpdateDto.getCompleteYn()); - } else { + } 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"); } } - for (Long todoDeleteId : request.getTodoDeleteIds()) { - IntStream.range(0,todoEntities.size()).forEach(i -> { - if(todoEntities.get(i).getId()==todoDeleteId){ - todoEntities.remove(i); - return; - } - }); - } } }