Skip to content

Commit

Permalink
refactor: TodoService클래스 리팩토링
Browse files Browse the repository at this point in the history
  • Loading branch information
jucheolkang committed Jan 14, 2024
1 parent 9053905 commit 618bd23
Showing 1 changed file with 9 additions and 15 deletions.
24 changes: 9 additions & 15 deletions src/main/java/org/idiot/yesslave/todo/application/TodoService.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,56 +11,50 @@

import javax.transaction.Transactional;
import java.time.LocalDateTime;
import java.util.Optional;

@Service
@RequiredArgsConstructor
@Transactional
public class TodoService {
private final TodoRepository todoRepository ;

// 저장
public void save(SaveDto saveDto) {
todoRepository.save(todo.builder()
.todo(saveDto.getTodo())
.registerDate(time())
.build());
}

// 수정
public void update(Long id, UpdateDto updateDto) {
todo todo = existId(id);
deleteStatus(todo);
checkDeleteStatus(todo);
todo.update(updateDto.getTodo(), time());
}

// 체크박스 true,false
public void changeCheck(Long id) {
todo todo = existId(id);
deleteStatus(todo);
checkDeleteStatus(todo);
todo.changeCheck(!todo.isTodoCheck());
}

// 논리적 삭제
public void delete(Long id) {
todo todo = existId(id);
deleteStatus(todo);
checkDeleteStatus(todo);
todo.delete(true);
}

// 삭제 상태 확인
private static void deleteStatus(todo todo) {
if (todo.isDelete()) throw new TodoIdHandler(ErrorCode.ID_DELETE);
private void checkDeleteStatus(todo todo) {
if (todo.isDelete()) {
throw new TodoIdHandler(ErrorCode.ID_DELETE);
}
}

//id 존재 확인
public todo existId(Long id) {
Optional<todo> op = todoRepository.findById(id);
todo todo = op.orElseThrow(() -> new TodoIdHandler(ErrorCode.ID_NOT_FOUND));
todo todo = todoRepository.findById(id)
.orElseThrow(() -> new TodoIdHandler(ErrorCode.ID_NOT_FOUND));
return todo;
}

//시간 갱신
private LocalDateTime time() {
LocalDateTime time = LocalDateTime.now();
return time;
Expand Down

0 comments on commit 618bd23

Please sign in to comment.