Merge branch 'nightly' into ui

This commit is contained in:
Mal 2020-09-09 21:35:48 +02:00
commit b5184b9b3c
5 changed files with 145 additions and 13 deletions

View File

@ -92,6 +92,34 @@ paths:
default: default:
$ref: '#/components/responses/Error' $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': '/api/v1/user/{userId}/fingerprints':
get: get:
tags: tags:
@ -110,13 +138,15 @@ paths:
type: array type: array
items: items:
type: object type: object
example: example:
- fingerprintId: 8 - fingerprintId: 8
fingerprint: '5BDF1668E59F2184582591699F55D9158DEF400A48772887A8F61531ED36B2A' fingerprint: '5BDF1668E59F2184582591699F55D9158DEF400A48772887A8F61531ED36B2A'
userId: 25 userId: 25
- fingerprintId: 42 - fingerprintId: 42
fingerprint: '6FF8842B6D17F5C2098A3DD8AB55D9158DEF400A48772887A8F61531ED36B2A' fingerprint: '6FF8842B6D17F5C2098A3DD8AB55D9158DEF400A48772887A8F61531ED36B2A'
userId: 25 userId: 25
default:
$ref: '#/components/responses/Error'
'/api/v1/user/{userId}/email': '/api/v1/user/{userId}/email':
get: get:

View File

@ -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());
}
}
}

View File

@ -4,6 +4,7 @@ declare(strict_types=1);
final class Sharing extends MySqlTable implements JsonSerializable final class Sharing extends MySqlTable implements JsonSerializable
{ {
public const FIELD_ID = 'SharingId';
public const FIELD_USER = 'User'; public const FIELD_USER = 'User';
public const FIELD_USER_SHARED = 'UserShared'; public const FIELD_USER_SHARED = 'UserShared';
@ -31,6 +32,11 @@ final class Sharing extends MySqlTable implements JsonSerializable
return $this->getField(self::FIELD_USER_SHARED); 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 public function setUserId(int $userId): void
{ {
$this->setField(self::FIELD_USER, $userId); $this->setField(self::FIELD_USER, $userId);

View File

@ -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;
}
}

View File

@ -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]; $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 $databaseGiven ? new User((int)$id, $database) : new User((int)$id);
return $user;
} }
public static function getFromEmail(string $email, DatabaseInterface &$database = null): self 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]; $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 $databaseGiven ? new User((int)$id, $database) : new User((int)$id);
return $user;
} }
public function getFingerprintIds(): array 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() public function jsonSerialize()
{ {
return [ return [