Endpoints for sharing implemented

This commit is contained in:
Mal 2020-08-20 18:36:30 +02:00
parent 0334c7022d
commit 459be4a999
4 changed files with 122 additions and 1 deletions

View File

@ -0,0 +1,38 @@
<?php
declare(strict_types=1);
final class SharingDeleteController extends AbstractController
{
protected string $route = '/api/v1/sharing/{sharingId}';
private int $sharingId;
public function __construct(string $url)
{
parent::__construct($url);
$this->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());
}
}
}

View File

@ -0,0 +1,38 @@
<?php
declare(strict_types=1);
final class SharingGetController extends AbstractController
{
protected string $route = '/api/v1/sharing/{sharingId}';
private int $sharingId;
public function __construct(string $url)
{
parent::__construct($url);
$this->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());
}
}
}

View File

@ -0,0 +1,36 @@
<?php
declare(strict_types=1);
final class SharingPostController extends AbstractController
{
protected string $route = '/api/v1/sharing';
protected array $mandatoryAttributes = [
'userId',
'userSharedId',
];
public function handle(): void
{
parent::handle();
if ($this->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());
}
}
}

View File

@ -2,7 +2,7 @@
declare(strict_types=1); 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 = 'User';
public const FIELD_USER_SHARED = 'UserShared'; public const FIELD_USER_SHARED = 'UserShared';
@ -40,4 +40,13 @@ final class Sharing extends MySqlTable
{ {
$this->setField(self::FIELD_USER_SHARED, $userShared); $this->setField(self::FIELD_USER_SHARED, $userShared);
} }
public function jsonSerialize()
{
return [
'sharingId' => $this->getSharingId(),
'userId' => $this->getUserId(),
'userSharedId' => $this->getUserShared(),
];
}
} }