diff --git a/public/index.php b/public/index.php index c019b98..938cc94 100644 --- a/public/index.php +++ b/public/index.php @@ -24,6 +24,8 @@ $app->get('/tasks/{id}', [TaskController::class, 'getTask']); $app->delete('/tasks/{id}', [TaskController::class, 'delete']); $app->patch('/tasks/{id}', [TaskController::class, 'update']); +$app->patch('/tasks/{id}/status/update', [TaskController::class, 'updateStatus']); + $app->get('/tasks', [TaskController::class, 'getTasks']); $app->post('/tasks', [TaskController::class, 'addTask']); diff --git a/src/Controllers/TaskController.php b/src/Controllers/TaskController.php index 4f6342a..ab839b6 100644 --- a/src/Controllers/TaskController.php +++ b/src/Controllers/TaskController.php @@ -261,4 +261,35 @@ class TaskController return $errorResponse; } } + + public function updateStatus(ServerRequestInterface $request, ResponseInterface $response, $id): ResponseInterface + { + try + { + $task = $this->repository->readById($id); + $newCode = $request->getParsedBody()['payload']['status_id']; + + if (is_null(TaskStatus::tryFrom($newCode))) + { + $this->builder->setError()->setErrorCode(400)->setErrorMessage('Invalid task status, must be between 1 to 4 (started, in progress, blocked, completed).'); + $response->getBody()->write($this->builder->build()); + + return $response->withStatus(400); + } + + $task->setStatusId(TaskStatus::from($newCode)); + $this->repository->updateSelective($task, ['status_id']); + + return $response->withStatus(204); + + + } catch (TaskNotFoundException $e) + { + $this->builder->setError()->setErrorCode(404)->setErrorMessage($e->getMessage()); + $response->getBody()->write($this->builder->build()); + + return $response->withStatus(404); + } + } + } \ No newline at end of file diff --git a/src/Models/Task.php b/src/Models/Task.php index b2ccbd2..c1de1d5 100644 --- a/src/Models/Task.php +++ b/src/Models/Task.php @@ -30,7 +30,8 @@ class Task private string $start; - private string $startDt; + + private array $dirty = []; public function __construct($status = TaskStatus::STARTED) @@ -40,6 +41,19 @@ class Task } + + protected function markDirty($field): bool + { + if(property_exists(self::class, $field)) + { + $this->dirty[] = $field; + return true; + } + + return false; + } + + public function getTaskOwner(): int { return self::task_owner; @@ -55,6 +69,16 @@ class Task $this->updatedAt = Carbon::now(); } + public function getCreatedAt(): string + { + return $this->createdAt->toDateTimeString(); + } + + public function getUpdatedAt(): string + { + return $this->updatedAt->toDateTimeString(); + } + public function getName(): string { return $this->name; @@ -63,6 +87,7 @@ class Task public function setName(string $name): self { $this->name = $name; + $this->touch(); return $this; } @@ -74,6 +99,7 @@ class Task public function setDescription(string $description): self { $this->description = $description; + $this->touch(); return $this; } @@ -85,6 +111,7 @@ class Task public function setStart(string $start): self { $this->start = Carbon::parse($start)->toDateTimeString(); + $this->touch(); return $this; } @@ -96,6 +123,7 @@ class Task public function setEnd(string $end): self { $this->end = Carbon::parse($end)->toDateTimeString(); + $this->touch(); return $this; } @@ -107,6 +135,7 @@ class Task public function setStatusId(TaskStatus $statusId): self { $this->statusId = $statusId; + $this->touch(); return $this; } @@ -119,6 +148,7 @@ class Task public function setId(int $id): Task { $this->id = $id; + $this->touch(); return $this; } @@ -148,8 +178,8 @@ class Task return [ 'id' => $this->id ?? null, 'task_owner' => 1, // fake hard-coded user for now - 'created_at' => $this->createdAt->toDateTimeString(), - 'updated_at' => $this->updatedAt->toDateTimeString(), + 'created_at' => $this->getCreatedAt(), + 'updated_at' => $this->getUpdatedAt(), 'name' => $this->getName(), 'description' => $this->getDescription(), 'start' => $this->getStart(), diff --git a/src/Repositories/TaskRepository.php b/src/Repositories/TaskRepository.php index e35a274..d83d373 100644 --- a/src/Repositories/TaskRepository.php +++ b/src/Repositories/TaskRepository.php @@ -107,6 +107,12 @@ class TaskRepository implements TaskDao if (property_exists($task, snakeToCamel($field))) { $setClause[] = camel_to_snake($field) . " = ?"; // SET name = ?, SET updatedAt = ? (fixed by camel_to_snake) + + if ($field == 'status_id') + { + $params[] = $task->getStatusId()->value; + continue; + } $params[] = $task->{'get' . ucfirst(snakeToCamel($field))}(); }