diff --git a/app/Facade/Filters/Filter.php b/app/Facade/Filters/Filter.php index e98d0f3..5d48875 100644 --- a/app/Facade/Filters/Filter.php +++ b/app/Facade/Filters/Filter.php @@ -5,6 +5,8 @@ namespace App\Facade\Filters; class Filter { public FilterTypeEnum $type; + public string $column; + public mixed $filter; } diff --git a/app/Facade/Filters/Filters.php b/app/Facade/Filters/Filters.php index 323dc0f..2797311 100644 --- a/app/Facade/Filters/Filters.php +++ b/app/Facade/Filters/Filters.php @@ -2,11 +2,9 @@ namespace App\Facade\Filters; -use App\Facade\Filters\Pagination; +use Error; use Illuminate\Database\Eloquent\Builder; use Illuminate\Http\Exceptions\HttpResponseException; - -use Error; use JsonMapper; class Filters @@ -23,9 +21,9 @@ class Filters public ?Pagination $pagination; - public static function fromArrayOrObject(array | object $data): Filters + public static function fromArrayOrObject(array|object $data): Filters { - $mapper = new JsonMapper(); + $mapper = new JsonMapper; if (is_array($data)) { $data = json_decode(json_encode($data), false); } @@ -37,7 +35,7 @@ class Filters try { return $mapper->map($data, \App\Facade\Filters\Filters::class); } catch (Error $err) { - throw new HttpResponseException(response()->json([ 'unknown' => $err->getMessage() ], 422)); + throw new HttpResponseException(response()->json(['unknown' => $err->getMessage()], 422)); } } @@ -61,7 +59,7 @@ class Filters $builder->orderBy($order->by, $order->sort->value); } - if (!is_null($this->pagination)) { + if (! is_null($this->pagination)) { $builder->paginate($this->pagination->size, ['*'], 'page', $this->pagination->page); } } diff --git a/app/Facade/Filters/Order.php b/app/Facade/Filters/Order.php index f28a742..440e411 100644 --- a/app/Facade/Filters/Order.php +++ b/app/Facade/Filters/Order.php @@ -5,5 +5,6 @@ namespace App\Facade\Filters; class Order { public string $by; + public OrderTypeEnum $sort; } diff --git a/app/Facade/Filters/Pagination.php b/app/Facade/Filters/Pagination.php index 6f249c2..8dc34e3 100644 --- a/app/Facade/Filters/Pagination.php +++ b/app/Facade/Filters/Pagination.php @@ -5,5 +5,6 @@ namespace App\Facade\Filters; class Pagination { public int $size; + public int $page; } diff --git a/app/Http/Controllers/PrivateUserController.php b/app/Http/Controllers/PrivateUserController.php index 2f8dd60..6ef9d89 100644 --- a/app/Http/Controllers/PrivateUserController.php +++ b/app/Http/Controllers/PrivateUserController.php @@ -11,8 +11,7 @@ class PrivateUserController extends Controller { public function __construct( private UserService $userService - ) { - } + ) {} public function list(AuthorizedRequest $request) { @@ -22,6 +21,7 @@ class PrivateUserController extends Controller public function listFilters(AuthorizedRequest $request) { $filters = Filters::fromArrayOrObject($request->all()); + return $this->userService->listAll($filters); } diff --git a/app/Http/Controllers/PublicUserController.php b/app/Http/Controllers/PublicUserController.php index cd8ed1b..63adc70 100755 --- a/app/Http/Controllers/PublicUserController.php +++ b/app/Http/Controllers/PublicUserController.php @@ -10,8 +10,7 @@ class PublicUserController extends Controller { public function __construct( private UserService $userService - ) { - } + ) {} public function register(RegisterRequest $request) { @@ -20,7 +19,7 @@ class PublicUserController extends Controller public function login(LoginRequest $request) { - if (!$this->userService->login($request->all())) { + if (! $this->userService->login($request->all())) { return response() ->json('bad_password', 400); } diff --git a/app/Http/Controllers/TrashUserController.php b/app/Http/Controllers/TrashUserController.php index a336f60..bf237b4 100644 --- a/app/Http/Controllers/TrashUserController.php +++ b/app/Http/Controllers/TrashUserController.php @@ -2,7 +2,6 @@ namespace App\Http\Controllers; -use App\Http\Requests\AuthorizedRequest; use App\Http\Requests\TrashGroupRequest; use App\Services\UserTrashService; @@ -10,9 +9,7 @@ class TrashUserController extends Controller { public function __construct( private UserTrashService $userTrashService - ) { - - } + ) {} public function addMultiple(TrashGroupRequest $request) { diff --git a/app/Http/Requests/LoginRequest.php b/app/Http/Requests/LoginRequest.php index 78224cb..09038be 100644 --- a/app/Http/Requests/LoginRequest.php +++ b/app/Http/Requests/LoginRequest.php @@ -3,7 +3,6 @@ namespace App\Http\Requests; use App\Rules\ZxcvbnRule; -use Illuminate\Foundation\Http\FormRequest; use Illuminate\Validation\Rules\Password; class LoginRequest extends RestRequest @@ -25,7 +24,7 @@ class LoginRequest extends RestRequest { return [ 'email' => ['required', 'email'], - 'password' => ['required', Password::min(1)->rules([ new ZxcvbnRule() ])], + 'password' => ['required', Password::min(1)->rules([new ZxcvbnRule])], ]; } } diff --git a/app/Http/Requests/RegisterRequest.php b/app/Http/Requests/RegisterRequest.php index daf5e6b..553b249 100644 --- a/app/Http/Requests/RegisterRequest.php +++ b/app/Http/Requests/RegisterRequest.php @@ -4,7 +4,6 @@ namespace App\Http\Requests; use App\Models\User; use App\Rules\ZxcvbnRule; -use Illuminate\Foundation\Http\FormRequest; use Illuminate\Validation\Rules\Password; class RegisterRequest extends RestRequest @@ -29,8 +28,8 @@ class RegisterRequest extends RestRequest 'name' => ['required', 'string'], 'middle_name' => ['required', 'string'], 'email' => ['required', 'email'], - 'phone' => ['required', 'regex:' . User::PHONE_REGEX ], - 'password' => ['required', Password::min(1)->rules([ new ZxcvbnRule() ])], + 'phone' => ['required', 'regex:'.User::PHONE_REGEX], + 'password' => ['required', Password::min(1)->rules([new ZxcvbnRule])], ]; } } diff --git a/app/Http/Requests/TrashGroupRequest.php b/app/Http/Requests/TrashGroupRequest.php index 1997dc5..ec76f13 100644 --- a/app/Http/Requests/TrashGroupRequest.php +++ b/app/Http/Requests/TrashGroupRequest.php @@ -10,14 +10,15 @@ class TrashGroupRequest extends AuthorizedRequest { $ids = explode(',', $this->query('ids')); $validator = validator([ - 'ids' => $ids + 'ids' => $ids, ], [ 'ids' => 'required|array|min:1', - 'ids.*' => 'required|uuid|distinct' + 'ids.*' => 'required|uuid|distinct', ]); if ($validator->fails()) { throw new HttpResponseException(response()->json($validator->errors(), 422)); } + return $ids; } } diff --git a/app/Http/Requests/UserEditRequest.php b/app/Http/Requests/UserEditRequest.php index 77f9d00..02952ba 100644 --- a/app/Http/Requests/UserEditRequest.php +++ b/app/Http/Requests/UserEditRequest.php @@ -4,7 +4,6 @@ namespace App\Http\Requests; use App\Models\User; use App\Rules\ZxcvbnRule; -use Illuminate\Foundation\Http\FormRequest; use Illuminate\Validation\Rules\Password; class UserEditRequest extends AuthorizedRequest @@ -17,13 +16,13 @@ class UserEditRequest extends AuthorizedRequest public function rules(): array { return [ - 'user.last_name' => [ 'string' ], - 'user.name' => [ 'string' ], - 'user.middle_name' => [ 'string' ], - 'user.email' => [ 'email' ], - 'user.phone' => [ 'string', 'regex:' . User::PHONE_REGEX ], + 'user.last_name' => ['string'], + 'user.name' => ['string'], + 'user.middle_name' => ['string'], + 'user.email' => ['email'], + 'user.phone' => ['string', 'regex:'.User::PHONE_REGEX], 'user' => 'required', - 'password' => [ Password::min(1)->rules([ new ZxcvbnRule() ]) ], + 'password' => [Password::min(1)->rules([new ZxcvbnRule])], 'user.email_verified_at' => 'prohibited', 'user.created_at' => 'prohibited', diff --git a/app/Models/User.php b/app/Models/User.php index ca0e15e..5d1718a 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -12,9 +12,9 @@ use Illuminate\Notifications\Notifiable; class User extends Authenticatable { use HasFactory; + use HasHistory; use Notifiable; use UuidId; - use HasHistory; public const PHONE_REGEX = '/^\+\d+$/'; diff --git a/app/Rules/ZxcvbnRule.php b/app/Rules/ZxcvbnRule.php index f468c93..779adfe 100644 --- a/app/Rules/ZxcvbnRule.php +++ b/app/Rules/ZxcvbnRule.php @@ -16,7 +16,7 @@ class ZxcvbnRule implements ValidationRule public function validate(string $attribute, mixed $value, Closure $fail): void { $value = (string) $value; - $zxcvbn = new Zxcvbn(); + $zxcvbn = new Zxcvbn; if ($zxcvbn->passwordStrength($value)['score'] < 3) { $fail('Password is not secure enough!'); } diff --git a/app/Services/UserService.php b/app/Services/UserService.php index 596f57f..c82de9c 100644 --- a/app/Services/UserService.php +++ b/app/Services/UserService.php @@ -12,12 +12,12 @@ class UserService { /** * Create(register) new user - * @param array $data This is expected to be validated already - * @return User + * + * @param array $data This is expected to be validated already */ public function create($data, $autoLogin = true): User { - if (User::where([ 'email' => $data['email'] ])->count() !== 0) { + if (User::where(['email' => $data['email']])->count() !== 0) { throw new HttpResponseException(response()->json('email_taken', 400)); } @@ -27,26 +27,30 @@ class UserService if ($autoLogin) { Auth::login($user); } + return $user; } /** * Login to user - * @param array $data This is expected to be validated already + * + * @param array $data This is expected to be validated already * @return User */ public function login($data): bool { if (Auth::attempt($data)) { request()->session()->regenerate(); + return true; } + return false; } public function setPassword($data) { - $user = User::where([ 'email' => $data['email'] ])->first(); + $user = User::where(['email' => $data['email']])->first(); if ($user === null) { return; } @@ -62,18 +66,19 @@ class UserService } $builder = User::query(); $filters->apply($builder); + return $builder->get()->toArray(); } - public function getOneById(string $id): User | null + public function getOneById(string $id): ?User { - return User::where([ 'id' => $id ])->first(); + return User::where(['id' => $id])->first(); } /** * Will return `null` if failed */ - public function editUser(array $data, string $id): User | null + public function editUser(array $data, string $id): ?User { $user = $this->getOneById($id); if ($user === null) { @@ -81,15 +86,16 @@ class UserService } if (array_key_exists('password', $data)) { - $this->setPassword([ 'email' => $user['email'], 'password' => $data['password'] ]); + $this->setPassword(['email' => $user['email'], 'password' => $data['password']]); } $user->fill($data['user']); $user->save(); + return $user; } - public function getUnexistingIds(array $ids): null | array + public function getUnexistingIds(array $ids): ?array { $users = User::whereIn('id', $ids)->get(); if ($users->count() != count($ids)) { @@ -98,8 +104,10 @@ class UserService return $model->id; } )->toArray(); + return array_diff($ids, $existingIds); } + return null; } } diff --git a/app/Services/UserTrashService.php b/app/Services/UserTrashService.php index 0d0c06f..f35392a 100644 --- a/app/Services/UserTrashService.php +++ b/app/Services/UserTrashService.php @@ -8,31 +8,29 @@ class UserTrashService { public function __construct( private UserService $userService - ) { + ) {} - } - - public function moveUsersToTrash(array $ids): bool | array + 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() ]); + return User::whereIn('id', $ids)->update(['deleted_at' => now()->toDateTimeImmutable()]); } - public function moveUsersFromTrash(array $ids): bool | array + 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 ]); + return User::whereIn('id', $ids)->update(['deleted_at' => null]); } - public function cleanUsersFromTrash(array $ids): bool | null | array + public function cleanUsersFromTrash(array $ids): bool|null|array { $unexisting = $this->userService->getUnexistingIds($ids); if (is_array($unexisting)) { diff --git a/app/UuidId.php b/app/UuidId.php index 8c5b4e1..de7345f 100644 --- a/app/UuidId.php +++ b/app/UuidId.php @@ -13,10 +13,12 @@ trait UuidId $model->id = Uuid::uuid6()->toString(); }); } + public function getIncrementing(): bool { return false; } + public function getKeyType(): string { return 'string'; diff --git a/bootstrap/app.php b/bootstrap/app.php index 11cb734..96d381d 100644 --- a/bootstrap/app.php +++ b/bootstrap/app.php @@ -12,7 +12,7 @@ return Application::configure(basePath: dirname(__DIR__)) ) ->withMiddleware(function (Middleware $middleware) { // https://upload.wikimedia.org/wikipedia/commons/thumb/8/8a/Axillary_Crutches.jpg/90px-Axillary_Crutches.jpg - $middleware->validateCsrfTokens([ '*' ]); + $middleware->validateCsrfTokens(['*']); }) ->withExceptions(function (Exceptions $exceptions) { // diff --git a/database/factories/UserFactory.php b/database/factories/UserFactory.php index c311276..fa6a4c4 100644 --- a/database/factories/UserFactory.php +++ b/database/factories/UserFactory.php @@ -30,7 +30,7 @@ class UserFactory extends Factory 'phone' => fake()->unique()->phoneNumber, 'email' => fake()->unique()->safeEmail, 'password' => Hash::make('password'), - 'remember_token' => Str::random() + 'remember_token' => Str::random(), ]; } diff --git a/database/migrations/0001_01_01_000000_create_users_table.php b/database/migrations/0001_01_01_000000_create_users_table.php index b352529..8d0f353 100644 --- a/database/migrations/0001_01_01_000000_create_users_table.php +++ b/database/migrations/0001_01_01_000000_create_users_table.php @@ -5,7 +5,8 @@ use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Schema\Builder; use Illuminate\Support\Facades\Schema; -return new class () extends Migration { +return new class extends Migration +{ /** * Run the migrations. */ diff --git a/database/migrations/0001_01_01_000001_create_cache_table.php b/database/migrations/0001_01_01_000001_create_cache_table.php index 66cdb48..b9c106b 100644 --- a/database/migrations/0001_01_01_000001_create_cache_table.php +++ b/database/migrations/0001_01_01_000001_create_cache_table.php @@ -4,7 +4,8 @@ use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; -return new class () extends Migration { +return new class extends Migration +{ /** * Run the migrations. */ diff --git a/database/migrations/0001_01_01_000002_create_jobs_table.php b/database/migrations/0001_01_01_000002_create_jobs_table.php index ccf3ec1..425e705 100644 --- a/database/migrations/0001_01_01_000002_create_jobs_table.php +++ b/database/migrations/0001_01_01_000002_create_jobs_table.php @@ -4,7 +4,8 @@ use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; -return new class () extends Migration { +return new class extends Migration +{ /** * Run the migrations. */ diff --git a/database/migrations/2024_08_29_211556_create_histories_table.php b/database/migrations/2024_08_29_211556_create_histories_table.php index 4840791..2da15a8 100644 --- a/database/migrations/2024_08_29_211556_create_histories_table.php +++ b/database/migrations/2024_08_29_211556_create_histories_table.php @@ -4,7 +4,8 @@ use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; -return new class () extends Migration { +return new class extends Migration +{ /** * Run the migrations. */ @@ -16,7 +17,7 @@ return new class () extends Migration { $table->string('model_name', 250); $table->jsonb('before'); $table->jsonb('after'); - $table->enum('action', [ 'insert', 'update', 'delete' ]); + $table->enum('action', ['insert', 'update', 'delete']); $table->timestamps(); }); } diff --git a/database/migrations/2024_08_30_013415_add_deleted_at_to_users.php b/database/migrations/2024_08_30_013415_add_deleted_at_to_users.php index c18da86..53fe5ef 100644 --- a/database/migrations/2024_08_30_013415_add_deleted_at_to_users.php +++ b/database/migrations/2024_08_30_013415_add_deleted_at_to_users.php @@ -4,7 +4,8 @@ use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; -return new class () extends Migration { +return new class extends Migration +{ /** * Run the migrations. */