diff --git a/.idea/.gitignore b/.idea/.gitignore
new file mode 100644
index 0000000..26d3352
--- /dev/null
+++ b/.idea/.gitignore
@@ -0,0 +1,3 @@
+# Default ignored files
+/shelf/
+/workspace.xml
diff --git a/.idea/To-Do-Back-End.iml b/.idea/To-Do-Back-End.iml
new file mode 100644
index 0000000..d6ebd48
--- /dev/null
+++ b/.idea/To-Do-Back-End.iml
@@ -0,0 +1,9 @@
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/compiler.xml b/.idea/compiler.xml
new file mode 100644
index 0000000..149eea4
--- /dev/null
+++ b/.idea/compiler.xml
@@ -0,0 +1,18 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/encodings.xml b/.idea/encodings.xml
new file mode 100644
index 0000000..bafb1e8
--- /dev/null
+++ b/.idea/encodings.xml
@@ -0,0 +1,6 @@
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/jarRepositories.xml b/.idea/jarRepositories.xml
new file mode 100644
index 0000000..712ab9d
--- /dev/null
+++ b/.idea/jarRepositories.xml
@@ -0,0 +1,20 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/misc.xml b/.idea/misc.xml
new file mode 100644
index 0000000..850bf4e
--- /dev/null
+++ b/.idea/misc.xml
@@ -0,0 +1,14 @@
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/modules.xml b/.idea/modules.xml
new file mode 100644
index 0000000..cb441c1
--- /dev/null
+++ b/.idea/modules.xml
@@ -0,0 +1,8 @@
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/vcs.xml b/.idea/vcs.xml
new file mode 100644
index 0000000..35eb1dd
--- /dev/null
+++ b/.idea/vcs.xml
@@ -0,0 +1,6 @@
+
+
+
+
+
+
\ No newline at end of file
diff --git a/todo-app-encora/src/main/java/com/assigment/todoapp/repository/ToDoRepository.java b/todo-app-encora/src/main/java/com/assigment/todoapp/repository/ToDoRepository.java
index 571821f..faa9ee4 100644
--- a/todo-app-encora/src/main/java/com/assigment/todoapp/repository/ToDoRepository.java
+++ b/todo-app-encora/src/main/java/com/assigment/todoapp/repository/ToDoRepository.java
@@ -1,48 +1,16 @@
package com.assigment.todoapp.repository;
import java.util.List;
-import java.util.stream.Collectors;
-
-import org.springframework.stereotype.Repository;
+import java.util.UUID;
import com.assigment.todoapp.domain.ToDoItem;
-
-
-/*
- @Repository
-public interface ToDoRepository extends JpaRepository to create the interface and use de Database
-*/
-
-@Repository
-public class ToDoRepository { // interface
-
-
- private List todoItems;
-
- public ToDoRepository(List todoItems) {
- this.todoItems = todoItems;
- }
-
- public List fetchAllToDoItems () {
- return todoItems;
- }
-
- public List findByDone(boolean aux) {
- return todoItems.stream()
- .filter(item -> item.isDone() == aux)
- .collect(Collectors.toList());
- }
-
- public List findByNameContainingIgnoreCase(String name) {
- return todoItems.stream()
- .filter(item -> item.getName().toLowerCase().contains(name.toLowerCase()))
- .collect(Collectors.toList());
- }
-
- public List findByPriority(String priority) {
- return todoItems.stream()
- .filter(item -> item.getPriority().equalsIgnoreCase(priority))
- .collect(Collectors.toList());
- }
-}
+public interface ToDoRepository {
+ List fetchAllToDoItems();
+ List findByDone(boolean done);
+ List findByNameContainingIgnoreCase(String name);
+ List findByPriority(String priority);
+ ToDoItem findById(UUID id);
+ void save(ToDoItem todoItem);
+ void deleteById(UUID id);
+}
\ No newline at end of file
diff --git a/todo-app-encora/src/main/java/com/assigment/todoapp/repository/ToDoRepositoryImpl.java b/todo-app-encora/src/main/java/com/assigment/todoapp/repository/ToDoRepositoryImpl.java
new file mode 100644
index 0000000..275ba05
--- /dev/null
+++ b/todo-app-encora/src/main/java/com/assigment/todoapp/repository/ToDoRepositoryImpl.java
@@ -0,0 +1,63 @@
+package com.assigment.todoapp.repository;
+
+import java.util.List;
+import java.util.UUID;
+import java.util.stream.Collectors;
+
+import org.springframework.stereotype.Repository;
+
+import com.assigment.todoapp.domain.ToDoItem;
+
+@Repository
+public class ToDoRepositoryImpl implements ToDoRepository {
+
+ private final List todoItems;
+
+ public ToDoRepositoryImpl(List todoItems) {
+ this.todoItems = todoItems;
+ }
+
+ @Override
+ public List fetchAllToDoItems() {
+ return todoItems;
+ }
+
+ @Override
+ public List findByDone(boolean done) {
+ return todoItems.stream()
+ .filter(item -> item.isDone() == done)
+ .collect(Collectors.toList());
+ }
+
+ @Override
+ public List findByNameContainingIgnoreCase(String name) {
+ return todoItems.stream()
+ .filter(item -> item.getName().toLowerCase().contains(name.toLowerCase()))
+ .collect(Collectors.toList());
+ }
+
+ @Override
+ public List findByPriority(String priority) {
+ return todoItems.stream()
+ .filter(item -> item.getPriority().equalsIgnoreCase(priority))
+ .collect(Collectors.toList());
+ }
+
+ @Override
+ public ToDoItem findById(UUID id) {
+ return todoItems.stream()
+ .filter(item -> item.getId().equals(id))
+ .findFirst()
+ .orElseThrow(() -> new RuntimeException("ToDoItem not found"));
+ }
+
+ @Override
+ public void save(ToDoItem todoItem) {
+ todoItems.add(todoItem);
+ }
+
+ @Override
+ public void deleteById(UUID id) {
+ todoItems.removeIf(item -> item.getId().equals(id));
+ }
+}
\ No newline at end of file
diff --git a/todo-app-encora/src/main/java/com/assigment/todoapp/service/ToDoService.java b/todo-app-encora/src/main/java/com/assigment/todoapp/service/ToDoService.java
index 1329297..c3696ee 100644
--- a/todo-app-encora/src/main/java/com/assigment/todoapp/service/ToDoService.java
+++ b/todo-app-encora/src/main/java/com/assigment/todoapp/service/ToDoService.java
@@ -1,14 +1,10 @@
package com.assigment.todoapp.service;
+import java.time.Duration;
+import java.time.LocalDate;
import java.time.LocalDateTime;
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.Comparator;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-import java.util.Optional;
-import java.util.UUID;
+import java.time.temporal.ChronoUnit;
+import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
@@ -20,214 +16,177 @@
@Service
public class ToDoService {
-
- @Autowired //Inject
- private ToDoRepository todoRepository;
- private List todoItems = new ArrayList<>();
- public boolean aux;
-
+
+ private final ToDoRepository todoRepository;
+
+ @Autowired
public ToDoService(ToDoRepository todoRepository) {
- this.todoItems = todoRepository.fetchAllToDoItems();
- }
-
- public void setTodoItems(List todoItems) {
- this.todoItems = todoItems;
- }
-
- public List fetchAllToDoItems (String state, String name, String priority) {
-
- Stream stream = todoRepository.fetchAllToDoItems().stream();
-
-
- if (!state.equals("All")) {
-
- if(state.equals("Done")) {
- aux = true;
- }else {
- aux = false;
- }
- stream = stream.filter(item -> item.isDone() == aux);
- }
-
-
- if (!priority.equals("All")) {
- stream = stream.filter(item -> item.getPriority().equals(priority));
- }
-
-
- if (!name.equals("")) {
- stream = stream.filter(item -> item.getName().contains(name));
- }
-
- return stream.collect(Collectors.toList());
+ this.todoRepository = todoRepository;
}
-
+ public List fetchAllToDoItems(String state, String name, String priority) {
+ Stream stream = todoRepository.fetchAllToDoItems().stream();
+
+ if (!"All".equals(state)) {
+ boolean isDone = "Done".equals(state);
+ stream = stream.filter(item -> item.isDone() == isDone);
+ }
+
+ if (!"All".equals(priority)) {
+ stream = stream.filter(item -> item.getPriority().equals(priority));
+ }
- public Map paginateToDoItems(
- List todoItems,
- int page,
- int pageSize) {
+ if (!name.isEmpty()) {
+ stream = stream.filter(item -> item.getName().contains(name));
+ }
- int totalItems = todoItems.size();
- int totalPages = (int) Math.ceil((double) totalItems / pageSize);
- int startIndex = (page - 1) * pageSize;
- int endIndex = Math.min(startIndex + pageSize, totalItems);
+ return stream.collect(Collectors.toList());
+ }
+
+ public Map paginateToDoItems(List todoItems, int page, int pageSize) {
+ int totalItems = todoItems.size();
+ int totalPages = (int) Math.ceil((double) totalItems / pageSize);
+ int startIndex = (page - 1) * pageSize;
+ int endIndex = Math.min(startIndex + pageSize, totalItems);
- List paginatedItems = todoItems.subList(startIndex, endIndex);
- int itemsOnPage = paginatedItems.size();
+ List paginatedItems = todoItems.subList(startIndex, endIndex);
- Map response = new HashMap<>();
- response.put("items", paginatedItems);
- response.put("currentPage", page);
- response.put("totalItems", totalItems);
- response.put("totalPages", totalPages);
- response.put("itemsOnPage", itemsOnPage);
+ Map response = new HashMap<>();
+ response.put("items", paginatedItems);
+ response.put("currentPage", page);
+ response.put("totalItems", totalItems);
+ response.put("totalPages", totalPages);
+ response.put("itemsOnPage", paginatedItems.size());
+
+ return response;
+ }
- return response;
+ public List sortToDoItems(List todoItems, String sortBy1, String order1, String sortBy2, String order2) {
+ Comparator comparator = getComparator(sortBy1, order1, sortBy2, order2);
+ todoItems.sort(comparator);
+ return todoItems;
+ }
+
+ private Comparator getComparator(String sortBy1, String order1, String sortBy2, String order2) {
+ Map priorityValues = Map.of("Low", 1, "Medium", 2, "High", 3);
+
+ Comparator comparator1 = getSingleComparator(sortBy1, order1, priorityValues);
+ Comparator comparator2 = getSingleComparator(sortBy2, order2, priorityValues);
+
+ return comparator1.thenComparing(comparator2);
+ }
+
+ private Comparator getSingleComparator(String sortBy, String order, Map priorityValues) {
+ Comparator comparator;
+ if ("priority".equals(sortBy)) {
+ comparator = Comparator.comparing(item -> priorityValues.getOrDefault(item.getPriority(), 0));
+ } else if ("dueDate".equals(sortBy)) {
+ comparator = Comparator.comparing(ToDoItem::getDueDate, Comparator.nullsLast(Comparator.naturalOrder()));
+ } else {
+ throw new IllegalArgumentException("Invalid sortBy: " + sortBy);
+ }
+
+ if ("desc".equals(order)) {
+ comparator = comparator.reversed();
+ }
+
+ return comparator;
}
-
- public List sortToDoItems(
- List todoItems,
- String sortBy1,
- String order1,
- String sortBy2,
- String order2) {
-
- Collections.sort(todoItems, new Comparator() {
- @Override
- public int compare(ToDoItem t1, ToDoItem t2) {
- Map priorityValues = new HashMap<>();
- priorityValues.put("Low", 1);
- priorityValues.put("Medium", 2);
- priorityValues.put("High", 3);
-
- int comparison1 = 0;
- if (sortBy1.equals("priority")) {
- int priority1Value = priorityValues.getOrDefault(t1.getPriority(), 0);
- int priority2Value = priorityValues.getOrDefault(t2.getPriority(), 0);
- if (order1.equals("desc")) {
- priority1Value = 4 - priority1Value;
- priority2Value = 4 - priority2Value;
- }
- comparison1 = Integer.compare(priority1Value, priority2Value);
- } else if (sortBy1.equals("dueDate")) {
- if (t1.getDueDate() == null && t2.getDueDate() == null) {
- comparison1 = 0;
- } else if (t1.getDueDate() == null) {
- comparison1 = -1;
- } else if (t2.getDueDate() == null) {
- comparison1 = 1;
- } else {
- comparison1 = t1.getDueDate().compareTo(t2.getDueDate());
- }
- if (order1.equals("desc")) {
- comparison1 = -comparison1;
- }
- }
-
- if (comparison1 != 0) {
- return comparison1;
- } else {
- int comparison2 = 0;
- if (sortBy2.equals("priority")) {
- int priority1Value = priorityValues.getOrDefault(t1.getPriority(), 0);
- int priority2Value = priorityValues.getOrDefault(t2.getPriority(), 0);
- if (order2.equals("desc")) {
- priority1Value = 4 - priority1Value;
- priority2Value = 4 - priority2Value;
- }
- comparison2 = Integer.compare(priority1Value, priority2Value);
- } else if (sortBy2.equals("dueDate")) {
- if (t1.getDueDate() == null && t2.getDueDate() == null) {
- comparison2 = 0;
- } else if (t1.getDueDate() == null) {
- comparison2 = -1;
- } else if (t2.getDueDate() == null) {
- comparison2 = 1;
- } else {
- comparison2 = t1.getDueDate().compareTo(t2.getDueDate());
- }
- if (order2.equals("desc")) {
- comparison2 = -comparison2;
- }
- }
- return comparison2;
- }
- }
- });
-
- return todoItems;
- }
public ToDoItem createToDoItem(ToDoItem todoItem) {
- todoItem.setId(UUID.randomUUID());
- todoItem.setCreationDate(LocalDateTime.now());
- todoItem.setDone(false);
-
-
- todoItems.add(todoItem);
- return todoItem;
- }
-
+ todoItem.setId(UUID.randomUUID());
+ todoItem.setCreationDate(LocalDateTime.now());
+ todoItem.setDone(false);
+ todoRepository.save(todoItem);
+ return todoItem;
+ }
+
public ToDoItem updateFlag(UUID id, ToDoItem todoItem) {
- Optional existingToDoItem = todoItems.stream()
- .filter(item -> item.getId().equals(id))
- .findFirst();
-
- if (existingToDoItem.isEmpty()) {
- throw new RuntimeException("ToDoItem no encontrado");
- }
- ToDoItem existingItem = existingToDoItem.get();
-
- if(existingItem.isDone()== false) {
- existingItem.setDone(todoItem.isDone());
- existingItem.setDoneDate(LocalDateTime.now());
- return existingItem;
- }else {
- existingItem.setDone(todoItem.isDone());
- existingItem.setDoneDate(null);
- return existingItem;
- }
-
-
+ ToDoItem existingItem = todoRepository.findById(id);
+ existingItem.setDone(todoItem.isDone());
+ existingItem.setDoneDate(todoItem.isDone() ? LocalDateTime.now() : null);
+ todoRepository.save(existingItem);
+ return existingItem;
}
public ToDoItem updateToDoItem(UUID id, ToDoItem updatedToDoItem) {
- Optional existingToDoItem = todoItems.stream()
- .filter(item -> item.getId().equals(id))
- .findFirst();
-
- if (existingToDoItem.isEmpty()) {
- throw new RuntimeException("ToDoItem no encontrado");
- }
-
- ToDoItem existingItem = existingToDoItem.get();
- existingItem.setName(updatedToDoItem.getName());
- existingItem.setDone(updatedToDoItem.isDone());
- existingItem.setPriority(updatedToDoItem.getPriority());
- existingItem.setDueDate(updatedToDoItem.getDueDate());
- return existingItem;
+ ToDoItem existingItem = todoRepository.findById(id);
+ existingItem.setName(updatedToDoItem.getName());
+ existingItem.setDone(updatedToDoItem.isDone());
+ existingItem.setPriority(updatedToDoItem.getPriority());
+ existingItem.setDueDate(updatedToDoItem.getDueDate());
+ todoRepository.save(existingItem);
+ return existingItem;
}
public void deleteToDoItem(UUID id) {
- todoItems.removeIf(item -> item.getId().equals(id));
+ todoRepository.deleteById(id);
}
public List fetchAllDoneToDoItems(String priority) {
- List doneItems = todoItems.stream()
- .filter(item -> item.isDone() && (priority.equals("All") || item.getPriority().equals(priority)))
- .collect(Collectors.toList());
-
- return doneItems;
- }
+ return todoRepository.findByDone(true).stream()
+ .filter(item -> "All".equals(priority) || item.getPriority().equals(priority))
+ .collect(Collectors.toList());
+ }
public List fetchAllItems() {
- List doneItems = todoItems.stream().collect(Collectors.toList());
+ return todoRepository.fetchAllToDoItems();
+ }
+
+ public List