issue-10 #17

Merged
sebastian merged 29 commits from issue-10 into master 2023-10-22 11:40:25 +02:00
2 changed files with 45 additions and 12 deletions
Showing only changes of commit 013819c887 - Show all commits

View File

@ -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<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.List;
import java.util.Optional;
@Service
public class TaskService {
@ -40,4 +41,24 @@ public class TaskService {
}
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);
}
}