Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FZ-121 pint fixes, user storage #180

Merged
merged 3 commits into from
Dec 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions app/Exceptions/ApiException.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ class ApiException extends Exception
{
use ApiExceptionTrait;

/** @throws static */
public function throw()
{
throw $this;
}

public function __construct(int $httpCode, string $errorCode, array $errorData = [])
{
$this->errorCode = $errorCode;
Expand Down
5 changes: 0 additions & 5 deletions app/Exceptions/ApiExceptionTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,6 @@ public function getJson(): string
return json_encode($this->getData());
}

public function throw()
{
throw $this;
}

/** @param array<ApiExceptionTrait> $addErrors */
public function renderMany(array $addErrors = []): JsonResponse
{
Expand Down
6 changes: 6 additions & 0 deletions app/Exceptions/ApiFatalException.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ class ApiFatalException extends RuntimeException
{
use ApiExceptionTrait;

/** @throws static */
public function throw()
{
throw $this;
}

public function __construct(string $errorCode, array $errorData = [])
{
$this->errorCode = $errorCode;
Expand Down
55 changes: 53 additions & 2 deletions app/Http/Controllers/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use App\Http\Resources\UserResource;
use App\Models\User;
use App\Services\User\ChangePasswordService;
use App\Services\User\StorageService;
use App\Services\User\UpdateUserService;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\Auth;
Expand All @@ -23,8 +24,7 @@ class UserController extends ApiController
{
protected function initPermissions(): void
{
$this->permissionOneOf(Permission::any);
$this->permissionOneOf(Permission::unverified, Permission::verified)->only(['patch', 'status', 'password']);
$this->permissionOneOf(Permission::unverified, Permission::verified)->except(['login', 'logout']);
}

#[OA\Post(
Expand Down Expand Up @@ -190,4 +190,55 @@ public function password(ChangePasswordService $changePasswordService): JsonResp

return new JsonResponse();
}

#[OA\Put(
path: '/api/v1/user/storage/{key}',
description: new PermissionDescribe([Permission::unverified, Permission::verified]),
summary: 'Update user storage, send null value to unset key',
requestBody: new OA\RequestBody(content: new OA\JsonContent(example: '{}')),
tags: ['User'],
parameters: [
new OA\Parameter(
name: 'key',
description: 'Storage key',
in: 'path',
required: true,
schema: new OA\Schema(type: 'string', example: ''),
),
],
responses: [
new OA\Response(response: 200, description: 'OK'),
new OA\Response(response: 400, description: 'Bad Request'),
new OA\Response(response: 401, description: 'Unauthorised'),
],
)] /** @throws ApiException */
public function storagePut(StorageService $storageService, Request $request, string $key): JsonResponse
{
return $storageService->put($this->getUserOrFail(), $key, $request->getContent());
}

#[OA\Get(
path: '/api/v1/user/storage/{key}',
description: new PermissionDescribe([Permission::unverified, Permission::verified]),
summary: 'Read user storage, send empty key to list keys',
tags: ['User'],
parameters: [
new OA\Parameter(
name: 'key',
description: 'Storage key',
in: 'path',
allowEmptyValue: true,
schema: new OA\Schema(type: 'string', example: ''),
),
],
responses: [
new OA\Response(response: 200, description: 'OK'),
new OA\Response(response: 400, description: 'Bad Request'),
new OA\Response(response: 401, description: 'Unauthorised'),
],
)]
public function storageGet(StorageService $storageService, string $key = ''): JsonResponse
{
return $storageService->get($this->getUserOrFail(), $key);
}
}
1 change: 0 additions & 1 deletion app/Http/Resources/MeetingAttendantResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
*/
class MeetingAttendantResource extends AbstractJsonResource
{

protected static function getMappedFields(): array
{
return [
Expand Down
1 change: 0 additions & 1 deletion app/Http/Resources/MeetingResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
*/
class MeetingResource extends AbstractJsonResource
{

protected static function getMappedFields(): array
{
return [
Expand Down
1 change: 0 additions & 1 deletion app/Http/Resources/MeetingResourceResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
*/
class MeetingResourceResource extends AbstractJsonResource
{

protected static function getMappedFields(): array
{
return [
Expand Down
1 change: 0 additions & 1 deletion app/Models/Dictionary.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use Carbon\CarbonImmutable;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;

/**
Expand Down
1 change: 0 additions & 1 deletion app/Models/QueryBuilders/MeetingAttendantBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,4 @@
*/
class MeetingAttendantBuilder extends Builder
{

}
13 changes: 6 additions & 7 deletions app/Models/QueryBuilders/MeetingBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,12 @@
use Illuminate\Database\Eloquent\Collection;

/**
* @method Collection|BuilderModel[] get(string[] $columns = ['*'])
* @method BuilderModel|null find(string $id)
* @method BuilderModel findOrFail(string $id)
* @method self with(array $relations)
* @method BuilderModel newModelInstance(array $attributes = [])
*/
* @method Collection|BuilderModel[] get(string[] $columns = ['*'])
* @method BuilderModel|null find(string $id)
* @method BuilderModel findOrFail(string $id)
* @method self with(array $relations)
* @method BuilderModel newModelInstance(array $attributes = [])
*/
abstract class MeetingBuilder extends Builder
{

}
1 change: 0 additions & 1 deletion app/Models/QueryBuilders/MeetingResourceBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,4 @@
*/
class MeetingResourceBuilder extends Builder
{

}
71 changes: 71 additions & 0 deletions app/Services/User/StorageService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

namespace App\Services\User;

use App\Exceptions\ApiException;
use App\Exceptions\ExceptionFactory;
use App\Exceptions\FatalExceptionFactory;
use App\Models\User;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\DB;
use JsonException;
use Throwable;

class StorageService
{
/** @throws JsonException */
private function getUserStorage(User $user): array
{
return json_decode(
DB::table('users')->find($user->id, 'storage')->storage ?? '[]',
associative: true,
flags: JSON_THROW_ON_ERROR,
);
}

private function setUserStorage(User $user, array $storage): void
{
DB::table('users')->where('id', $user->id)->update([
'storage' => json_encode($storage),
]);
}

private function getStorageKeys(array $storage): JsonResponse
{
return new JsonResponse(array_keys($storage));
}

private function getStorageByKey(array $storage, string $key): JsonResponse
{
return new JsonResponse($storage[$key] ?? 'null', json: true);
}

/** @throws ApiException */
public function put(User $user, string $key, string $contents): JsonResponse
{
try {
$data = json_decode($contents, flags: JSON_THROW_ON_ERROR);
$storage = $this->getUserStorage($user);
if ($data === null) {
unset($storage[$key]);
} else {
$storage[$key] = $contents;
}
$this->setUserStorage($user, $storage);
return $this->getStorageKeys($storage);
} catch (Throwable) {
ExceptionFactory::validation()->throw();
}
}

public function get(User $user, string $key): JsonResponse
{
try {
$storage = $this->getUserStorage($user);
return ($key === '') ? $this->getStorageKeys($storage)
: $this->getStorageByKey($storage, $key);
} catch (Throwable) {
FatalExceptionFactory::unexpected()->throw();
}
}
}
27 changes: 27 additions & 0 deletions database/migrations/2023_12_01_224508_users_storage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration {
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('users', function (Blueprint $table) {
$table->mediumText('storage')->nullable();
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('users', function (Blueprint $table) {
$table->dropColumn('storage');
});
}
};
2 changes: 2 additions & 0 deletions routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
Route::get('/status/{facility?}', [UserController::class, 'status']);
Route::match(['get', 'post'], '/logout', [UserController::class, 'logout']);
Route::post('/password', [UserController::class, 'password']);
Route::put('/storage/{key}', [UserController::class, 'storagePut']);
Route::get('/storage/{key?}', [UserController::class, 'storageGet']);
});
Route::prefix('/admin')->group(function () {
Route::get('/migrate/{hash?}', [AdminController::class, 'migrate']);
Expand Down