feat: add update status endpoint
This commit is contained in:
parent
c2e621859e
commit
646655bc50
@ -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']);
|
||||
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -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(),
|
||||
|
@ -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))}();
|
||||
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user