service 패키지에 TodoService 클래스 파일을 생성한다.
다음의 6가지 기능을 구현하기 위해 서비스 코드에 메서드 시그니처들을 정의한다.
package org.example.service;
import lombok.AllArgsConstructor;
import org.example.model.TodoEntity;
import org.example.model.TodoRequest;
import org.example.repository.TodoRepository;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.web.server.ResponseStatusException;
import java.util.List;
@Service
@AllArgsConstructor
public class TodoService {
private final TodoRepository todoRepository;
public TodoEntity add(TodoRequest request) {
TodoEntity todoEntity = new TodoEntity();
todoEntity.setTitle(request.getTitle());
todoEntity.setOrder(request.getOrder());
todoEntity.setCompleted(request.getCompleted());
return this.todoRepository.save(todoEntity);
}
public List<TodoEntity> searchAll() {
return this.todoRepository.findAll();
}
public TodoEntity searchById(Long id) {
return this.todoRepository.findById(id)
.orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND));
}
public TodoEntity updateById(Long id, TodoRequest request) {
TodoEntity todoEntity = this.searchById(id);
if (request.getTitle() != null) {
todoEntity.setTitle(request.getTitle());
}
if (request.getOrder() != null) {
todoEntity.setOrder(request.getOrder());
}
if (request.getCompleted() != null) {
todoEntity.setCompleted(request.getCompleted());
}
return this.todoRepository.save(todoEntity);
}
public void deleteById(Long id) {
this.todoRepository.deleteById(id);
}
public void deleteAll() {
this.todoRepository.deleteAll();
}
}