aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/com/encora/Main.java
diff options
context:
space:
mode:
authorAdrián Oliva <adrian.oliva@cimat.mx>2023-05-25 21:35:01 -0600
committerAdrián Oliva <adrian.oliva@cimat.mx>2023-05-25 21:35:01 -0600
commitad37e97ef75cbe8f48a98d71874d4632dc157d59 (patch)
tree7f0e648bbf684da3e09057e8bf1829a3e1ef7d76 /src/main/java/com/encora/Main.java
parentb51e47b7abfb8340ab4ee67a2fc95cb07fcbfbd1 (diff)
downloadToDo-App-BE-ad37e97ef75cbe8f48a98d71874d4632dc157d59.tar.gz
ToDo-App-BE-ad37e97ef75cbe8f48a98d71874d4632dc157d59.zip
New GET for sorted to dos.
GET request with path "/todos/{field}/{order}", where {field} can be "Id", "Priority" or "DueDate" for sorting the corresponding fields, and {order} can be "ASC" or "DESC" for ascending or descending order, respectively.
Diffstat (limited to 'src/main/java/com/encora/Main.java')
-rw-r--r--src/main/java/com/encora/Main.java21
1 files changed, 21 insertions, 0 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);
+ }
}