Skip to content

Commit

Permalink
[Refactor] JPA 연관관계 매핑
Browse files Browse the repository at this point in the history
  • Loading branch information
jiyunio committed May 29, 2024
1 parent 460277b commit 10c67f0
Show file tree
Hide file tree
Showing 10 changed files with 50 additions and 31 deletions.
7 changes: 1 addition & 6 deletions src/main/java/com/jiyunio/todolist/category/Category.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,11 @@ public class Category {

private String color;

@OneToMany(fetch = FetchType.LAZY)
@JoinColumn(name = "todoId")
List<Todo> todo;

@Builder
protected Category(Member member, String content, String color, List<Todo> todo) {
protected Category(Member member, String content, String color) {
this.member = member;
this.content = content;
this.color = color;
this.todo = todo;
}

protected void updateCategory(CategoryDTO categoryDTO) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,17 @@ public ResponseEntity<ResponseCategoryDTO> createCategory(@Parameter(description
return new ResponseEntity<>(categoryService.createCategory(memberId, categoryDTO), HttpStatus.CREATED);
}

@GetMapping("/{memberId}")
@Operation(summary = "카테고리 조회")
public List<ResponseCategoryDTO> getCategory(@Parameter(description = "member의 id") @PathVariable Long memberId) {
return categoryService.getCategory(memberId);
@GetMapping("/categories/{memberId}")
@Operation(summary = "카테고리 전체 조회")
public List<ResponseCategoryDTO> getCategories(@Parameter(description = "member의 id") @PathVariable Long memberId) {
return categoryService.getCategories(memberId);
}

@GetMapping("/{memberId}/{categoryId}")
@Operation(summary = "카테고리 전체 조회")
public ResponseCategoryDTO getCategory(@Parameter(description = "member의 id") @PathVariable Long memberId,
@Parameter(description = "category의 id") @PathVariable Long categoryId) {
return categoryService.getCategory(memberId, categoryId);
}

@PutMapping("/{categoryId}")
Expand Down
19 changes: 17 additions & 2 deletions src/main/java/com/jiyunio/todolist/category/CategoryService.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.jiyunio.todolist.member.MemberRepository;
import com.jiyunio.todolist.responseDTO.ResponseCategoryDTO;
import com.jiyunio.todolist.todo.Todo;
import com.jiyunio.todolist.todo.TodoRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
Expand All @@ -20,6 +21,7 @@
public class CategoryService {
private final CategoryRepository categoryRepository;
private final MemberRepository memberRepository;
private final TodoRepository todoRepository;

public ResponseCategoryDTO createCategory(Long memberId, CategoryDTO categoryDTO) {
Member member = memberRepository.findById(memberId).orElseThrow(
Expand All @@ -40,7 +42,7 @@ public ResponseCategoryDTO createCategory(Long memberId, CategoryDTO categoryDTO
.build();
}

public List<ResponseCategoryDTO> getCategory(Long memberId) {
public List<ResponseCategoryDTO> getCategories(Long memberId) {
List<Category> categories = categoryRepository.findByMemberId(memberId);
List<ResponseCategoryDTO> getCategoryDTO = new ArrayList<>();

Expand All @@ -54,12 +56,25 @@ public List<ResponseCategoryDTO> getCategory(Long memberId) {
return getCategoryDTO;
}

public ResponseCategoryDTO getCategory(Long memberId, Long categoryId) {
List<Category> categories = categoryRepository.findByMemberId(memberId);
for (Category category : categories){
if(category.getId().equals(categoryId)){
return ResponseCategoryDTO.builder()
.categoryId(categoryId)
.content(category.getContent())
.color(category.getColor())
.build();
}
}
throw new CustomException(HttpStatus.NOT_FOUND, ErrorCode.NOT_EXIST_CATEGORY);
}

public ResponseCategoryDTO updateCategory(Long categoryId, CategoryDTO categoryDTO) {
Category category = categoryRepository.findById(categoryId).get();
category.updateCategory(categoryDTO);
categoryRepository.save(category);

List<Todo> todoList = category.getTodo();

return ResponseCategoryDTO.builder()
.categoryId(category.getId())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public enum ErrorCode {
// 404 Not Found
NOT_EXIST_MEMBER("404_Not_Found", "회원이 존재하지 않습니다."),
NOT_EXIST_TODO("404_Not_Found", "TODO가 존재하지 않습니다."),
NOT_EXIST_CATEGORY("404_Not_Found", "Category가 존재하지 않습니다."),

// 409 Conflict (중복된 값)
EXIST_USERID("409_Conflict", "이미 존재하는 아이디입니다."),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,16 @@ public class ResponseTodoDTO {
private boolean checked;
private LocalDate writeDate;
private LocalDate setDate;
private Category category;
private Long categoryId;

@Builder
protected ResponseTodoDTO(Long todoId, String content, boolean checked,
Category category, LocalDate writeDate, LocalDate setDate, String color){
LocalDate writeDate, LocalDate setDate, Long categoryId){
this.todoId = todoId;
this.content = content;
this.checked = checked;
this.writeDate = writeDate;
this.setDate = setDate;
this.category = category;
this.categoryId = categoryId;
}
}
11 changes: 5 additions & 6 deletions src/main/java/com/jiyunio/todolist/todo/Todo.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,26 +34,25 @@ public class Todo {

private LocalDate setDate;

@OneToOne
private Category category;
private Long categoryId;


@Builder
protected Todo(Member member, String content, Boolean checked, Category category,
LocalDate writeDate, LocalDate setDate, String color) {
protected Todo(Member member, String content, Boolean checked,
LocalDate writeDate, LocalDate setDate, Long categoryId) {
this.member = member;
this.content = content;
this.checked = checked;
this.category = category;
this.writeDate = writeDate;
this.setDate = setDate;
this.categoryId = categoryId;
}

protected void updateTodo(GetUpdateTodoDTO getUpdateTodoDto) {
this.content = getUpdateTodoDto.getContent();
this.checked = getUpdateTodoDto.getChecked();
this.writeDate = getUpdateTodoDto.getWriteDate();
this.setDate = getUpdateTodoDto.getSetDate();
this.category = getUpdateTodoDto.getCategory();
this.categoryId = getUpdateTodoDto.getCategoryId();
}
}
2 changes: 2 additions & 0 deletions src/main/java/com/jiyunio/todolist/todo/TodoRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@ public interface TodoRepository extends JpaRepository<Todo, Long> {
List<Todo> findByMemberId(Long memberId);

Optional<Todo> findById(Long todoId);

boolean existsByMemberId(Long memberId);
}
10 changes: 5 additions & 5 deletions src/main/java/com/jiyunio/todolist/todo/TodoService.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public ResponseTodoDTO createTodo(Long memberId, CreateTodoDTO createTodo) {
.writeDate(createTodo.getWriteDate())
.setDate(createTodo.getSetDate())
.checked(false)
.category(createTodo.getCategory())
.categoryId(createTodo.getCategoryId())
.build();
todoRepository.save(todo);

Expand All @@ -42,7 +42,7 @@ public ResponseTodoDTO createTodo(Long memberId, CreateTodoDTO createTodo) {
.checked(todo.getChecked())
.writeDate(todo.getWriteDate())
.setDate(todo.getSetDate())
.category(todo.getCategory())
.categoryId(todo.getCategoryId())
.build();
}

Expand All @@ -57,13 +57,13 @@ public List<ResponseTodoDTO> getTodo(Long memberId) {
.writeDate(todo.getWriteDate())
.setDate(todo.getSetDate())
.checked(todo.getChecked())
.category(todo.getCategory())
.categoryId(todo.getCategoryId())
.build());
}
return getTodoList;
}

public ResponseTodoDTO updateTodo(Long todoId, GetUpdateTodoDTO updateTodo) {
public ResponseTodoDTO updateTodo(Long todoId,GetUpdateTodoDTO updateTodo) {
Todo todo = todoRepository.findById(todoId).get();
todo.updateTodo(updateTodo);
todoRepository.save(todo);
Expand All @@ -74,7 +74,7 @@ public ResponseTodoDTO updateTodo(Long todoId, GetUpdateTodoDTO updateTodo) {
.checked(todo.getChecked())
.writeDate(todo.getWriteDate())
.setDate(todo.getSetDate())
.category(todo.getCategory())
.categoryId(todo.getCategoryId())
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@ public class CreateTodoDTO {
private LocalDate setDate;

@NotBlank
private Category category;
private Long categoryId;
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ public class GetUpdateTodoDTO {
private LocalDate setDate;

@NotBlank
private Category category;
private Long categoryId;

@Builder
protected GetUpdateTodoDTO(String content, Boolean checked, Category category,
LocalDate writeDate, LocalDate setDate, String color) {
protected GetUpdateTodoDTO(String content, Boolean checked,
LocalDate writeDate, LocalDate setDate, Long categoryId) {
this.content = content;
this.checked = checked;
this.category = category;
this.writeDate = writeDate;
this.setDate = setDate;
this.categoryId = categoryId;
}
}

0 comments on commit 10c67f0

Please sign in to comment.