aboutsummaryrefslogtreecommitdiff
path: root/src/main/java
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java')
-rw-r--r--src/main/java/com/encora/Main.java21
-rw-r--r--src/main/java/com/encora/ToDosRepository.java59
2 files changed, 71 insertions, 9 deletions
diff --git a/src/main/java/com/encora/Main.java b/src/main/java/com/encora/Main.java
index 63c6a48..a233550 100644
--- a/src/main/java/com/encora/Main.java
+++ b/src/main/java/com/encora/Main.java
@@ -2,11 +2,13 @@ package com.encora;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.data.domain.Sort;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import java.util.Date;
import java.util.List;
+import java.util.Objects;
@SpringBootApplication
@RestController
@@ -100,4 +102,23 @@ public class Main {
selectedToDo.setDoneDate(null);
toDosRepository.save(selectedToDo);
}
+
+
+ // Getting sorted to dos.
+ enum SortingsFields {
+ Id, Priority, DueDate
+ }
+ enum SortingOrders {
+ ASC, DESC
+ }
+ @GetMapping("/todos/{field}/{order}")
+ @ResponseStatus(value=HttpStatus.OK)
+ public List<ToDos> getFilteredToDos(@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();
+ }
+
+ return toDosRepository.findAll(sortingMethod);
+ }
}
diff --git a/src/main/java/com/encora/ToDosRepository.java b/src/main/java/com/encora/ToDosRepository.java
index f19d1ce..65c1b39 100644
--- a/src/main/java/com/encora/ToDosRepository.java
+++ b/src/main/java/com/encora/ToDosRepository.java
@@ -11,10 +11,7 @@ import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.repository.query.FluentQuery;
-import java.util.ArrayList;
-import java.util.List;
-import java.util.Objects;
-import java.util.Optional;
+import java.util.*;
import java.util.function.Function;
public class ToDosRepository implements JpaRepository<ToDos, Integer> {
@@ -39,6 +36,21 @@ public class ToDosRepository implements JpaRepository<ToDos, Integer> {
return this.todos;
}
+ // Return all to dos and sorted.
+ @Override
+ public List<ToDos> findAll(Sort sort) {
+ List<ToDos> sortedList = new ArrayList<>(this.todos);
+
+ try {
+ Comparator<ToDos> comparator = this.getToDoComparator(sort);
+ Collections.sort(sortedList, comparator);
+ } catch (Exception e) {
+ throw new RuntimeException(e);
+ }
+
+ return sortedList;
+ }
+
// Save new element.
@Override
public <S extends ToDos> S save(S entity) {
@@ -81,6 +93,40 @@ public class ToDosRepository implements JpaRepository<ToDos, Integer> {
return null;
}
+ 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
+ // need of a database.
+ String sortString = sort.toString(); // <- '{field}: {order}'
+ String[] sortCriteria = sortString.split(": ");
+
+ String field = sortCriteria[0];
+ String order = sortCriteria[1];
+
+ Comparator<ToDos> comparator = null;
+ switch (field) {
+ case "Id":
+ comparator = Comparator.comparing(ToDos::getId);
+ break;
+
+ case "Priority":
+ comparator = Comparator.comparing(ToDos::getPriority);
+ break;
+
+ case "DueDate":
+ comparator = Comparator.comparing(ToDos::getDueDate);
+ break;
+
+ default:
+ throw new Exception("Field sorting not implemented.");
+ }
+
+ if (order.equalsIgnoreCase("desc")) {
+ comparator = comparator.reversed();
+ }
+ return comparator;
+ }
+
/*
* N O T Y E T D E F I N E D .
*/
@@ -210,11 +256,6 @@ public class ToDosRepository implements JpaRepository<ToDos, Integer> {
}
@Override
- public List<ToDos> findAll(Sort sort) {
- return null;
- }
-
- @Override
public Page<ToDos> findAll(Pageable pageable) {
return null;
}