Implementing edit route for tasks

This commit is contained in:
Sebastian 2023-10-22 01:03:10 +02:00
parent 346826ce10
commit 013819c887
2 changed files with 45 additions and 12 deletions

View File

@ -37,18 +37,7 @@ public class TaskController {
return ResponseEntity.status(404).body(new SimpleStatusResponse("failed")); return ResponseEntity.status(404).body(new SimpleStatusResponse("failed"));
} }
if(status.equalsIgnoreCase("all")) {
return ResponseEntity.ok(taskgroupPermissionResult.getResult().getTasks().stream().map(TaskEntityInfo::new)); 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"));
}
} }
@PutMapping("/tasks/{taskgroupID}") @PutMapping("/tasks/{taskgroupID}")
@ -69,4 +58,27 @@ public class TaskController {
return ResponseEntity.ok(new TaskEntityInfo(creationResult.getResult())); return ResponseEntity.ok(new TaskEntityInfo(creationResult.getResult()));
} }
} }
@PostMapping("/tasks/{taskID}")
public ResponseEntity<?> editTask(@PathVariable long taskID, @RequestBody @Valid TaskFieldInfo taskFieldInfo) {
PermissionResult<Task> 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<Task> 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"));
}
}
} }

View File

@ -9,6 +9,7 @@ import org.springframework.stereotype.Service;
import java.util.Collection; import java.util.Collection;
import java.util.List; import java.util.List;
import java.util.Optional;
@Service @Service
public class TaskService { public class TaskService {
@ -40,4 +41,24 @@ public class TaskService {
} }
return false; return false;
} }
public PermissionResult<Task> getTaskPermissions(long taskID, String name) {
Optional<Task> 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<Task> 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);
}
} }