feat: /api/users/private/trash/*

This commit is contained in:
b1ek 2024-08-30 14:16:16 +10:00
parent 01269f44cf
commit a0332df101
Signed by: blek
GPG Key ID: 14546221E3595D0C
7 changed files with 169 additions and 0 deletions

View File

@ -0,0 +1,48 @@
<?php
namespace App\Http\Controllers;
use App\Http\Requests\AuthorizedRequest;
use App\Http\Requests\TrashGroupRequest;
use App\Services\UserTrashService;
class TrashUserController extends Controller
{
public function __construct(
private UserTrashService $userTrashService
) {
}
public function addMultiple(TrashGroupRequest $request)
{
$ids = $request->getValidatedIds();
$ret = $this->userTrashService->moveUsersToTrash($ids);
if (is_array($ret)) {
return response()->json($ret, 404);
}
}
public function deleteMultiple(TrashGroupRequest $request)
{
$ids = $request->getValidatedIds();
$ret = $this->userTrashService->moveUsersFromTrash($ids);
if (is_array($ret)) {
return response()->json($ret, 404);
}
}
public function cleanMultiple(TrashGroupRequest $request)
{
$ids = $request->getValidatedIds();
$ret = $this->userTrashService->cleanUsersFromTrash($ids);
if (is_array($ret)) {
return response()->json($ret, 404);
}
}
public function list(AuthorizedRequest $request)
{
return $this->userTrashService->listUsersInTrash();
}
}

View File

@ -0,0 +1,23 @@
<?php
namespace App\Http\Requests;
use Illuminate\Http\Exceptions\HttpResponseException;
class TrashGroupRequest extends AuthorizedRequest
{
public function getValidatedIds(): array
{
$ids = explode(',', $this->query('ids'));
$validator = validator([
'ids' => $ids
], [
'ids' => 'required|array|min:1',
'ids.*' => 'required|uuid|distinct'
]);
if ($validator->fails()) {
throw new HttpResponseException(response()->json($validator->errors(), 422));
}
return $ids;
}
}

View File

@ -48,6 +48,7 @@ class User extends Authenticatable
{ {
return [ return [
'email_verified_at' => 'datetime', 'email_verified_at' => 'datetime',
'deleted_at' => 'datetime',
'password' => 'hashed', 'password' => 'hashed',
]; ];
} }

View File

@ -82,4 +82,18 @@ class UserService
$user->save(); $user->save();
return $user; return $user;
} }
public function getUnexistingIds(array $ids): null | array
{
$users = User::whereIn('id', $ids)->get();
if ($users->count() != count($ids)) {
$existingIds = $users->map(
function($model) {
return $model->id;
}
)->toArray();
return array_diff($ids, $existingIds);
}
return null;
}
} }

View File

@ -0,0 +1,48 @@
<?php
namespace App\Services;
use App\Models\User;
class UserTrashService
{
public function __construct(
private UserService $userService
) {
}
public function moveUsersToTrash(array $ids): bool | array
{
$unexisting = $this->userService->getUnexistingIds($ids);
if (is_array($unexisting)) {
return $unexisting;
}
return User::whereIn('id', $ids)->update([ 'deleted_at' => now()->toDateTimeImmutable() ]);
}
public function moveUsersFromTrash(array $ids): bool | array
{
$unexisting = $this->userService->getUnexistingIds($ids);
if (is_array($unexisting)) {
return $unexisting;
}
return User::whereIn('id', $ids)->update([ 'deleted_at' => null ]);
}
public function cleanUsersFromTrash(array $ids): bool | null | array
{
$unexisting = $this->userService->getUnexistingIds($ids);
if (is_array($unexisting)) {
return $unexisting;
}
return User::whereIn('id', $ids)->whereNotNull('deleted_at')->delete();
}
public function listUsersInTrash(): array
{
return User::whereNotNull('deleted_at')->get()->toArray();
}
}

View File

@ -0,0 +1,28 @@
<?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->timestamp('deleted_at')->nullable();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('users', function (Blueprint $table) {
$table->dropColumn('deleted_at');
});
}
};

View File

@ -2,6 +2,7 @@
use App\Http\Controllers\PrivateUserController; use App\Http\Controllers\PrivateUserController;
use App\Http\Controllers\PublicUserController; use App\Http\Controllers\PublicUserController;
use App\Http\Controllers\TrashUserController;
use Illuminate\Support\Facades\Route; use Illuminate\Support\Facades\Route;
Route::get('/', function() { Route::get('/', function() {
@ -19,6 +20,12 @@ Route::prefix('/api')->group(function() {
Route::get('/get/{id}', 'get')->whereUuid('id'); Route::get('/get/{id}', 'get')->whereUuid('id');
Route::put('/edit/{id}', 'edit')->whereUuid('id'); Route::put('/edit/{id}', 'edit')->whereUuid('id');
}); });
Route::controller(TrashUserController::class)->prefix('/users/private/trash')->group(function () {
Route::put('/group', 'addMultiple');
Route::delete('/group', 'deleteMultiple');
Route::delete('/clean', 'cleanMultiple');
Route::get('/list', 'list');
});
}); });
Route::get('/session', function() { Route::get('/session', function() {