Endpoint for editing fingerprints added
This commit is contained in:
parent
f6d1376dbb
commit
b0f2991346
|
@ -0,0 +1,57 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
final class FingerprintPutController extends AbstractController
|
||||
{
|
||||
protected string $route = '/api/v1/fingerprint/{fingerprintId}';
|
||||
|
||||
private int $fingerprintId;
|
||||
|
||||
public function __construct(string $url)
|
||||
{
|
||||
parent::__construct($url);
|
||||
|
||||
$this->fingerprintId = (int)$this->getUrlParamInt('fingerprintId');
|
||||
}
|
||||
|
||||
public function handle(): void
|
||||
{
|
||||
parent::handle();
|
||||
|
||||
if ($this->response->getStatus() !== ServerStatus::OK) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->response = new ApiJsonResponse();
|
||||
|
||||
try {
|
||||
$json = json_decode($this->requestBody, true);
|
||||
$fingerprint = new Fingerprint($this->fingerprintId);
|
||||
|
||||
if ($this->handleFingerprint($fingerprint, $json)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->response->setMessage('Fingerprint did not differ from the stored. Nothing changed.');
|
||||
} catch (Throwable $e) {
|
||||
$this->response->setStatus(ServerStatus::BAD_REQUEST);
|
||||
$this->response->setParameter('success', false);
|
||||
$this->response->setMessage($e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public function handleFingerprint(Fingerprint $fingerprint, array $json): bool
|
||||
{
|
||||
if (isset($json['fingerprint'])) {
|
||||
if ($fingerprint->getFingerprint() !== $json['fingerprint']) {
|
||||
$fingerprint->setFingerprint($json['fingerprint']);
|
||||
$fingerprint->Save();
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue