Endpoint for user's sharings created
This commit is contained in:
parent
2080de7a10
commit
c6a927e3a5
|
@ -92,6 +92,34 @@ paths:
|
|||
default:
|
||||
$ref: '#/components/responses/Error'
|
||||
|
||||
'/api/v1/user/{userId}/sharings':
|
||||
get:
|
||||
tags:
|
||||
- User
|
||||
summary: A list of all sharings with other users.
|
||||
responses:
|
||||
200:
|
||||
description: Returns the success state and a list with all sharings of the user.
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
properties:
|
||||
success:
|
||||
$ref: '#/components/schemas/Success'
|
||||
sharings:
|
||||
type: array
|
||||
items:
|
||||
type: object
|
||||
example:
|
||||
- sharingId: 8
|
||||
userId: 25
|
||||
userSharingId: 42
|
||||
- sharingId: 9
|
||||
userId: 25
|
||||
userSharingId:
|
||||
default:
|
||||
$ref: '#/components/responses/Error'
|
||||
|
||||
'/api/v1/user/{userId}/fingerprints':
|
||||
get:
|
||||
tags:
|
||||
|
@ -117,6 +145,8 @@ paths:
|
|||
- fingerprintId: 42
|
||||
fingerprint: '6FF8842B6D17F5C2098A3DD8AB55D9158DEF400A48772887A8F61531ED36B2A'
|
||||
userId: 25
|
||||
default:
|
||||
$ref: '#/components/responses/Error'
|
||||
|
||||
'/api/v1/user/{userId}/email':
|
||||
get:
|
||||
|
|
|
@ -0,0 +1,31 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
final class UserSharingsGetController extends AbstractController
|
||||
{
|
||||
protected string $route = '/api/v1/user/{userId}/sharings';
|
||||
|
||||
private int $userId;
|
||||
|
||||
public function __construct(string $url)
|
||||
{
|
||||
parent::__construct($url);
|
||||
|
||||
$this->userId = (int)$this->getUrlParamInt('userId');
|
||||
}
|
||||
|
||||
public function handle(): void
|
||||
{
|
||||
$user = new User($this->userId);
|
||||
|
||||
try {
|
||||
$this->response = new ApiJsonResponse();
|
||||
$this->response->setParameter('sharings', $user->getSharings());
|
||||
} catch (Throwable $e) {
|
||||
$this->response = new ApiJsonResponse($e->getCode() !== 0 ? $e->getCode() : ServerStatus::BAD_REQUEST);
|
||||
$this->response->setSuccess(false);
|
||||
$this->response->setMessage($e->getMessage());
|
||||
}
|
||||
}
|
||||
}
|
|
@ -4,6 +4,7 @@ declare(strict_types=1);
|
|||
|
||||
final class Sharing extends MySqlTable implements JsonSerializable
|
||||
{
|
||||
public const FIELD_ID = 'SharingId';
|
||||
public const FIELD_USER = 'User';
|
||||
public const FIELD_USER_SHARED = 'UserShared';
|
||||
|
||||
|
@ -31,6 +32,11 @@ final class Sharing extends MySqlTable implements JsonSerializable
|
|||
return $this->getField(self::FIELD_USER_SHARED);
|
||||
}
|
||||
|
||||
public function setId(int $id): void
|
||||
{
|
||||
$this->setField(self::FIELD_ID, $id);
|
||||
}
|
||||
|
||||
public function setUserId(int $userId): void
|
||||
{
|
||||
$this->setField(self::FIELD_USER, $userId);
|
||||
|
|
|
@ -0,0 +1,46 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
final class SharingCollection implements Iterator, JsonSerializable
|
||||
{
|
||||
private int $position = 0;
|
||||
|
||||
/** @var Sharing[] */
|
||||
private array $sharings = [];
|
||||
|
||||
public function add(Sharing $userId): void
|
||||
{
|
||||
$this->sharings[] = $userId;
|
||||
}
|
||||
|
||||
public function current(): Sharing
|
||||
{
|
||||
return $this->sharings[$this->position];
|
||||
}
|
||||
|
||||
public function next(): void
|
||||
{
|
||||
$this->position++;
|
||||
}
|
||||
|
||||
public function key(): int
|
||||
{
|
||||
return $this->position;
|
||||
}
|
||||
|
||||
public function valid(): bool
|
||||
{
|
||||
return isset($this->sharings[$this->position]);
|
||||
}
|
||||
|
||||
public function rewind(): void
|
||||
{
|
||||
$this->position = 0;
|
||||
}
|
||||
|
||||
public function jsonSerialize()
|
||||
{
|
||||
return $this->sharings;
|
||||
}
|
||||
}
|
|
@ -90,9 +90,7 @@ final class User extends MySqlTable implements JsonSerializable
|
|||
|
||||
$id = $database->Select(self::class, [self::FIELD_ID], [self::FIELD_USERNAME => $username])[0][self::FIELD_ID];
|
||||
|
||||
$user = $databaseGiven ? new User((int)$id, $database) : new User((int)$id);
|
||||
|
||||
return $user;
|
||||
return $databaseGiven ? new User((int)$id, $database) : new User((int)$id);
|
||||
}
|
||||
|
||||
public static function getFromEmail(string $email, DatabaseInterface &$database = null): self
|
||||
|
@ -110,9 +108,7 @@ final class User extends MySqlTable implements JsonSerializable
|
|||
|
||||
$id = $database->Select(self::class, [self::FIELD_ID], [self::FIELD_EMAIL => $email])[0][self::FIELD_ID];
|
||||
|
||||
$user = $databaseGiven ? new User((int)$id, $database) : new User((int)$id);
|
||||
|
||||
return $user;
|
||||
return $databaseGiven ? new User((int)$id, $database) : new User((int)$id);
|
||||
}
|
||||
|
||||
public function getFingerprintIds(): array
|
||||
|
@ -140,6 +136,29 @@ final class User extends MySqlTable implements JsonSerializable
|
|||
);
|
||||
}
|
||||
|
||||
public function getSharings(): SharingCollection
|
||||
{
|
||||
$result = $this->database->Select(
|
||||
Sharing::class,
|
||||
[],
|
||||
[Sharing::FIELD_USER => $this->getUserId()]
|
||||
);
|
||||
|
||||
$sharings = new SharingCollection();
|
||||
|
||||
foreach ($result as $record) {
|
||||
$sharing = new Sharing(null, $this->database);
|
||||
|
||||
$sharing->setId((int)$record[Sharing::FIELD_ID]);
|
||||
$sharing->setUserId((int)$record[Sharing::FIELD_USER]);
|
||||
$sharing->setUserShared((int)$record[Sharing::FIELD_USER_SHARED]);
|
||||
|
||||
$sharings->add($sharing);
|
||||
}
|
||||
|
||||
return $sharings;
|
||||
}
|
||||
|
||||
public function jsonSerialize()
|
||||
{
|
||||
return [
|
||||
|
|
Loading…
Reference in New Issue