From 459be4a99963f1562fbec2599eccab55f03b9f14 Mon Sep 17 00:00:00 2001 From: Mal <=> Date: Thu, 20 Aug 2020 18:36:30 +0200 Subject: [PATCH] Endpoints for sharing implemented --- .../controller/SharingDeleteController.php | 38 +++++++++++++++++++ .../controller/SharingGetController.php | 38 +++++++++++++++++++ .../controller/SharingPostController.php | 36 ++++++++++++++++++ backend/classes/database/Sharing.php | 11 +++++- 4 files changed, 122 insertions(+), 1 deletion(-) create mode 100644 backend/classes/controller/SharingDeleteController.php create mode 100644 backend/classes/controller/SharingGetController.php create mode 100644 backend/classes/controller/SharingPostController.php diff --git a/backend/classes/controller/SharingDeleteController.php b/backend/classes/controller/SharingDeleteController.php new file mode 100644 index 0000000..122d14d --- /dev/null +++ b/backend/classes/controller/SharingDeleteController.php @@ -0,0 +1,38 @@ +sharingId = (int)$this->getUrlParamInt('sharingId'); + } + + public function handle(): void + { + parent::handle(); + + if ($this->response->getStatus() !== ServerStatus::OK) { + return; + } + + try { + $sharing = new Sharing($this->sharingId); + $sharing->Delete(); + + $this->response = new ApiJsonResponse(); + $this->response->setParameter('success', true); + } catch (Throwable $e) { + $this->response = new ApiJsonResponse(ServerStatus::BAD_REQUEST); + $this->response->setParameter('success', false); + $this->response->setMessage($e->getMessage()); + } + } +} \ No newline at end of file diff --git a/backend/classes/controller/SharingGetController.php b/backend/classes/controller/SharingGetController.php new file mode 100644 index 0000000..80788ef --- /dev/null +++ b/backend/classes/controller/SharingGetController.php @@ -0,0 +1,38 @@ +sharingId = (int)$this->getUrlParamInt('sharingId'); + } + + public function handle(): void + { + parent::handle(); + + if ($this->response->getStatus() !== ServerStatus::OK) { + return; + } + + try { + $sharing = new Sharing($this->sharingId); + + $this->response = new ApiJsonResponse(); + $this->response->setParameter('success', true); + $this->response->setResult($sharing); + } catch (Throwable $e) { + $this->response = new ApiJsonResponse(ServerStatus::BAD_REQUEST); + $this->response->setParameter('success', false); + $this->response->setMessage($e->getMessage()); + } + } +} \ No newline at end of file diff --git a/backend/classes/controller/SharingPostController.php b/backend/classes/controller/SharingPostController.php new file mode 100644 index 0000000..8cef2e2 --- /dev/null +++ b/backend/classes/controller/SharingPostController.php @@ -0,0 +1,36 @@ +response->getStatus() !== ServerStatus::OK) { + return; + } + + $json = json_decode($this->requestBody); + + try { + $sharing = new Sharing(); + $sharing->setUserId($json->userId); + $sharing->setUserShared($json->userSharedId); + $sharing->Save(); + + $this->response = new ApiJsonResponse(); + $this->response->setParameter('sharingId', $sharing->getSharingId()); + } catch (Throwable $e) { + $this->response = new ApiJsonResponse(ServerStatus::BAD_REQUEST); + $this->response->setMessage($e->getMessage()); + } + } +} \ No newline at end of file diff --git a/backend/classes/database/Sharing.php b/backend/classes/database/Sharing.php index d9b4999..c3ebbfd 100644 --- a/backend/classes/database/Sharing.php +++ b/backend/classes/database/Sharing.php @@ -2,7 +2,7 @@ declare(strict_types=1); -final class Sharing extends MySqlTable +final class Sharing extends MySqlTable implements JsonSerializable { public const FIELD_USER = 'User'; public const FIELD_USER_SHARED = 'UserShared'; @@ -40,4 +40,13 @@ final class Sharing extends MySqlTable { $this->setField(self::FIELD_USER_SHARED, $userShared); } + + public function jsonSerialize() + { + return [ + 'sharingId' => $this->getSharingId(), + 'userId' => $this->getUserId(), + 'userSharedId' => $this->getUserShared(), + ]; + } }