diff options
Diffstat (limited to 'src/main/java/com/encora/ToDosRepository.java')
-rw-r--r-- | src/main/java/com/encora/ToDosRepository.java | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/src/main/java/com/encora/ToDosRepository.java b/src/main/java/com/encora/ToDosRepository.java index e7e939e..ff9d9a6 100644 --- a/src/main/java/com/encora/ToDosRepository.java +++ b/src/main/java/com/encora/ToDosRepository.java @@ -13,6 +13,7 @@ import org.springframework.data.repository.query.FluentQuery; import java.util.*; import java.util.function.Function; +import java.util.stream.Collectors; public class ToDosRepository implements JpaRepository<ToDos, Integer> { Integer lastId; @@ -107,6 +108,42 @@ public class ToDosRepository implements JpaRepository<ToDos, Integer> { } } + // Get to dos list filtered. + public List<ToDos> findAllWithFilter(String name, String priority, String done) throws Exception { + List<ToDos> filtered = new ArrayList<>(this.todos); + + if (name != null && !name.equals("")) { + filtered = filtered.stream() + .filter(todo -> todo.getText().contains(name)) + .collect(Collectors.toList()); + } + if (priority != null && !priority.equalsIgnoreCase("all")) { + filtered = filtered.stream() + .filter(todo -> Objects.equals(String.valueOf(todo.getPriority()), priority)) + .collect(Collectors.toList()); + } + if (done != null && !done.equalsIgnoreCase("all")) { + switch (done) { + case "Done": + filtered = filtered.stream() + .filter(ToDos::isDone) + .collect(Collectors.toList()); + break; + + case "Undone": + filtered = filtered.stream() + .filter(todo -> !todo.isDone()) + .collect(Collectors.toList()); + break; + + default: + throw new Exception("Filtering not supported on 'done'."); + } + } + + return filtered; + } + private Comparator<ToDos> getToDoComparator(Sort sort) throws Exception { // Personal function. Creates a `Comparator` based on the `sort` // parameter. This is for us to successfully sort our List without the |