refactor: with pint
This commit is contained in:
parent
9922ebbc39
commit
c67c96cf92
|
@ -5,6 +5,8 @@ namespace App\Facade\Filters;
|
||||||
class Filter
|
class Filter
|
||||||
{
|
{
|
||||||
public FilterTypeEnum $type;
|
public FilterTypeEnum $type;
|
||||||
|
|
||||||
public string $column;
|
public string $column;
|
||||||
|
|
||||||
public mixed $filter;
|
public mixed $filter;
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,11 +2,9 @@
|
||||||
|
|
||||||
namespace App\Facade\Filters;
|
namespace App\Facade\Filters;
|
||||||
|
|
||||||
use App\Facade\Filters\Pagination;
|
use Error;
|
||||||
use Illuminate\Database\Eloquent\Builder;
|
use Illuminate\Database\Eloquent\Builder;
|
||||||
use Illuminate\Http\Exceptions\HttpResponseException;
|
use Illuminate\Http\Exceptions\HttpResponseException;
|
||||||
|
|
||||||
use Error;
|
|
||||||
use JsonMapper;
|
use JsonMapper;
|
||||||
|
|
||||||
class Filters
|
class Filters
|
||||||
|
@ -25,7 +23,7 @@ class Filters
|
||||||
|
|
||||||
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)) {
|
if (is_array($data)) {
|
||||||
$data = json_decode(json_encode($data), false);
|
$data = json_decode(json_encode($data), false);
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,5 +5,6 @@ namespace App\Facade\Filters;
|
||||||
class Order
|
class Order
|
||||||
{
|
{
|
||||||
public string $by;
|
public string $by;
|
||||||
|
|
||||||
public OrderTypeEnum $sort;
|
public OrderTypeEnum $sort;
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,5 +5,6 @@ namespace App\Facade\Filters;
|
||||||
class Pagination
|
class Pagination
|
||||||
{
|
{
|
||||||
public int $size;
|
public int $size;
|
||||||
|
|
||||||
public int $page;
|
public int $page;
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,8 +11,7 @@ class PrivateUserController extends Controller
|
||||||
{
|
{
|
||||||
public function __construct(
|
public function __construct(
|
||||||
private UserService $userService
|
private UserService $userService
|
||||||
) {
|
) {}
|
||||||
}
|
|
||||||
|
|
||||||
public function list(AuthorizedRequest $request)
|
public function list(AuthorizedRequest $request)
|
||||||
{
|
{
|
||||||
|
@ -22,6 +21,7 @@ class PrivateUserController extends Controller
|
||||||
public function listFilters(AuthorizedRequest $request)
|
public function listFilters(AuthorizedRequest $request)
|
||||||
{
|
{
|
||||||
$filters = Filters::fromArrayOrObject($request->all());
|
$filters = Filters::fromArrayOrObject($request->all());
|
||||||
|
|
||||||
return $this->userService->listAll($filters);
|
return $this->userService->listAll($filters);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -10,8 +10,7 @@ class PublicUserController extends Controller
|
||||||
{
|
{
|
||||||
public function __construct(
|
public function __construct(
|
||||||
private UserService $userService
|
private UserService $userService
|
||||||
) {
|
) {}
|
||||||
}
|
|
||||||
|
|
||||||
public function register(RegisterRequest $request)
|
public function register(RegisterRequest $request)
|
||||||
{
|
{
|
||||||
|
|
|
@ -2,7 +2,6 @@
|
||||||
|
|
||||||
namespace App\Http\Controllers;
|
namespace App\Http\Controllers;
|
||||||
|
|
||||||
use App\Http\Requests\AuthorizedRequest;
|
|
||||||
use App\Http\Requests\TrashGroupRequest;
|
use App\Http\Requests\TrashGroupRequest;
|
||||||
use App\Services\UserTrashService;
|
use App\Services\UserTrashService;
|
||||||
|
|
||||||
|
@ -10,9 +9,7 @@ class TrashUserController extends Controller
|
||||||
{
|
{
|
||||||
public function __construct(
|
public function __construct(
|
||||||
private UserTrashService $userTrashService
|
private UserTrashService $userTrashService
|
||||||
) {
|
) {}
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public function addMultiple(TrashGroupRequest $request)
|
public function addMultiple(TrashGroupRequest $request)
|
||||||
{
|
{
|
||||||
|
|
|
@ -3,7 +3,6 @@
|
||||||
namespace App\Http\Requests;
|
namespace App\Http\Requests;
|
||||||
|
|
||||||
use App\Rules\ZxcvbnRule;
|
use App\Rules\ZxcvbnRule;
|
||||||
use Illuminate\Foundation\Http\FormRequest;
|
|
||||||
use Illuminate\Validation\Rules\Password;
|
use Illuminate\Validation\Rules\Password;
|
||||||
|
|
||||||
class LoginRequest extends RestRequest
|
class LoginRequest extends RestRequest
|
||||||
|
@ -25,7 +24,7 @@ class LoginRequest extends RestRequest
|
||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
'email' => ['required', 'email'],
|
'email' => ['required', 'email'],
|
||||||
'password' => ['required', Password::min(1)->rules([ new ZxcvbnRule() ])],
|
'password' => ['required', Password::min(1)->rules([new ZxcvbnRule])],
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,7 +4,6 @@ namespace App\Http\Requests;
|
||||||
|
|
||||||
use App\Models\User;
|
use App\Models\User;
|
||||||
use App\Rules\ZxcvbnRule;
|
use App\Rules\ZxcvbnRule;
|
||||||
use Illuminate\Foundation\Http\FormRequest;
|
|
||||||
use Illuminate\Validation\Rules\Password;
|
use Illuminate\Validation\Rules\Password;
|
||||||
|
|
||||||
class RegisterRequest extends RestRequest
|
class RegisterRequest extends RestRequest
|
||||||
|
@ -30,7 +29,7 @@ class RegisterRequest extends RestRequest
|
||||||
'middle_name' => ['required', 'string'],
|
'middle_name' => ['required', 'string'],
|
||||||
'email' => ['required', 'email'],
|
'email' => ['required', 'email'],
|
||||||
'phone' => ['required', 'regex:'.User::PHONE_REGEX],
|
'phone' => ['required', 'regex:'.User::PHONE_REGEX],
|
||||||
'password' => ['required', Password::min(1)->rules([ new ZxcvbnRule() ])],
|
'password' => ['required', Password::min(1)->rules([new ZxcvbnRule])],
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,14 +10,15 @@ class TrashGroupRequest extends AuthorizedRequest
|
||||||
{
|
{
|
||||||
$ids = explode(',', $this->query('ids'));
|
$ids = explode(',', $this->query('ids'));
|
||||||
$validator = validator([
|
$validator = validator([
|
||||||
'ids' => $ids
|
'ids' => $ids,
|
||||||
], [
|
], [
|
||||||
'ids' => 'required|array|min:1',
|
'ids' => 'required|array|min:1',
|
||||||
'ids.*' => 'required|uuid|distinct'
|
'ids.*' => 'required|uuid|distinct',
|
||||||
]);
|
]);
|
||||||
if ($validator->fails()) {
|
if ($validator->fails()) {
|
||||||
throw new HttpResponseException(response()->json($validator->errors(), 422));
|
throw new HttpResponseException(response()->json($validator->errors(), 422));
|
||||||
}
|
}
|
||||||
|
|
||||||
return $ids;
|
return $ids;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,7 +4,6 @@ namespace App\Http\Requests;
|
||||||
|
|
||||||
use App\Models\User;
|
use App\Models\User;
|
||||||
use App\Rules\ZxcvbnRule;
|
use App\Rules\ZxcvbnRule;
|
||||||
use Illuminate\Foundation\Http\FormRequest;
|
|
||||||
use Illuminate\Validation\Rules\Password;
|
use Illuminate\Validation\Rules\Password;
|
||||||
|
|
||||||
class UserEditRequest extends AuthorizedRequest
|
class UserEditRequest extends AuthorizedRequest
|
||||||
|
@ -23,7 +22,7 @@ class UserEditRequest extends AuthorizedRequest
|
||||||
'user.email' => ['email'],
|
'user.email' => ['email'],
|
||||||
'user.phone' => ['string', 'regex:'.User::PHONE_REGEX],
|
'user.phone' => ['string', 'regex:'.User::PHONE_REGEX],
|
||||||
'user' => 'required',
|
'user' => 'required',
|
||||||
'password' => [ Password::min(1)->rules([ new ZxcvbnRule() ]) ],
|
'password' => [Password::min(1)->rules([new ZxcvbnRule])],
|
||||||
|
|
||||||
'user.email_verified_at' => 'prohibited',
|
'user.email_verified_at' => 'prohibited',
|
||||||
'user.created_at' => 'prohibited',
|
'user.created_at' => 'prohibited',
|
||||||
|
|
|
@ -12,9 +12,9 @@ use Illuminate\Notifications\Notifiable;
|
||||||
class User extends Authenticatable
|
class User extends Authenticatable
|
||||||
{
|
{
|
||||||
use HasFactory;
|
use HasFactory;
|
||||||
|
use HasHistory;
|
||||||
use Notifiable;
|
use Notifiable;
|
||||||
use UuidId;
|
use UuidId;
|
||||||
use HasHistory;
|
|
||||||
|
|
||||||
public const PHONE_REGEX = '/^\+\d+$/';
|
public const PHONE_REGEX = '/^\+\d+$/';
|
||||||
|
|
||||||
|
|
|
@ -16,7 +16,7 @@ class ZxcvbnRule implements ValidationRule
|
||||||
public function validate(string $attribute, mixed $value, Closure $fail): void
|
public function validate(string $attribute, mixed $value, Closure $fail): void
|
||||||
{
|
{
|
||||||
$value = (string) $value;
|
$value = (string) $value;
|
||||||
$zxcvbn = new Zxcvbn();
|
$zxcvbn = new Zxcvbn;
|
||||||
if ($zxcvbn->passwordStrength($value)['score'] < 3) {
|
if ($zxcvbn->passwordStrength($value)['score'] < 3) {
|
||||||
$fail('Password is not secure enough!');
|
$fail('Password is not secure enough!');
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,8 +12,8 @@ class UserService
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* Create(register) new user
|
* Create(register) new 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 create($data, $autoLogin = true): User
|
public function create($data, $autoLogin = true): User
|
||||||
{
|
{
|
||||||
|
@ -27,11 +27,13 @@ class UserService
|
||||||
if ($autoLogin) {
|
if ($autoLogin) {
|
||||||
Auth::login($user);
|
Auth::login($user);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $user;
|
return $user;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Login to 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
|
* @return User
|
||||||
*/
|
*/
|
||||||
|
@ -39,8 +41,10 @@ class UserService
|
||||||
{
|
{
|
||||||
if (Auth::attempt($data)) {
|
if (Auth::attempt($data)) {
|
||||||
request()->session()->regenerate();
|
request()->session()->regenerate();
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -62,10 +66,11 @@ class UserService
|
||||||
}
|
}
|
||||||
$builder = User::query();
|
$builder = User::query();
|
||||||
$filters->apply($builder);
|
$filters->apply($builder);
|
||||||
|
|
||||||
return $builder->get()->toArray();
|
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();
|
||||||
}
|
}
|
||||||
|
@ -73,7 +78,7 @@ class UserService
|
||||||
/**
|
/**
|
||||||
* Will return `null` if failed
|
* 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);
|
$user = $this->getOneById($id);
|
||||||
if ($user === null) {
|
if ($user === null) {
|
||||||
|
@ -86,10 +91,11 @@ class UserService
|
||||||
|
|
||||||
$user->fill($data['user']);
|
$user->fill($data['user']);
|
||||||
$user->save();
|
$user->save();
|
||||||
|
|
||||||
return $user;
|
return $user;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getUnexistingIds(array $ids): null | array
|
public function getUnexistingIds(array $ids): ?array
|
||||||
{
|
{
|
||||||
$users = User::whereIn('id', $ids)->get();
|
$users = User::whereIn('id', $ids)->get();
|
||||||
if ($users->count() != count($ids)) {
|
if ($users->count() != count($ids)) {
|
||||||
|
@ -98,8 +104,10 @@ class UserService
|
||||||
return $model->id;
|
return $model->id;
|
||||||
}
|
}
|
||||||
)->toArray();
|
)->toArray();
|
||||||
|
|
||||||
return array_diff($ids, $existingIds);
|
return array_diff($ids, $existingIds);
|
||||||
}
|
}
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,9 +8,7 @@ class UserTrashService
|
||||||
{
|
{
|
||||||
public function __construct(
|
public function __construct(
|
||||||
private UserService $userService
|
private UserService $userService
|
||||||
) {
|
) {}
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public function moveUsersToTrash(array $ids): bool|array
|
public function moveUsersToTrash(array $ids): bool|array
|
||||||
{
|
{
|
||||||
|
|
|
@ -13,10 +13,12 @@ trait UuidId
|
||||||
$model->id = Uuid::uuid6()->toString();
|
$model->id = Uuid::uuid6()->toString();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getIncrementing(): bool
|
public function getIncrementing(): bool
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getKeyType(): string
|
public function getKeyType(): string
|
||||||
{
|
{
|
||||||
return 'string';
|
return 'string';
|
||||||
|
|
|
@ -30,7 +30,7 @@ class UserFactory extends Factory
|
||||||
'phone' => fake()->unique()->phoneNumber,
|
'phone' => fake()->unique()->phoneNumber,
|
||||||
'email' => fake()->unique()->safeEmail,
|
'email' => fake()->unique()->safeEmail,
|
||||||
'password' => Hash::make('password'),
|
'password' => Hash::make('password'),
|
||||||
'remember_token' => Str::random()
|
'remember_token' => Str::random(),
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -5,7 +5,8 @@ use Illuminate\Database\Schema\Blueprint;
|
||||||
use Illuminate\Database\Schema\Builder;
|
use Illuminate\Database\Schema\Builder;
|
||||||
use Illuminate\Support\Facades\Schema;
|
use Illuminate\Support\Facades\Schema;
|
||||||
|
|
||||||
return new class () extends Migration {
|
return new class extends Migration
|
||||||
|
{
|
||||||
/**
|
/**
|
||||||
* Run the migrations.
|
* Run the migrations.
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -4,7 +4,8 @@ use Illuminate\Database\Migrations\Migration;
|
||||||
use Illuminate\Database\Schema\Blueprint;
|
use Illuminate\Database\Schema\Blueprint;
|
||||||
use Illuminate\Support\Facades\Schema;
|
use Illuminate\Support\Facades\Schema;
|
||||||
|
|
||||||
return new class () extends Migration {
|
return new class extends Migration
|
||||||
|
{
|
||||||
/**
|
/**
|
||||||
* Run the migrations.
|
* Run the migrations.
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -4,7 +4,8 @@ use Illuminate\Database\Migrations\Migration;
|
||||||
use Illuminate\Database\Schema\Blueprint;
|
use Illuminate\Database\Schema\Blueprint;
|
||||||
use Illuminate\Support\Facades\Schema;
|
use Illuminate\Support\Facades\Schema;
|
||||||
|
|
||||||
return new class () extends Migration {
|
return new class extends Migration
|
||||||
|
{
|
||||||
/**
|
/**
|
||||||
* Run the migrations.
|
* Run the migrations.
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -4,7 +4,8 @@ use Illuminate\Database\Migrations\Migration;
|
||||||
use Illuminate\Database\Schema\Blueprint;
|
use Illuminate\Database\Schema\Blueprint;
|
||||||
use Illuminate\Support\Facades\Schema;
|
use Illuminate\Support\Facades\Schema;
|
||||||
|
|
||||||
return new class () extends Migration {
|
return new class extends Migration
|
||||||
|
{
|
||||||
/**
|
/**
|
||||||
* Run the migrations.
|
* Run the migrations.
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -4,7 +4,8 @@ use Illuminate\Database\Migrations\Migration;
|
||||||
use Illuminate\Database\Schema\Blueprint;
|
use Illuminate\Database\Schema\Blueprint;
|
||||||
use Illuminate\Support\Facades\Schema;
|
use Illuminate\Support\Facades\Schema;
|
||||||
|
|
||||||
return new class () extends Migration {
|
return new class extends Migration
|
||||||
|
{
|
||||||
/**
|
/**
|
||||||
* Run the migrations.
|
* Run the migrations.
|
||||||
*/
|
*/
|
||||||
|
|
Loading…
Reference in New Issue