From 48178cc64aff6114ea326638167af76d8e659290 Mon Sep 17 00:00:00 2001 From: jiyunio Date: Thu, 26 Dec 2024 15:38:01 +0900 Subject: [PATCH] =?UTF-8?q?[Refactor]=20todolist=20=EA=B4=80=EB=A0=A8=20?= =?UTF-8?q?=EA=B8=B0=EB=8A=A5=EB=93=A4=20=EB=A6=AC=ED=8C=A9=ED=86=A0?= =?UTF-8?q?=EB=A7=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../todolist/domain/todo/TodoService.java | 132 +++++++++++++++++ .../todolist/global/config/WebConfig.java | 2 +- .../jiyunio/todolist/todo/TodoService.java | 133 ------------------ 3 files changed, 133 insertions(+), 134 deletions(-) create mode 100644 src/main/java/com/jiyunio/todolist/domain/todo/TodoService.java delete mode 100644 src/main/java/com/jiyunio/todolist/todo/TodoService.java diff --git a/src/main/java/com/jiyunio/todolist/domain/todo/TodoService.java b/src/main/java/com/jiyunio/todolist/domain/todo/TodoService.java new file mode 100644 index 0000000..90f446f --- /dev/null +++ b/src/main/java/com/jiyunio/todolist/domain/todo/TodoService.java @@ -0,0 +1,132 @@ +package com.jiyunio.todolist.domain.todo; + +import com.jiyunio.todolist.domain.category.Category; +import com.jiyunio.todolist.domain.category.CategoryRepository; +import com.jiyunio.todolist.domain.category.dto.CategoryRes; +import com.jiyunio.todolist.domain.member.Member; +import com.jiyunio.todolist.domain.member.MemberRepository; +import com.jiyunio.todolist.domain.todo.domain.Todo; +import com.jiyunio.todolist.domain.todo.domain.TodoList; +import com.jiyunio.todolist.domain.todo.dto.CreateTodoReq; +import com.jiyunio.todolist.domain.todo.dto.TodoListRes; +import com.jiyunio.todolist.domain.todo.dto.TodoRes; +import com.jiyunio.todolist.domain.todo.dto.UpdateTodoReq; +import com.jiyunio.todolist.global.customError.CustomException; +import com.jiyunio.todolist.global.customError.ErrorCode; +import lombok.RequiredArgsConstructor; +import org.springframework.http.HttpStatus; +import org.springframework.stereotype.Service; + +import java.time.LocalDate; +import java.util.ArrayList; +import java.util.List; +import java.util.Objects; + +@Service +@RequiredArgsConstructor +public class TodoService { + private final MemberRepository memberRepository; + private final TodoRepository todoRepository; + private final TodoListRepository todoListRepository; + private final CategoryRepository categoryRepository; + + public TodoRes createTodo(String userId, CreateTodoReq createTodo) { + Member member = memberRepository.findByUserId(userId).orElseThrow( + // 회원 존재 안함 + () -> new CustomException(HttpStatus.NOT_FOUND, ErrorCode.NOT_EXIST_MEMBER) + ); + Category category = categoryRepository.findById(createTodo.getCategoryId()).orElseThrow( + // 카테고리 없음 + () -> new CustomException(HttpStatus.NOT_FOUND, ErrorCode.NOT_EXIST_CATEGORY) + ); + + Todo todo = Todo.builder() + .content(createTodo.getContent()) + .checked(false) + .categoryId(category.getId()) + .categoryContent(category.getContent()) + .categoryColor(category.getColor()) + .build(); + + + TodoList todoList = todoListRepository.findByUserIdAndTodoListDate(member.getUserId(), createTodo.getSetDate()).orElseThrow( + () -> new CustomException(HttpStatus.NOT_FOUND, ErrorCode.NOT_EXIST_TODOLIST) + ); + + todoList.getTodos().add(todo); + + return TodoRes.from(todo); + } + + public TodoListRes createTodoList(String userId, LocalDate todoListDate) { + Member member = memberRepository.findByUserId(userId).orElseThrow( + // 회원 존재 안함 + () -> new CustomException(HttpStatus.NOT_FOUND, ErrorCode.NOT_EXIST_MEMBER) + ); + + TodoList todoList = TodoList.builder() + .userId(member.getUserId()) + .todoListDate(todoListDate) + .isToday(checkToday(todoListDate)) + .todos(new ArrayList<>()) + .build(); + + todoListRepository.save(todoList); + return TodoListRes.from(todoList); + } + + public List getTodos(String userId) { + List todoList = todoListRepository.findAllByUserId(userId); + if (todoList == null) { + throw new CustomException(HttpStatus.NOT_FOUND, ErrorCode.NOT_EXIST_MEMBER); + } + + return new ArrayList<>(todoList.stream() + .map(TodoListRes::from).toList()); + } + + public TodoRes updateTodo(Long todoId, UpdateTodoReq updateTodo) { + Todo todo = todoRepository.findById(todoId).orElseThrow( + () -> new CustomException(HttpStatus.NOT_FOUND, ErrorCode.NOT_EXIST_TODO) + + ); + Category category = categoryRepository.findById(updateTodo.getCategoryId()).get(); + todo.updateTodo(updateTodo); + todo.updateCategory(CategoryRes.builder() + .categoryId(category.getId()) + .color(category.getColor()) + .content(category.getContent()) + .build()); + + return TodoRes.from(todo); + } + + public void updateCategory(CategoryRes categoryDTO) { + List todoList = todoRepository.findByCategoryId(categoryDTO.getCategoryId()); + if (todoList.isEmpty()) { + return; + } + for (Todo todo : todoList) { + // 변경된 카테고리와 관련된 todo의 cateory도 같이 변경 + todo.updateCategory(categoryDTO); + todoRepository.save(todo); + } + } + + public void deleteTodo(Long todoId) { + todoRepository.deleteById(todoId); + } + + public void deleteTodoList(Long todoListId) { + todoListRepository.deleteById(todoListId); + } + + public void deleteAllTodo(String userId) { + todoListRepository.deleteAllByUserId(userId); + } + + public boolean checkToday(LocalDate localDate) { + LocalDate today = LocalDate.now(); + return Objects.equals(today, localDate); + } +} diff --git a/src/main/java/com/jiyunio/todolist/global/config/WebConfig.java b/src/main/java/com/jiyunio/todolist/global/config/WebConfig.java index 9951896..f482183 100644 --- a/src/main/java/com/jiyunio/todolist/global/config/WebConfig.java +++ b/src/main/java/com/jiyunio/todolist/global/config/WebConfig.java @@ -1,4 +1,4 @@ -package com.jiyunio.todolist.config; +package com.jiyunio.todolist.global.config; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Configuration; diff --git a/src/main/java/com/jiyunio/todolist/todo/TodoService.java b/src/main/java/com/jiyunio/todolist/todo/TodoService.java deleted file mode 100644 index 136464c..0000000 --- a/src/main/java/com/jiyunio/todolist/todo/TodoService.java +++ /dev/null @@ -1,133 +0,0 @@ -package com.jiyunio.todolist.todo; - -import com.jiyunio.todolist.category.Category; -import com.jiyunio.todolist.category.CategoryRepository; -import com.jiyunio.todolist.customError.CustomException; -import com.jiyunio.todolist.customError.ErrorCode; -import com.jiyunio.todolist.member.Member; -import com.jiyunio.todolist.member.MemberRepository; -import com.jiyunio.todolist.responseDTO.ResponseCategoryDTO; -import com.jiyunio.todolist.responseDTO.ResponseTodoDTO; -import com.jiyunio.todolist.todo.dto.CreateTodoDTO; -import com.jiyunio.todolist.todo.dto.UpdateTodoDTO; -import lombok.RequiredArgsConstructor; -import org.springframework.http.HttpStatus; -import org.springframework.stereotype.Service; - -import java.util.ArrayList; -import java.util.List; - -@Service -@RequiredArgsConstructor -public class TodoService { - private final MemberRepository memberRepository; - private final TodoRepository todoRepository; - private final CategoryRepository categoryRepository; - - public ResponseTodoDTO createTodo(String userId, CreateTodoDTO createTodo) { - Member member = memberRepository.findByUserId(userId).orElseThrow( - // 회원 존재 안함 - () -> new CustomException(HttpStatus.NOT_FOUND, ErrorCode.NOT_EXIST_MEMBER) - ); - Category category = categoryRepository.findById(createTodo.getCategoryId()).orElseThrow( - // 카테고리 없음 - () -> new CustomException(HttpStatus.NOT_FOUND, ErrorCode.NOT_EXIST_CATEGORY) - ); - - Todo todo = Todo.builder() - .userId(member.getUserId()) - .content(createTodo.getContent()) - .setDate(createTodo.getSetDate()) - .checked(false) - .categoryId(category.getId()) - .categoryContent(category.getContent()) - .categoryColor(category.getColor()) - .build(); - - todoRepository.save(todo); - - return ResponseTodoDTO.builder() - .todoId(todo.getId()) - .content(todo.getContent()) - .checked(todo.getChecked()) - .setDate(todo.getSetDate()) - .category(ResponseCategoryDTO.builder() - .categoryId(todo.getCategoryId()) - .content(todo.getContent()) - .color(todo.getCategoryColor()) - .build()) - .build(); - } - - public List getTodo(String userId) { - List todoList = todoRepository.findByUserId(userId); - if (todoList == null) { - throw new CustomException(HttpStatus.NOT_FOUND, ErrorCode.NOT_EXIST_MEMBER); - } - - List getTodoList = new ArrayList<>(); - - for (Todo todo : todoList) { - getTodoList.add(ResponseTodoDTO.builder() - .todoId(todo.getId()) - .content(todo.getContent()) - .setDate(todo.getSetDate()) - .checked(todo.getChecked()) - .category(ResponseCategoryDTO.builder() - .categoryId(todo.getCategoryId()) - .content(todo.getCategoryContent()) - .color(todo.getCategoryColor()) - .build()) - .build()); - } - return getTodoList; - } - - public ResponseTodoDTO updateTodo(Long todoId, UpdateTodoDTO updateTodo) { - Todo todo = todoRepository.findById(todoId).get(); - Category category = categoryRepository.findById(updateTodo.getCategoryId()).get(); - todo.updateTodo(updateTodo); - todo.updateCategory(ResponseCategoryDTO.builder() - .categoryId(category.getId()) - .color(category.getColor()) - .content(category.getContent()) - .build()); - todoRepository.save(todo); - - return ResponseTodoDTO.builder() - .todoId(todo.getId()) - .content(todo.getContent()) - .checked(todo.getChecked()) - .setDate(todo.getSetDate()) - .category(ResponseCategoryDTO.builder() - .categoryId(todo.getCategoryId()) - .content(todo.getCategoryContent()) - .color(todo.getCategoryColor()) - .build()) - .build(); - } - - public void updateCategory(ResponseCategoryDTO categoryDTO) { - List todoList = todoRepository.findByCategoryId(categoryDTO.getCategoryId()); - if (todoList.isEmpty()) { - return; - } else { - for (Todo todo : todoList) { - // 변경된 카테고리와 관련된 todo의 cateory도 같이 변경 - todo.updateCategory(categoryDTO); - todoRepository.save(todo); - } - } - } - - public void deleteTodo(Long todoId) { - todoRepository.deleteById(todoId); - } - - public void deleteTodos(String userId) { - List todoList = getTodo(userId); - for (ResponseTodoDTO todo : todoList) { - todoRepository.deleteById(todo.getTodoId()); - } - } -}