From 013819c887717577f7c1d7ee897ce0c83c0d3b64 Mon Sep 17 00:00:00 2001 From: Sebastian Date: Sun, 22 Oct 2023 01:03:10 +0200 Subject: [PATCH] Implementing edit route for tasks --- .../core/api/controller/TaskController.java | 36 ++++++++++++------- .../main/java/core/services/TaskService.java | 21 +++++++++++ 2 files changed, 45 insertions(+), 12 deletions(-) diff --git a/backend/src/main/java/core/api/controller/TaskController.java b/backend/src/main/java/core/api/controller/TaskController.java index c205a55..ef850e7 100644 --- a/backend/src/main/java/core/api/controller/TaskController.java +++ b/backend/src/main/java/core/api/controller/TaskController.java @@ -37,18 +37,7 @@ public class TaskController { return ResponseEntity.status(404).body(new SimpleStatusResponse("failed")); } - - if(status.equalsIgnoreCase("all")) { - return ResponseEntity.ok(taskgroupPermissionResult.getResult().getTasks().stream().map(TaskEntityInfo::new)); - } else if(status.equalsIgnoreCase("overdue")) { - //List overdue tasks - return ResponseEntity.ok(new SimpleStatusResponse("success")); - } else if(status.equalsIgnoreCase("upcoming")) { - //List upcoming tasks - return ResponseEntity.ok(new SimpleStatusResponse("success")); - } else { - return ResponseEntity.status(404).body(new SimpleStatusResponse("failed")); - } + return ResponseEntity.ok(taskgroupPermissionResult.getResult().getTasks().stream().map(TaskEntityInfo::new)); } @PutMapping("/tasks/{taskgroupID}") @@ -69,4 +58,27 @@ public class TaskController { return ResponseEntity.ok(new TaskEntityInfo(creationResult.getResult())); } } + + @PostMapping("/tasks/{taskID}") + public ResponseEntity editTask(@PathVariable long taskID, @RequestBody @Valid TaskFieldInfo taskFieldInfo) { + 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")); + } + + if(taskFieldInfo.getStartDate().isAfter(taskFieldInfo.getDeadline())) { + return ResponseEntity.status(400).body(new SimpleStatusResponse("failed")); + } + + ServiceResult editorResult = taskService.editTask(taskPermissionResult.getResult(), taskFieldInfo); + if (editorResult.getExitCode() == ServiceExitCode.OK) { + return ResponseEntity.ok(new SimpleStatusResponse("success")); + } else { + return ResponseEntity.status(409).body(new SimpleStatusResponse("failed")); + } + } } diff --git a/backend/src/main/java/core/services/TaskService.java b/backend/src/main/java/core/services/TaskService.java index f9c2020..0d3d323 100644 --- a/backend/src/main/java/core/services/TaskService.java +++ b/backend/src/main/java/core/services/TaskService.java @@ -9,6 +9,7 @@ import org.springframework.stereotype.Service; import java.util.Collection; import java.util.List; +import java.util.Optional; @Service public class TaskService { @@ -40,4 +41,24 @@ public class TaskService { } return false; } + + public PermissionResult getTaskPermissions(long taskID, String name) { + Optional task = taskRepository.findById(taskID); + return task.map(value -> new PermissionResult<>(value, value.getTaskgroup().getUser().getUsername().equals(name))).orElseGet(() -> + new PermissionResult<>(ServiceExitCode.MISSING_ENTITY)); + } + + public ServiceResult editTask(Task task, TaskFieldInfo taskFieldInfo) { + if(!task.getTaskName().equals(taskFieldInfo.getTaskName()) && !existTaskByName(task.getTaskgroup().getTasks(), taskFieldInfo.getTaskName())) { + task.setTaskName(taskFieldInfo.getTaskName()); + + } else if(!task.getTaskName().equals(taskFieldInfo.getTaskName()) && existTaskByName(task.getTaskgroup().getTasks(), taskFieldInfo.getTaskName())) { + return new ServiceResult<>(ServiceExitCode.ENTITY_ALREADY_EXIST); + } + task.setEta(taskFieldInfo.getEta()); + task.setStartDate(taskFieldInfo.getStartDate()); + task.setDeadline(taskFieldInfo.getDeadline()); + taskRepository.save(task); + return new ServiceResult<>(task); + } }