diff --git a/src/main/java/com/example/demo/controller/ToDoController.java b/src/main/java/com/example/demo/controller/ToDoController.java index a1c576d..0ddee53 100644 --- a/src/main/java/com/example/demo/controller/ToDoController.java +++ b/src/main/java/com/example/demo/controller/ToDoController.java @@ -27,7 +27,13 @@ public String handleException(Exception ex) { @GetMapping("/todos") @Valid - public List getAll() { + public List getAll(@RequestParam(required = false) Boolean isCompleted) { + if (isCompleted != null) { + if (isCompleted) { + return toDoService.getAllCompleted(); + } + return toDoService.getAllInProgress(); + } return toDoService.getAll(); } diff --git a/src/main/java/com/example/demo/service/ToDoService.java b/src/main/java/com/example/demo/service/ToDoService.java index 57df487..f36c6a0 100644 --- a/src/main/java/com/example/demo/service/ToDoService.java +++ b/src/main/java/com/example/demo/service/ToDoService.java @@ -1,67 +1,82 @@ package com.example.demo.service; -import java.util.List; -import java.util.stream.Collectors; - -import org.springframework.stereotype.Service; - import com.example.demo.dto.ToDoResponse; import com.example.demo.dto.ToDoSaveRequest; import com.example.demo.dto.mapper.ToDoEntityToResponseMapper; import com.example.demo.exception.ToDoNotFoundException; import com.example.demo.model.ToDoEntity; import com.example.demo.repository.ToDoRepository; +import org.springframework.stereotype.Service; + +import java.util.List; +import java.util.stream.Collectors; + +import static com.example.demo.repository.specification.ToDoSpecifications.isCompleted; +import static com.example.demo.repository.specification.ToDoSpecifications.isInProgress; +import static org.springframework.data.jpa.domain.Specification.where; @Service public class ToDoService { - - private final ToDoRepository toDoRepository; - public ToDoService(ToDoRepository toDoRepository) { - this.toDoRepository = toDoRepository; - } - - public List getAll() { - return toDoRepository.findAll().stream() - .map(ToDoEntityToResponseMapper::map) - .collect(Collectors.toList()); - } + private final ToDoRepository toDoRepository; + + public ToDoService(ToDoRepository toDoRepository) { + this.toDoRepository = toDoRepository; + } + + public List getAll() { + return toDoRepository.findAll().stream() + .map(ToDoEntityToResponseMapper::map) + .collect(Collectors.toList()); + } + + public List getAllCompleted() { + return toDoRepository.findAll(where(isCompleted())).stream() + .map(ToDoEntityToResponseMapper::map) + .collect(Collectors.toList()); + } + + public List getAllInProgress() { + return toDoRepository.findAll(where(isInProgress())).stream() + .map(ToDoEntityToResponseMapper::map) + .collect(Collectors.toList()); + } - public ToDoResponse upsert(ToDoSaveRequest toDoDTO) throws ToDoNotFoundException { - ToDoEntity todo; - //update if it has id or create if it hasn't - if (toDoDTO.id == null) { - todo = new ToDoEntity(toDoDTO.text); - } else { - todo = toDoRepository.findById(toDoDTO.id).orElseThrow(() -> new ToDoNotFoundException(toDoDTO.id)); - todo.setText(toDoDTO.text); - } - return ToDoEntityToResponseMapper.map(toDoRepository.save(todo)); - } + public ToDoResponse upsert(ToDoSaveRequest toDoDTO) throws ToDoNotFoundException { + ToDoEntity todo; + //update if it has id or create if it hasn't + if (toDoDTO.id == null) { + todo = new ToDoEntity(toDoDTO.text); + } else { + todo = toDoRepository.findById(toDoDTO.id).orElseThrow(() -> new ToDoNotFoundException(toDoDTO.id)); + todo.setText(toDoDTO.text); + } + return ToDoEntityToResponseMapper.map(toDoRepository.save(todo)); + } - public ToDoResponse completeToDo(Long id) throws ToDoNotFoundException { - ToDoEntity todo = toDoRepository.findById(id).orElseThrow(() -> new ToDoNotFoundException(id)); - todo.completeNow(); - return ToDoEntityToResponseMapper.map(toDoRepository.save(todo)); - } + public ToDoResponse completeToDo(Long id) throws ToDoNotFoundException { + ToDoEntity todo = toDoRepository.findById(id).orElseThrow(() -> new ToDoNotFoundException(id)); + todo.completeNow(); + return ToDoEntityToResponseMapper.map(toDoRepository.save(todo)); + } - public ToDoResponse cancelToDo(Long id) throws ToDoNotFoundException { - ToDoEntity todo = toDoRepository.findById(id).orElseThrow(() -> new ToDoNotFoundException(id)); - todo.cancelNow(); - return ToDoEntityToResponseMapper.map(toDoRepository.save(todo)); - } + public ToDoResponse cancelToDo(Long id) throws ToDoNotFoundException { + ToDoEntity todo = toDoRepository.findById(id).orElseThrow(() -> new ToDoNotFoundException(id)); + todo.cancelNow(); + return ToDoEntityToResponseMapper.map(toDoRepository.save(todo)); + } - public ToDoResponse getOne(Long id) throws ToDoNotFoundException { - return ToDoEntityToResponseMapper.map( - toDoRepository.findById(id).orElseThrow(() -> new ToDoNotFoundException(id)) - ); - } + public ToDoResponse getOne(Long id) throws ToDoNotFoundException { + return ToDoEntityToResponseMapper.map( + toDoRepository.findById(id).orElseThrow(() -> new ToDoNotFoundException(id)) + ); + } - public void deleteOne(Long id) { - toDoRepository.deleteById(id); - } + public void deleteOne(Long id) { + toDoRepository.deleteById(id); + } - public void deleteAll() { - toDoRepository.deleteAll(); - } + public void deleteAll() { + toDoRepository.deleteAll(); + } }