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->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
items:
$ref: '#/components/schemas/User'
401:
description: Auth failed
403:
description: Auth failed
/api/users/private/get/{id}:
get:
parameters:
@ -148,6 +152,7 @@ paths:
required: true
schema:
type: string
description: Must be a valid UUID
tags:
- Private routes
security:
@ -167,6 +172,10 @@ paths:
$ref: '#/components/schemas/ValidationError'
404:
description: User not found
401:
description: Auth failed
403:
description: Auth failed
/api/users/private/edit/{id}:
put:
parameters:
@ -175,6 +184,7 @@ paths:
required: true
schema:
type: string
description: Must be a valid UUID
tags:
- Private routes
security:
@ -188,6 +198,10 @@ paths:
application/json:
schema:
$ref: '#/components/schemas/ValidationError'
401:
description: Auth failed
403:
description: Auth failed
requestBody:
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
@ -219,6 +233,7 @@ paths:
schema:
type: string
example: 'comma,separated,values'
description: All must be valid UUIDs
responses:
200:
description: OK
@ -237,6 +252,10 @@ paths:
application/json:
schema:
$ref: '#/components/schemas/ValidationError'
401:
description: Auth failed
403:
description: Auth failed
delete:
summary: Remove user(s) from trash
tags:
@ -250,6 +269,7 @@ paths:
schema:
type: string
example: 'comma,separated,values'
description: All must be valid UUIDs
responses:
200:
description: |-
@ -271,6 +291,10 @@ paths:
application/json:
schema:
$ref: '#/components/schemas/ValidationError'
401:
description: Auth failed
403:
description: Auth failed
/api/users/private/trash/clean:
delete:
summary: Delete user(s) for good from trash
@ -285,6 +309,7 @@ paths:
schema:
type: string
example: 'comma,separated,values'
description: All must be valid UUIDs
responses:
200:
description: OK
@ -302,6 +327,10 @@ paths:
application/json:
schema:
$ref: '#/components/schemas/ValidationError'
401:
description: Auth failed
403:
description: Auth failed
/api/users/private/trash/list:
get:
summary: List users in trash
@ -318,6 +347,10 @@ paths:
type: array
items:
$ref: '#/components/schemas/User'
401:
description: Auth failed
403:
description: Auth failed
components:
schemas:
User:

View File

@ -1,5 +1,6 @@
<?php
use App\Http\Controllers\PrivateUserController;
use App\Http\Controllers\PublicUserController;
use Illuminate\Support\Facades\Route;
@ -13,6 +14,10 @@ Route::prefix('/api')->group(function() {
Route::post('/login', 'login');
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() {