반응형
레파지터리 구현
- TodoRepository.java 생성
import org.example.model.TodoEntity;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface TodoRepository extends JpaRepository<TodoEntity, Long> {
}
- TodoServerApplication.java 생성
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class TodoServerApplication {
public static void main(String[] args) {
SpringApplication.run(TodoServerApplication.class, args);
}
}
서비스 구현
- TodoService.java
import lombok.AllArgsConstructor;
import org.example.Repository.TodoRepository;
import org.example.model.TodoEntity;
import org.example.model.TodoRequest;
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;
//todo 리스트 목록에 아이템 추가
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 TodoEntity searchById(Long id){
return this.todoRepository.findById(id).orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND));
}
//전체목록 조회
public List<TodoEntity> searchAll(){
return this.todoRepository.findAll();
}
//특정 아이템 수정
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();
}
}
반응형
'JAVA' 카테고리의 다른 글
[JAVA] Spring Boot 프로젝트 생성 & DevTools 적용 (0) | 2022.08.02 |
---|---|
[JAVA] 인텔리제이 To - do List_컨트롤러 구현 (0) | 2022.08.01 |
[JAVA] 인텔리제이 To - do List_프로젝트셋팅 및 모델구현 (0) | 2022.08.01 |
[JAVA] 인텔리제이 상황별 단축키 (0) | 2022.07.28 |
[JAVA] 인텔리제이 단축키 정리 (0) | 2022.07.27 |
댓글