Compare commits

...

3 Commits

Author SHA1 Message Date
Mal 22e27ed899 UI objects for UserSharingsGet and FingerprintPut added 2020-09-10 11:46:31 +02:00
Mal b5184b9b3c Merge branch 'nightly' into ui 2020-09-09 21:35:48 +02:00
Mal c6a927e3a5 Endpoint for user's sharings created 2020-09-09 21:35:11 +02:00
11 changed files with 254 additions and 14 deletions

View File

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

View File

@ -31,7 +31,7 @@ final class FingerprintPutController extends AbstractController
try {
$fingerprint = new Fingerprint($this->fingerprintId);
if ($this->hasUserPermission($fingerprint->getUserId()) || $this->handleFingerprint($fingerprint)) {
if ($this->hasUserPermission($fingerprint->getUserId()) && $this->handleFingerprint($fingerprint)) {
return;
}

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
{
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);

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];
$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 [

View File

@ -0,0 +1,18 @@
import ApiRequest from "./ApiRequest.js";
import Fingerprint from "../types/Fingerprint.js";
import RequestMethod from "./RequestMethod.js";
import FingerprintChangedEvent from "../event/FingerprintChangedEvent.js";
export default class FingerprintPutApiRequest extends ApiRequest
{
public constructor(fingerprint: Fingerprint) {
super('fingerprint/' + fingerprint.getFingerprintId(), RequestMethod.PUT);
this.addParameter('fingerprint', fingerprint.getFingerprint());
this.onSuccess = (responseText: string) => {
console.log(responseText);
window.dispatchEvent(new FingerprintChangedEvent(fingerprint));
};
}
}

View File

@ -0,0 +1,26 @@
import ApiRequest from "./ApiRequest.js";
import RequestMethod from "./RequestMethod.js";
import Sharing from "../types/Sharing.js";
import UserSharingsGetEvent from "../event/UserSharingsGetEvent.js";
export default class UserSharingsGetApiRequest extends ApiRequest
{
public constructor(userId: number)
{
super('user/' + userId + '/sharings', RequestMethod.GET);
this.onSuccess = (response: string) => {
let json = JSON.parse(response);
let sharings: Array<Sharing> = [];
json.sharings.forEach(
(result: any) => {
sharings.push(new Sharing(result.sharingId, result.userId, result.userSharedId));
}
);
window.dispatchEvent(new UserSharingsGetEvent(sharings));
}
}
}

View File

@ -0,0 +1,18 @@
import RingfingerEvent from "./RingfingerEvent.js";
import Fingerprint from "../types/Fingerprint.js";
export default class FingerprintChangedEvent extends RingfingerEvent
{
private fingerprint: Fingerprint;
public constructor(fingerprint: Fingerprint) {
super(FingerprintChangedEvent.name);
this.fingerprint = fingerprint;
}
public getFingerprint(): Fingerprint
{
return this.fingerprint;
}
}

View File

@ -0,0 +1,18 @@
import RingfingerEvent from "./RingfingerEvent.js";
import Sharing from "../types/Sharing.js";
export default class UserSharingsGetEvent extends RingfingerEvent
{
private sharings: Array<Sharing>;
public constructor(sharings: Array<Sharing>) {
super(UserSharingsGetEvent.name);
this.sharings = sharings;
}
public getSharings(): Array<Sharing>
{
return this.sharings;
}
}

28
js/types/Sharing.ts Normal file
View File

@ -0,0 +1,28 @@
export default class Sharing
{
private readonly sharingId: number;
private readonly userId: number;
private readonly userSharingId: number;
public constructor(sharingId: number, userId: number, userSharingId: number)
{
this.sharingId = sharingId;
this.userId = userId;
this.userSharingId = userSharingId;
}
public getSharingId(): number
{
return this.sharingId;
}
public getUserId(): number
{
return this.userId;
}
public getUserSharingId(): number
{
return this.userSharingId;
}
}