Skip to content

Commit

Permalink
[Feat] todo 생성 오류 수정 & 카테고리 삭제시 todo도 함께 삭제 기능 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
jiyunio committed Jun 17, 2024
1 parent eee163b commit fb5088d
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ public ResponseEntity<ResponseCategoryDTO> updateCategory(@Parameter(description
@DeleteMapping("/{categoryId}")
@Operation(summary = "카테고리 삭제")
@ApiResponse(responseCode = "200", description = "카테고리 삭제 성공", content = @Content(schema = @Schema(implementation = ResponseDTO.class)))
public ResponseEntity<ResponseDTO> deleteCategory(@Parameter(description = "카테고리의 id") @PathVariable Long categoryId) {
categoryService.deleteCategory(categoryId);
public ResponseEntity<ResponseDTO> deleteCategory(@AuthenticationPrincipal CustomUserDetails user, @Parameter(description = "카테고리의 id") @PathVariable Long categoryId) {
categoryService.deleteCategory(user.getUsername(), categoryId);
return ResponseEntity.ok(ResponseDTO.builder()
.msg("카테고리 삭제 성공")
.build());
Expand Down
26 changes: 20 additions & 6 deletions src/main/java/com/jiyunio/todolist/category/CategoryService.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
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.TodoRepository;
import com.jiyunio.todolist.todo.TodoService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
Expand All @@ -13,13 +15,15 @@

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Service
@RequiredArgsConstructor
@Transactional
public class CategoryService {
private final CategoryRepository categoryRepository;
private final MemberRepository memberRepository;
private final TodoRepository todoRepository;
private final CategoryRepository categoryRepository;
private final TodoService todoService;

public ResponseCategoryDTO createCategory(String userId, CategoryDTO categoryDTO) {
Expand Down Expand Up @@ -59,10 +63,13 @@ public ResponseCategoryDTO updateCategory(Long categoryId, CategoryDTO categoryD
Category category = categoryRepository.findById(categoryId).get();
category.updateCategory(categoryDTO);
categoryRepository.save(category);
todoService.updateCategory(categoryId, CategoryDTO.builder() //member의 category 상태도 변경
.content(category.getContent())
.color(category.getColor())
.build());

//member의 category 상태도 변경
todoService.updateCategory(ResponseCategoryDTO.builder()
.categoryId(categoryId)
.content(categoryDTO.getContent())
.color(categoryDTO.getColor()).build());


return ResponseCategoryDTO.builder()
.categoryId(category.getId())
Expand All @@ -71,11 +78,18 @@ public ResponseCategoryDTO updateCategory(Long categoryId, CategoryDTO categoryD
.build();
}

public void deleteCategory(Long categoryId) {
public void deleteCategory(String userId, Long categoryId) {
if (categoryRepository.count() == 1) {
//카데고리 개수 >= 1
throw new CustomException(HttpStatus.BAD_REQUEST, ErrorCode.NO_ANYMORE_CATEGORY);
} else {
// 카테고리 삭제시, 관련 todo도 함께 삭제
List<ResponseTodoDTO> todoList = todoService.getTodo(userId);
for (ResponseTodoDTO todo : todoList) {
if (Objects.equals(todo.getCategory().getCategoryId(), categoryId)) {
todoRepository.deleteById(todo.getTodoId());
}
}
categoryRepository.deleteById(categoryId);
}
}
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/com/jiyunio/todolist/todo/Todo.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.jiyunio.todolist.todo;

import com.jiyunio.todolist.category.CategoryDTO;
import com.jiyunio.todolist.responseDTO.ResponseCategoryDTO;
import com.jiyunio.todolist.todo.dto.UpdateTodoDTO;
import jakarta.persistence.*;
import lombok.AccessLevel;
Expand Down Expand Up @@ -53,8 +54,9 @@ protected void updateTodo(UpdateTodoDTO updateTodoDto) {
this.setDate = updateTodoDto.getSetDate();
}

protected void updateCategory(CategoryDTO categoryDTO) {
this.categoryContent = categoryDTO.getContent();
protected void updateCategory(ResponseCategoryDTO categoryDTO) {
this.categoryId = categoryDTO.getCategoryId();
; this.categoryContent = categoryDTO.getContent();
this.categoryColor = categoryDTO.getColor();
}
}
25 changes: 19 additions & 6 deletions src/main/java/com/jiyunio/todolist/todo/TodoService.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.jiyunio.todolist.todo;

import com.jiyunio.todolist.category.CategoryDTO;
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;
Expand All @@ -21,21 +22,26 @@
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(createTodo.getCategory().getCategoryId())
.categoryContent(createTodo.getCategory().getContent())
.categoryColor(createTodo.getCategory().getColor())
.categoryId(category.getId())
.categoryContent(category.getContent())
.categoryColor(category.getColor())
.build();

todoRepository.save(todo);
Expand Down Expand Up @@ -79,7 +85,13 @@ public List<ResponseTodoDTO> getTodo(String userId) {

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()
Expand All @@ -95,12 +107,13 @@ public ResponseTodoDTO updateTodo(Long todoId, UpdateTodoDTO updateTodo) {
.build();
}

public void updateCategory(Long categoryId, CategoryDTO categoryDTO) {
List<Todo> todoList = todoRepository.findByCategoryId(categoryId);
public void updateCategory(ResponseCategoryDTO categoryDTO) {
List<Todo> todoList = todoRepository.findByCategoryId(categoryDTO.getCategoryId());
if (todoList.isEmpty()) {
return;
} else {
for (Todo todo : todoList) {
// 변경된 카테고리와 관련된 todo의 cateory도 같이 변경
todo.updateCategory(categoryDTO);
todoRepository.save(todo);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.jiyunio.todolist.todo.dto;

import com.jiyunio.todolist.responseDTO.ResponseCategoryDTO;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
Expand All @@ -22,6 +21,6 @@ public class CreateTodoDTO {
private LocalDate setDate;

@NotNull
@Schema(description = "category")
private ResponseCategoryDTO category;
@Schema(description = "category Id", example = "1")
private Long categoryId;
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.jiyunio.todolist.todo.dto;

import com.jiyunio.todolist.category.CategoryDTO;
import com.jiyunio.todolist.responseDTO.ResponseCategoryDTO;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
Expand All @@ -27,15 +28,15 @@ public class UpdateTodoDTO {
private LocalDate setDate;

@NotNull
@Schema(description = "category")
private CategoryDTO category;
@Schema(description = "category Id", example = "1")
private Long categoryId;

@Builder
protected UpdateTodoDTO(String content, Boolean checked, LocalDate setDate,
CategoryDTO category) {
Long categoryId) {
this.content = content;
this.checked = checked;
this.setDate = setDate;
this.category = category;
this.categoryId = categoryId;
}
}

0 comments on commit fb5088d

Please sign in to comment.