From b0f299134632ba76b6815e5052f1daf182ababd0 Mon Sep 17 00:00:00 2001 From: Mal <=> Date: Thu, 20 Aug 2020 23:07:55 +0200 Subject: [PATCH] Endpoint for editing fingerprints added --- .../controller/FingerprintPutController.php | 57 +++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 backend/classes/controller/FingerprintPutController.php diff --git a/backend/classes/controller/FingerprintPutController.php b/backend/classes/controller/FingerprintPutController.php new file mode 100644 index 0000000..a2123e9 --- /dev/null +++ b/backend/classes/controller/FingerprintPutController.php @@ -0,0 +1,57 @@ +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; + } +} \ No newline at end of file