diff options
author | Adrián Oliva <adrian.oliva@cimat.mx> | 2023-05-26 13:34:05 -0600 |
---|---|---|
committer | Adrián Oliva <adrian.oliva@cimat.mx> | 2023-05-26 13:34:05 -0600 |
commit | 4d0e671eb82c4ff6941905744dd10b856e16e725 (patch) | |
tree | fea476973c7980bd8ddb7bce7d3e2735d4d9d29e /src/main/java/com/encora | |
parent | 0dd1b42bf1008ed5cbc0f0f0afaa143248e75dd2 (diff) | |
download | ToDo-App-BE-4d0e671eb82c4ff6941905744dd10b856e16e725.tar.gz ToDo-App-BE-4d0e671eb82c4ff6941905744dd10b856e16e725.zip |
Added a GET request with filters.
Path is "/todos/filter". You have to pass as a body the arguments "name"
for searching inside the to do's name, "priority" for specifying a
priority level (or "All") and/or "done" for specifying if the to do is
"Done" or "Undone" (or "All").
Diffstat (limited to 'src/main/java/com/encora')
-rw-r--r-- | src/main/java/com/encora/Main.java | 17 | ||||
-rw-r--r-- | src/main/java/com/encora/ToDosRepository.java | 37 |
2 files changed, 53 insertions, 1 deletions
diff --git a/src/main/java/com/encora/Main.java b/src/main/java/com/encora/Main.java index dbd3364..ac2a609 100644 --- a/src/main/java/com/encora/Main.java +++ b/src/main/java/com/encora/Main.java @@ -124,7 +124,7 @@ public class Main { } @GetMapping("/todos/{field}/{order}") @ResponseStatus(value=HttpStatus.OK) - public List<ToDos> getFilteredToDos(@PathVariable("field") SortingsFields field, @PathVariable("order") SortingOrders order) { + public List<ToDos> getSortedToDos(@PathVariable("field") SortingsFields field, @PathVariable("order") SortingOrders order) { Sort sortingMethod = Sort.by(String.valueOf(field)); if (Objects.equals(String.valueOf(order), "DESC")) { sortingMethod = sortingMethod.descending(); @@ -132,4 +132,19 @@ public class Main { return toDosRepository.findAll(sortingMethod); } + + + // Getting filtered to dos. + record toDoFilters ( + String name, + String priority, + String done + ) { + + } + @GetMapping("/todos/filter") + @ResponseStatus(value=HttpStatus.OK) + public List<ToDos> getFilteredToDos(@RequestBody toDoFilters filters) throws Exception { + return toDosRepository.findAllWithFilter(filters.name(), filters.priority(), filters.done()); + } } 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 |