diff --git a/backend/.idea/workspace.xml b/backend/.idea/workspace.xml
index d97e3bf..ef6d92a 100644
--- a/backend/.idea/workspace.xml
+++ b/backend/.idea/workspace.xml
@@ -6,7 +6,11 @@
-
+
+
+
+
+
@@ -29,6 +33,9 @@
+
+
+
{
"customColor": "",
"associatedIndex": 6
@@ -85,7 +92,9 @@
-
+
+
+
diff --git a/backend/src/main/java/core/api/controller/TaskController.java b/backend/src/main/java/core/api/controller/TaskController.java
index ef850e7..f80135a 100644
--- a/backend/src/main/java/core/api/controller/TaskController.java
+++ b/backend/src/main/java/core/api/controller/TaskController.java
@@ -81,4 +81,19 @@ public class TaskController {
return ResponseEntity.status(409).body(new SimpleStatusResponse("failed"));
}
}
+
+ @DeleteMapping("/tasks/{taskID}")
+ public ResponseEntity> deleteTask(@PathVariable long taskID) {
+ PermissionResult taskPermissionResult = taskService.getTaskPermissions(taskID, SecurityContextHolder.getContext().getAuthentication().getName());
+ if (!taskPermissionResult.isHasPermissions()) {
+ return ResponseEntity.status(403).body(new SimpleStatusResponse("failed"));
+ }
+
+ if (taskPermissionResult.getExitCode() == ServiceExitCode.MISSING_ENTITY) {
+ return ResponseEntity.status(404).body(new SimpleStatusResponse("failed"));
+ }
+
+ taskService.deleteTask(taskPermissionResult.getResult());
+ return ResponseEntity.ok(new SimpleStatusResponse("success"));
+ }
}
diff --git a/backend/src/main/java/core/api/controller/TaskgroupController.java b/backend/src/main/java/core/api/controller/TaskgroupController.java
index 2b7e818..2735d48 100644
--- a/backend/src/main/java/core/api/controller/TaskgroupController.java
+++ b/backend/src/main/java/core/api/controller/TaskgroupController.java
@@ -4,13 +4,9 @@ import core.api.models.auth.SimpleStatusResponse;
import core.api.models.timemanager.taskgroup.TaskgroupEntityInfo;
import core.api.models.timemanager.taskgroup.TaskgroupFieldInfo;
import core.entities.timemanager.Taskgroup;
-import core.services.PermissionResult;
-import core.services.ServiceExitCode;
-import core.services.ServiceResult;
-import core.services.TaskgroupService;
+import core.services.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
-import org.springframework.scheduling.config.Task;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;
@@ -24,9 +20,11 @@ import java.util.Set;
public class TaskgroupController {
private final TaskgroupService taskgroupService;
+ private final TaskService taskService;
- public TaskgroupController(@Autowired TaskgroupService taskgroupService) {
+ public TaskgroupController(@Autowired TaskgroupService taskgroupService, @Autowired TaskService taskService) {
this.taskgroupService = taskgroupService;
+ this.taskService = taskService;
}
@PutMapping("/taskgroups")
@@ -95,4 +93,19 @@ public class TaskgroupController {
Set children = taskgroupPermissionResult.getResult().getChildren();
return ResponseEntity.ok(children.stream().map(TaskgroupEntityInfo::new));
}
+
+ @DeleteMapping("/taskgroups/{taskgroupID}/clear")
+ public ResponseEntity> clearTasks(@PathVariable long taskgroupID) {
+ PermissionResult taskgroupPermissionResult = taskgroupService.getTaskgroupByIDAndUsername(taskgroupID, SecurityContextHolder.getContext().getAuthentication().getName());
+ if (!taskgroupPermissionResult.isHasPermissions()) {
+ return ResponseEntity.status(403).body(new SimpleStatusResponse("failed"));
+ }
+
+ if (taskgroupPermissionResult.getExitCode() == ServiceExitCode.MISSING_ENTITY) {
+ return ResponseEntity.status(404).body(new SimpleStatusResponse("failed"));
+ }
+
+ taskService.clearTasks(taskgroupPermissionResult.getResult());
+ return ResponseEntity.ok(new SimpleStatusResponse("success"));
+ }
}
diff --git a/backend/src/main/java/core/entities/timemanager/Task.java b/backend/src/main/java/core/entities/timemanager/Task.java
index 6c2269e..8c17c7c 100644
--- a/backend/src/main/java/core/entities/timemanager/Task.java
+++ b/backend/src/main/java/core/entities/timemanager/Task.java
@@ -2,6 +2,7 @@ package core.entities.timemanager;
import javax.persistence.*;
import java.time.LocalDate;
+import java.util.Objects;
@Entity
@Table(name = "tasks")
@@ -91,4 +92,17 @@ public class Task {
public void setFinished(boolean finished) {
this.finished = finished;
}
+
+ @Override
+ public boolean equals(Object o) {
+ if (this == o) return true;
+ if (o == null || getClass() != o.getClass()) return false;
+ Task task = (Task) o;
+ return taskID == task.taskID;
+ }
+
+ @Override
+ public int hashCode() {
+ return Objects.hash(taskID);
+ }
}
diff --git a/backend/src/main/java/core/repositories/timemanager/TaskRepository.java b/backend/src/main/java/core/repositories/timemanager/TaskRepository.java
index 8feb765..4659263 100644
--- a/backend/src/main/java/core/repositories/timemanager/TaskRepository.java
+++ b/backend/src/main/java/core/repositories/timemanager/TaskRepository.java
@@ -1,9 +1,24 @@
package core.repositories.timemanager;
import core.entities.timemanager.Task;
+import core.entities.timemanager.Taskgroup;
+import org.springframework.data.jpa.repository.Modifying;
+import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
+import javax.transaction.Transactional;
+
@Repository
public interface TaskRepository extends CrudRepository {
+
+ @Transactional
+ @Modifying
+ @Query(value = "DELETE FROM Task t WHERE t.taskgroup = ?1")
+ void deleteAllByTaskgroup(Taskgroup taskgroup);
+
+ @Transactional
+ @Modifying
+ @Query(value = "DELETE FROM Task t WHERE t.taskID = ?1")
+ void deleteByTaskID(long taskID);
}
diff --git a/backend/src/main/java/core/services/TaskService.java b/backend/src/main/java/core/services/TaskService.java
index 0d3d323..061cb08 100644
--- a/backend/src/main/java/core/services/TaskService.java
+++ b/backend/src/main/java/core/services/TaskService.java
@@ -4,6 +4,7 @@ import core.api.models.timemanager.tasks.TaskFieldInfo;
import core.entities.timemanager.Task;
import core.entities.timemanager.Taskgroup;
import core.repositories.timemanager.TaskRepository;
+import core.repositories.timemanager.TaskgroupRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@@ -15,9 +16,12 @@ import java.util.Optional;
public class TaskService {
private final TaskRepository taskRepository;
+ private final TaskgroupRepository taskgroupRepository;
- public TaskService(@Autowired TaskRepository taskRepository) {
+ public TaskService(@Autowired TaskRepository taskRepository,
+ TaskgroupRepository taskgroupRepository) {
this.taskRepository = taskRepository;
+ this.taskgroupRepository = taskgroupRepository;
}
public ServiceResult createTask(Taskgroup taskgroup, TaskFieldInfo taskFieldInfo) {
@@ -61,4 +65,12 @@ public class TaskService {
taskRepository.save(task);
return new ServiceResult<>(task);
}
+
+ public void deleteTask(Task task) {
+ taskRepository.deleteByTaskID(task.getTaskID());
+ }
+
+ public void clearTasks(Taskgroup taskgroup) {
+ taskRepository.deleteAllByTaskgroup(taskgroup);
+ }
}