feat: add update status endpoint

This commit is contained in:
Miguel Nogueira 2025-04-16 16:06:19 +01:00
parent c2e621859e
commit 646655bc50
4 changed files with 72 additions and 3 deletions

View File

@ -24,6 +24,8 @@ $app->get('/tasks/{id}', [TaskController::class, 'getTask']);
$app->delete('/tasks/{id}', [TaskController::class, 'delete']); $app->delete('/tasks/{id}', [TaskController::class, 'delete']);
$app->patch('/tasks/{id}', [TaskController::class, 'update']); $app->patch('/tasks/{id}', [TaskController::class, 'update']);
$app->patch('/tasks/{id}/status/update', [TaskController::class, 'updateStatus']);
$app->get('/tasks', [TaskController::class, 'getTasks']); $app->get('/tasks', [TaskController::class, 'getTasks']);
$app->post('/tasks', [TaskController::class, 'addTask']); $app->post('/tasks', [TaskController::class, 'addTask']);

View File

@ -261,4 +261,35 @@ class TaskController
return $errorResponse; 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);
}
}
} }

View File

@ -30,7 +30,8 @@ class Task
private string $start; private string $start;
private string $startDt;
private array $dirty = [];
public function __construct($status = TaskStatus::STARTED) 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 public function getTaskOwner(): int
{ {
return self::task_owner; return self::task_owner;
@ -55,6 +69,16 @@ class Task
$this->updatedAt = Carbon::now(); $this->updatedAt = Carbon::now();
} }
public function getCreatedAt(): string
{
return $this->createdAt->toDateTimeString();
}
public function getUpdatedAt(): string
{
return $this->updatedAt->toDateTimeString();
}
public function getName(): string public function getName(): string
{ {
return $this->name; return $this->name;
@ -63,6 +87,7 @@ class Task
public function setName(string $name): self public function setName(string $name): self
{ {
$this->name = $name; $this->name = $name;
$this->touch();
return $this; return $this;
} }
@ -74,6 +99,7 @@ class Task
public function setDescription(string $description): self public function setDescription(string $description): self
{ {
$this->description = $description; $this->description = $description;
$this->touch();
return $this; return $this;
} }
@ -85,6 +111,7 @@ class Task
public function setStart(string $start): self public function setStart(string $start): self
{ {
$this->start = Carbon::parse($start)->toDateTimeString(); $this->start = Carbon::parse($start)->toDateTimeString();
$this->touch();
return $this; return $this;
} }
@ -96,6 +123,7 @@ class Task
public function setEnd(string $end): self public function setEnd(string $end): self
{ {
$this->end = Carbon::parse($end)->toDateTimeString(); $this->end = Carbon::parse($end)->toDateTimeString();
$this->touch();
return $this; return $this;
} }
@ -107,6 +135,7 @@ class Task
public function setStatusId(TaskStatus $statusId): self public function setStatusId(TaskStatus $statusId): self
{ {
$this->statusId = $statusId; $this->statusId = $statusId;
$this->touch();
return $this; return $this;
} }
@ -119,6 +148,7 @@ class Task
public function setId(int $id): Task public function setId(int $id): Task
{ {
$this->id = $id; $this->id = $id;
$this->touch();
return $this; return $this;
} }
@ -148,8 +178,8 @@ class Task
return [ return [
'id' => $this->id ?? null, 'id' => $this->id ?? null,
'task_owner' => 1, // fake hard-coded user for now 'task_owner' => 1, // fake hard-coded user for now
'created_at' => $this->createdAt->toDateTimeString(), 'created_at' => $this->getCreatedAt(),
'updated_at' => $this->updatedAt->toDateTimeString(), 'updated_at' => $this->getUpdatedAt(),
'name' => $this->getName(), 'name' => $this->getName(),
'description' => $this->getDescription(), 'description' => $this->getDescription(),
'start' => $this->getStart(), 'start' => $this->getStart(),

View File

@ -107,6 +107,12 @@ class TaskRepository implements TaskDao
if (property_exists($task, snakeToCamel($field))) if (property_exists($task, snakeToCamel($field)))
{ {
$setClause[] = camel_to_snake($field) . " = ?"; // SET name = ?, SET updatedAt = ? (fixed by camel_to_snake) $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))}(); $params[] = $task->{'get' . ucfirst(snakeToCamel($field))}();
} }