feat: implement /api/users/private/list and /get/{id}

This commit is contained in:
b1ek 2024-08-30 10:17:13 +10:00
parent f2c398248b
commit 6f77d8100f
Signed by: blek
GPG Key ID: 14546221E3595D0C
5 changed files with 89 additions and 0 deletions

View File

@ -0,0 +1,25 @@
<?php
namespace App\Http\Controllers;
use App\Http\Requests\AuthorizedRequest;
use App\Services\UserService;
use Illuminate\Http\Request;
use Validator;
class PrivateUserController extends Controller
{
public function __construct(
private UserService $userService
) { }
public function list(AuthorizedRequest $request)
{
return $this->userService->listAll();
}
public function get(AuthorizedRequest $request, string $id)
{
return $this->userService->getOneById($id);
}
}

View File

@ -0,0 +1,16 @@
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class AuthorizedRequest extends RestRequest
{
/**
* Determine if the user is authorized to make this request.
*/
public function authorize(): bool
{
return auth()->check();
}
}

View File

@ -53,4 +53,14 @@ class UserService
$user->password = Hash::make($data['password']); $user->password = Hash::make($data['password']);
$user->save(); $user->save();
} }
public function listAll(): array
{
return User::all()->toArray();
}
public function getOneById(string $id): User | null
{
return User::where([ 'id' => $id ])->first();
}
} }

View File

@ -140,6 +140,10 @@ paths:
type: array type: array
items: items:
$ref: '#/components/schemas/User' $ref: '#/components/schemas/User'
401:
description: Auth failed
403:
description: Auth failed
/api/users/private/get/{id}: /api/users/private/get/{id}:
get: get:
parameters: parameters:
@ -148,6 +152,7 @@ paths:
required: true required: true
schema: schema:
type: string type: string
description: Must be a valid UUID
tags: tags:
- Private routes - Private routes
security: security:
@ -167,6 +172,10 @@ paths:
$ref: '#/components/schemas/ValidationError' $ref: '#/components/schemas/ValidationError'
404: 404:
description: User not found description: User not found
401:
description: Auth failed
403:
description: Auth failed
/api/users/private/edit/{id}: /api/users/private/edit/{id}:
put: put:
parameters: parameters:
@ -175,6 +184,7 @@ paths:
required: true required: true
schema: schema:
type: string type: string
description: Must be a valid UUID
tags: tags:
- Private routes - Private routes
security: security:
@ -188,6 +198,10 @@ paths:
application/json: application/json:
schema: schema:
$ref: '#/components/schemas/ValidationError' $ref: '#/components/schemas/ValidationError'
401:
description: Auth failed
403:
description: Auth failed
requestBody: requestBody:
description: |- description: |-
All fields of `user` are required. The whole record will be updated with exactly what you provide here. It is assumed that you already have all information about the user beforehand All fields of `user` are required. The whole record will be updated with exactly what you provide here. It is assumed that you already have all information about the user beforehand
@ -219,6 +233,7 @@ paths:
schema: schema:
type: string type: string
example: 'comma,separated,values' example: 'comma,separated,values'
description: All must be valid UUIDs
responses: responses:
200: 200:
description: OK description: OK
@ -237,6 +252,10 @@ paths:
application/json: application/json:
schema: schema:
$ref: '#/components/schemas/ValidationError' $ref: '#/components/schemas/ValidationError'
401:
description: Auth failed
403:
description: Auth failed
delete: delete:
summary: Remove user(s) from trash summary: Remove user(s) from trash
tags: tags:
@ -250,6 +269,7 @@ paths:
schema: schema:
type: string type: string
example: 'comma,separated,values' example: 'comma,separated,values'
description: All must be valid UUIDs
responses: responses:
200: 200:
description: |- description: |-
@ -271,6 +291,10 @@ paths:
application/json: application/json:
schema: schema:
$ref: '#/components/schemas/ValidationError' $ref: '#/components/schemas/ValidationError'
401:
description: Auth failed
403:
description: Auth failed
/api/users/private/trash/clean: /api/users/private/trash/clean:
delete: delete:
summary: Delete user(s) for good from trash summary: Delete user(s) for good from trash
@ -285,6 +309,7 @@ paths:
schema: schema:
type: string type: string
example: 'comma,separated,values' example: 'comma,separated,values'
description: All must be valid UUIDs
responses: responses:
200: 200:
description: OK description: OK
@ -302,6 +327,10 @@ paths:
application/json: application/json:
schema: schema:
$ref: '#/components/schemas/ValidationError' $ref: '#/components/schemas/ValidationError'
401:
description: Auth failed
403:
description: Auth failed
/api/users/private/trash/list: /api/users/private/trash/list:
get: get:
summary: List users in trash summary: List users in trash
@ -318,6 +347,10 @@ paths:
type: array type: array
items: items:
$ref: '#/components/schemas/User' $ref: '#/components/schemas/User'
401:
description: Auth failed
403:
description: Auth failed
components: components:
schemas: schemas:
User: User:

View File

@ -1,5 +1,6 @@
<?php <?php
use App\Http\Controllers\PrivateUserController;
use App\Http\Controllers\PublicUserController; use App\Http\Controllers\PublicUserController;
use Illuminate\Support\Facades\Route; use Illuminate\Support\Facades\Route;
@ -13,6 +14,10 @@ Route::prefix('/api')->group(function() {
Route::post('/login', 'login'); Route::post('/login', 'login');
Route::post('/reset', 'reset'); Route::post('/reset', 'reset');
}); });
Route::controller(PrivateUserController::class)->prefix('/users/private')->group(function () {
Route::get('/list', 'list');
Route::get('/get/{id}', 'get')->whereUuid('id');
});
}); });
Route::get('/session', function() { Route::get('/session', function() {