diff --git a/backend/.idea/workspace.xml b/backend/.idea/workspace.xml
index 3e12b67..fc7a90a 100644
--- a/backend/.idea/workspace.xml
+++ b/backend/.idea/workspace.xml
@@ -4,9 +4,8 @@
-
+
-
@@ -86,22 +85,7 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
@@ -117,7 +101,7 @@
-
+
@@ -127,7 +111,7 @@
-
+
@@ -149,17 +133,34 @@
-
+
-
+
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
@@ -173,11 +174,11 @@
-
-
+
+
+
-
@@ -457,16 +458,37 @@
1699693472182
-
+
+
+ 1699694400508
+
+
+
+ 1699694400508
+
+
+
+ 1699694552466
+
+
+
+ 1699694552466
+
+
+
+ 1699695051881
+
+
+
+ 1699695051881
+
+
-
-
-
@@ -489,7 +511,10 @@
-
+
+
+
+
diff --git a/backend/src/main/java/core/services/TaskService.java b/backend/src/main/java/core/services/TaskService.java
index 278a69f..eaa1f24 100644
--- a/backend/src/main/java/core/services/TaskService.java
+++ b/backend/src/main/java/core/services/TaskService.java
@@ -11,9 +11,6 @@ import core.repositories.timemanager.TaskgroupRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
-import javax.persistence.EntityManager;
-import javax.persistence.PersistenceContext;
-import javax.transaction.Transactional;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.*;
@@ -24,8 +21,6 @@ public class TaskService {
private final TaskRepository taskRepository;
private final TaskScheduleService taskScheduleService;
private final TaskgroupRepository taskgroupRepository;
- @PersistenceContext
- private EntityManager entityManager;
public TaskService(@Autowired TaskRepository taskRepository,
@@ -79,6 +74,12 @@ public class TaskService {
}
}
+ //Check for invalid date (deadline before start
+ if(taskFieldInfo.getStartDate() != null && taskFieldInfo.getDeadline() != null &&
+ taskFieldInfo.getDeadline().isBefore(taskFieldInfo.getStartDate())) {
+ return new ServiceResult<>(ServiceExitCode.INVALID_PARAMETER);
+ }
+
//Check for invalid date (deadline before start
if(taskFieldInfo.getStartDate() != null && taskFieldInfo.getDeadline() != null &&
taskFieldInfo.getDeadline().isBefore(taskFieldInfo.getStartDate())) {
@@ -91,13 +92,9 @@ public class TaskService {
return new ServiceResult<>(task);
}
- @Transactional
public void deleteTask(Task task) {
taskScheduleService.deleteScheduleByTask(task);
taskRepository.deleteByTaskID(task.getTaskID());
-
- entityManager.flush();
- entityManager.detach(task);
}
public void clearTasks(Taskgroup taskgroup) {
diff --git a/backend/src/test/java/core/tasks/TaskServiceTest.java b/backend/src/test/java/core/tasks/TaskServiceTest.java
index 1cd88a9..0d30d8c 100644
--- a/backend/src/test/java/core/tasks/TaskServiceTest.java
+++ b/backend/src/test/java/core/tasks/TaskServiceTest.java
@@ -1,6 +1,7 @@
package core.tasks;
import core.api.models.timemanager.tasks.TaskFieldInfo;
+import core.api.models.timemanager.tasks.TaskScope;
import core.entities.timemanager.Task;
import core.entities.timemanager.Taskgroup;
import core.repositories.timemanager.TaskRepository;
@@ -17,6 +18,7 @@ import javax.persistence.EntityManager;
import javax.transaction.Transactional;
import java.time.LocalDate;
+import java.util.List;
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
import static org.junit.jupiter.api.Assertions.*;
@@ -184,8 +186,93 @@ public class TaskServiceTest {
})
void deleteTask() {
for(long i=1; i<=15; i++) {
- taskRepository.deleteByTaskID(entityManager.find(Task.class, i).getTaskID());
+ taskService.deleteTask(entityManager.find(Task.class, i));
assertThat(entityManager.find(Task.class, i)).isNull();
}
}
+
+ @Test
+ @SqlGroup({
+ @Sql("classpath:taskgroupRepositoryTestEntries.sql"),
+ @Sql("classpath:taskRepositoryEntries.sql")
+ })
+ void clearTasks() {
+ //Situation 1: Delete from taskgroup with no tasks
+ taskService.clearTasks(entityManager.find(Taskgroup.class, 1L));
+ for(long i=1; i<=15; i++) {
+ assertThat(entityManager.find(Task.class, i)).isNotNull();
+ }
+ //Situation 2: Delete from taskgroup with tasks
+ taskService.clearTasks(entityManager.find(Taskgroup.class, 2L));
+ for(long i=1; i<=15; i++) {
+ assertThat(entityManager.find(Task.class, i)).isNull();
+ }
+ }
+
+ @Test
+ @SqlGroup({
+ @Sql("classpath:taskgroupRepositoryTestEntries.sql"),
+ @Sql("classpath:taskRepositoryEntries.sql")
+ })
+ void finishTask() {
+ taskService.finishTask(entityManager.find(Task.class, 1L));
+ assertTrue(entityManager.find(Task.class, 1L).isFinished());
+
+ taskService.finishTask(entityManager.find(Task.class, 2L));
+ assertTrue(entityManager.find(Task.class, 1L).isFinished());
+ }
+
+ @Test
+ @SqlGroup({
+ @Sql("classpath:taskgroupRepositoryTestEntries.sql"),
+ @Sql("classpath:taskRepositoryEntries.sql")
+ })
+ void loadAllTasks() {
+ //Situation 1: No tasks existing
+ List result_1 = taskService.loadAllTasks(username2, TaskScope.UNFINISHED);
+ assertEquals(0, result_1.size());
+
+ //Situation 2: Tasks existing, Unfinished#
+ List result_2 = taskService.loadAllTasks(username, TaskScope.UNFINISHED);
+ assertEquals(8, result_2.size());
+ assertTrue(result_2.contains(entityManager.find(Task.class, 1L)));
+ assertTrue(result_2.contains(entityManager.find(Task.class, 3L)));
+ assertTrue(result_2.contains(entityManager.find(Task.class, 5L)));
+ assertTrue(result_2.contains(entityManager.find(Task.class, 7L)));
+ assertTrue(result_2.contains(entityManager.find(Task.class, 9L)));
+ assertTrue(result_2.contains(entityManager.find(Task.class, 11L)));
+ assertTrue(result_2.contains(entityManager.find(Task.class, 12L)));
+
+ //Situation 3: Finished tasks
+ List result_3 = taskService.loadAllTasks(username, TaskScope.FINISHED);
+ assertEquals(7, result_3.size());
+ assertTrue(result_3.contains(entityManager.find(Task.class, 2L)));
+ assertTrue(result_3.contains(entityManager.find(Task.class, 4L)));
+ assertTrue(result_3.contains(entityManager.find(Task.class, 6L)));
+ assertTrue(result_3.contains(entityManager.find(Task.class, 8L)));
+ assertTrue(result_3.contains(entityManager.find(Task.class, 10L)));
+ assertTrue(result_3.contains(entityManager.find(Task.class, 13L)));
+ assertTrue(result_3.contains(entityManager.find(Task.class, 14L)));
+
+ //overdue
+ List result_4 = taskService.loadAllTasks(username, TaskScope.OVERDUE);
+ assertEquals(2, result_4.size());
+ assertTrue(result_4.contains(entityManager.find(Task.class, 3L)));
+ assertTrue(result_4.contains(entityManager.find(Task.class, 5L)));
+
+ //upcoming
+ List result_5 = taskService.loadAllTasks(username, TaskScope.UPCOMING);
+ assertEquals(2, result_5.size());
+ assertTrue(result_5.contains(entityManager.find(Task.class, 7L)));
+ assertTrue(result_5.contains(entityManager.find(Task.class, 9L)));
+ //Active
+ List result_6 = taskService.loadAllTasks(username, TaskScope.ACTIVE);
+ assertEquals(6, result_6.size());
+ assertTrue(result_6.contains(entityManager.find(Task.class, 1L)));
+ assertTrue(result_6.contains(entityManager.find(Task.class, 3L)));
+ assertTrue(result_6.contains(entityManager.find(Task.class, 5L)));
+ assertTrue(result_6.contains(entityManager.find(Task.class, 11L)));
+ assertTrue(result_6.contains(entityManager.find(Task.class, 12L)));
+
+ }
}