Skip to content

Commit

Permalink
[Feat] todo 삭제 및 todolist 삭제 관련 기능 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
jiyunio committed Dec 26, 2024
1 parent 07c4ad0 commit e46b70b
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ public CategoryRes updateCategory(Long categoryId, CategoryReq categoryReq) {
}

public void deleteCategory(String userId, Long categoryId) {
if (categoryRepository.count() == 1) {
//카데고리 개수 >= 1
if (categoryRepository.count() == 0) {
//카데고리 개수 == 0
throw new CustomException(HttpStatus.BAD_REQUEST, ErrorCode.NO_ANYMORE_CATEGORY);
}
// 카테고리 삭제시, 관련 todo도 함께 삭제
Expand Down
41 changes: 18 additions & 23 deletions src/main/java/com/jiyunio/todolist/domain/member/MemberService.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.jiyunio.todolist.domain.member;

import com.jiyunio.todolist.domain.category.CategoryService;
import com.jiyunio.todolist.domain.category.dto.CategoryReq;
import com.jiyunio.todolist.domain.member.dto.req.ChangePwReq;
import com.jiyunio.todolist.domain.member.dto.req.SignInReq;
import com.jiyunio.todolist.domain.member.dto.req.SignUpReq;
Expand Down Expand Up @@ -40,29 +39,25 @@ public MemberRes signUp(@Valid SignUpReq signUpReq) {
throw new CustomException(HttpStatus.CONFLICT, ErrorCode.EXIST_USERID);
}

if (signUpReq.getUserPw().equals(signUpReq.getConfirmUserPw())) {
// 회원가입 성공
Member member = Member.builder()
.userId(signUpReq.getUserId())
.userPw(passwordEncoder.encode(signUpReq.getUserPw()))
.nickname(signUpReq.getNickname())
.build();

memberRepository.save(member);

//기본 카테고리 동시에 생성
categoryService.createCategory(member.getUserId(), CategoryReq.builder()
.content("기본")
.color("FFFFFF").build());

return MemberRes.builder()
.memberId(member.getId())
.userId(member.getUserId())
.nickname(member.getNickname())
.build();
if (!signUpReq.getUserPw().equals(signUpReq.getConfirmUserPw())) {
// 비밀번호 불일치
throw new CustomException(HttpStatus.BAD_REQUEST, ErrorCode.NOT_SAME_CONFIRM_PASSWORD);
}
// 비밀번호 불일치
throw new CustomException(HttpStatus.BAD_REQUEST, ErrorCode.NOT_SAME_CONFIRM_PASSWORD);

// 회원가입 성공
Member member = Member.builder()
.userId(signUpReq.getUserId())
.userPw(passwordEncoder.encode(signUpReq.getUserPw()))
.nickname(signUpReq.getNickname())
.build();

memberRepository.save(member);

return MemberRes.builder()
.memberId(member.getId())
.userId(member.getUserId())
.nickname(member.getNickname())
.build();
}

public SignInRes signIn(@Valid SignInReq signInReq) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ public List<TodoListRes> getTodo(@AuthenticationPrincipal CustomUserDetails user
@PutMapping("/{todoId}")
@Operation(summary = "todo 수정")
@ApiResponse(responseCode = "200", description = "todo 수정 성공", content = @Content(schema = @Schema(implementation = TodoRes.class)))
public ResponseEntity<TodoRes> updateTodo(@Parameter(description = "todo의 id") @PathVariable Long todoId, @Valid @RequestBody UpdateTodoReq updateTodo) {
return ResponseEntity.ok(todoService.updateTodo(todoId, updateTodo));
public ResponseEntity<TodoRes> updateTodo(@AuthenticationPrincipal CustomUserDetails user, @Parameter(description = "todo의 id") @PathVariable Long todoId, @Valid @RequestBody UpdateTodoReq updateTodo) {
return ResponseEntity.ok(todoService.updateTodo(user.getUsername(), todoId, updateTodo));
}

@DeleteMapping("/{todoId}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,4 @@ public interface TodoListRepository extends JpaRepository<TodoList, Long> {

@Query("select count(tl) > 0 from TodoList tl where tl.userId = :userId and tl.todoListDate = :todoListDate")
boolean existsByUserIdAndTodoListDate(@Param("userId") String userId, @Param("todoListDate") LocalDate todoListDate);

}
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> findByCategoryId(Long categoryId);

List<Todo> findAllByTodoListId(Long todoListId);

void deleteAllByTodoListId(Long todoListId);
}
52 changes: 46 additions & 6 deletions src/main/java/com/jiyunio/todolist/domain/todo/TodoService.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.ArrayList;
Expand All @@ -25,6 +26,7 @@
@Slf4j
@Service
@RequiredArgsConstructor
@Transactional
public class TodoService {
private final MemberRepository memberRepository;
private final TodoRepository todoRepository;
Expand All @@ -41,9 +43,7 @@ public TodoRes createTodo(String userId, CreateTodoReq createTodo) {
() -> new CustomException(HttpStatus.NOT_FOUND, ErrorCode.NOT_EXIST_CATEGORY)
);

TodoList todoList = todoListRepository.findByUserIdAndTodoListDate(member.getUserId(), createTodo.getSetDate()).orElseThrow(
() -> new CustomException(HttpStatus.NOT_FOUND, ErrorCode.NOT_EXIST_TODOLIST)
);
TodoList todoList = makeTodoList(member, createTodo.getSetDate());

Todo todo = Todo.builder()
.content(createTodo.getContent())
Expand Down Expand Up @@ -92,22 +92,56 @@ public List<TodoListRes> getTodos(String userId) {
).toList();
}

public TodoRes updateTodo(Long todoId, UpdateTodoReq updateTodo) {
public TodoRes updateTodo(String userId, Long todoId, UpdateTodoReq updateTodo) {
Member member = memberRepository.findByUserId(userId).orElseThrow(
// 회원 존재 안함
() -> new CustomException(HttpStatus.NOT_FOUND, ErrorCode.NOT_EXIST_MEMBER)
);

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);

TodoList todoList = makeTodoList(member, updateTodo.getSetDate());

Category category = categoryRepository.findById(updateTodo.getCategoryId()).orElseThrow(
() -> new CustomException(HttpStatus.NOT_FOUND, ErrorCode.NOT_EXIST_CATEGORY)
);

Long pastTodoListId = todo.getTodoListId();

todo.updateTodo(updateTodo, todoList.getId());
todo.updateCategory(CategoryRes.builder()
.categoryId(category.getId())
.color(category.getColor())
.content(category.getContent())
.build());

//todo의 옮김으로 todolist 비었는지 확인
checkEmptyTodoList(pastTodoListId);

return TodoRes.from(todo);
}

private TodoList makeTodoList(Member member, LocalDate setDate) {
if (!todoListRepository.existsByUserIdAndTodoListDate(member.getUserId(), setDate) && !LocalDate.now().isEqual(setDate)) {
// 현재가 아니거나 만들어진 todolist가 없음
throw new CustomException(HttpStatus.NOT_FOUND, ErrorCode.NOT_EXIST_TODOLIST);
}

return todoListRepository.findByUserIdAndTodoListDate(member.getUserId(), setDate).orElseGet(
() -> {
// 현재는 자동으로 todolist 만들어줌
TodoList nowTodoList = TodoList.builder()
.userId(member.getUserId())
.todoListDate(setDate)
.build();
return todoListRepository.save(nowTodoList);
}
);
}

public void updateCategory(CategoryRes categoryDTO) {
List<Todo> todoList = todoRepository.findByCategoryId(categoryDTO.getCategoryId());
if (todoList.isEmpty()) {
Expand All @@ -125,11 +159,17 @@ public void deleteTodo(Long todoId) {
}

public void deleteTodoList(Long todoListId) {
todoRepository.deleteAllByTodoListId(todoListId);
todoListRepository.deleteById(todoListId);
}

// 회원 탈퇴시 모든 투두 삭제 메소드
public void deleteAllTodo(String userId) {
todoListRepository.deleteAllByUserId(userId);
}

public void checkEmptyTodoList(Long todoListId) {
List<Todo> todos = todoRepository.findAllByTodoListId(todoListId);
if (todos.isEmpty()) todoListRepository.deleteById(todoListId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,10 @@ private Todo(String content, Boolean checked,
this.todoListId = todoListId;
}

public void updateTodo(UpdateTodoReq updateTodoReq) {
public void updateTodo(UpdateTodoReq updateTodoReq, Long todoListId) {
this.content = updateTodoReq.getContent();
this.checked = updateTodoReq.getChecked();
this.todoListId = todoListId;
}

public void updateCategory(CategoryRes categoryDTO) {
Expand Down

0 comments on commit e46b70b

Please sign in to comment.