diff --git a/api/openapi.yaml b/api/openapi.yaml index 6303a6f..b515104 100644 --- a/api/openapi.yaml +++ b/api/openapi.yaml @@ -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: @@ -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: diff --git a/backend/classes/controller/UserSharingsGetController.php b/backend/classes/controller/UserSharingsGetController.php new file mode 100644 index 0000000..2a9faf0 --- /dev/null +++ b/backend/classes/controller/UserSharingsGetController.php @@ -0,0 +1,31 @@ +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()); + } + } +} \ No newline at end of file diff --git a/backend/classes/database/Sharing.php b/backend/classes/database/Sharing.php index 37d41be..fe9beb0 100644 --- a/backend/classes/database/Sharing.php +++ b/backend/classes/database/Sharing.php @@ -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); diff --git a/backend/classes/database/SharingCollection.php b/backend/classes/database/SharingCollection.php new file mode 100644 index 0000000..9881cd1 --- /dev/null +++ b/backend/classes/database/SharingCollection.php @@ -0,0 +1,46 @@ +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; + } +} \ No newline at end of file diff --git a/backend/classes/database/User.php b/backend/classes/database/User.php index 99fd976..753fc5c 100644 --- a/backend/classes/database/User.php +++ b/backend/classes/database/User.php @@ -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 [