This commit is contained in:
@@ -21,6 +21,7 @@ use App\DotsAPI\Fetcher\v2\AuthApiFetcher;
|
||||
use App\DotsAPI\Fetcher\v2\AuthApiSender;
|
||||
use App\Http\Resources\API\v2\CartItemCollection;
|
||||
|
||||
use App\Models\Validation\UserValidationByMatrixUsername;
|
||||
use App\Models\Validation\CityValidationByName;
|
||||
use App\Models\Validation\CompanyValidationByName;
|
||||
|
||||
@@ -117,10 +118,9 @@ class CartController extends Controller
|
||||
$matrixUsername = $request->input('matrixUsername') ?? '';
|
||||
$cityName = $request->input('cityName') ?? '';
|
||||
|
||||
// check for not valid user
|
||||
$validation = User::validateWithMatrixUsername($matrixUsername);
|
||||
if(array_key_exists('error', $validation))
|
||||
return response()->json($validation);
|
||||
$validator = new UserValidationByMatrixUsername($matrixUsername);
|
||||
if(!$validator->isValid())
|
||||
return response()->json($validator->getMessageMap());
|
||||
|
||||
$validator = new CityValidationByName($cityName);
|
||||
if(!$validator->isValid())
|
||||
@@ -161,10 +161,9 @@ class CartController extends Controller
|
||||
$matrixUsername = $request->input('matrixUsername') ?? '';
|
||||
$companyName = $request->input('companyName') ?? '';
|
||||
|
||||
// check for not valid user
|
||||
$validation = User::validateWithMatrixUsername($matrixUsername);
|
||||
if(array_key_exists('error', $validation))
|
||||
return response()->json($validation);
|
||||
$validator = new UserValidationByMatrixUsername($matrixUsername);
|
||||
if(!$validator->isValid())
|
||||
return response()->json($validator->getMessageMap());
|
||||
|
||||
$validator = new CompanyValidationByName($companyName);
|
||||
if(!$validator->isValid())
|
||||
@@ -203,10 +202,9 @@ class CartController extends Controller
|
||||
$itemName = $request->input('itemName') ?? '';
|
||||
$itemCount = $request->input('itemCount') ?? '';
|
||||
|
||||
// check for not valid user
|
||||
$validation = User::validateWithMatrixUsername($matrixUsername);
|
||||
if(array_key_exists('error', $validation))
|
||||
return response()->json($validation);
|
||||
$validator = new UserValidationByMatrixUsername($matrixUsername);
|
||||
if(!$validator->isValid())
|
||||
return response()->json($validator->getMessageMap());
|
||||
|
||||
// check for not valid item
|
||||
$validation = Item::validate_with_name($itemName);
|
||||
|
||||
+13
-17
@@ -8,7 +8,9 @@ use Illuminate\Foundation\Auth\User as Authenticatable;
|
||||
use Illuminate\Notifications\Notifiable;
|
||||
use Laravel\Sanctum\HasApiTokens;
|
||||
|
||||
class User extends Authenticatable
|
||||
use App\Models\Validation\ValidationByNameInterface;
|
||||
|
||||
class User extends Authenticatable implements ValidationByNameInterface
|
||||
{
|
||||
use HasApiTokens, HasFactory, Notifiable;
|
||||
|
||||
@@ -23,24 +25,18 @@ class User extends Authenticatable
|
||||
'phone',
|
||||
];
|
||||
|
||||
public static function validateWithMatrixUsername(string $matrixUsername)
|
||||
|
||||
|
||||
|
||||
public static function isExistByName(string $name): bool
|
||||
{
|
||||
$matrixUsername = $matrixUsername ?? '';
|
||||
$count = User::where('matrix_username', $name)->count();
|
||||
|
||||
if($matrixUsername == '')
|
||||
return [
|
||||
'error' => 'The username is empty, please, write username!!!'
|
||||
];
|
||||
return $count != 0;
|
||||
}
|
||||
|
||||
$user = User::where('matrix_username', $matrixUsername)->first();
|
||||
|
||||
if(!$user)
|
||||
return [
|
||||
'error' => 'A user with the username does not exist!!!'
|
||||
];
|
||||
|
||||
return [
|
||||
'ok' => 'A user with the username is valid.'
|
||||
];
|
||||
public static function isNameValid(string $name): bool
|
||||
{
|
||||
return $name != '';
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models\Validation\Messages;
|
||||
|
||||
use App\Models\Validation\Messages\BaseMessages;
|
||||
|
||||
class UserMessagesFactory
|
||||
{
|
||||
protected array $messages = [
|
||||
'found' => 'A user with the username is valid.',
|
||||
'not_found' => 'A user with the username does not exist!!!',
|
||||
'invalid_name' => 'The username is empty, please, write username!!!',
|
||||
];
|
||||
|
||||
public function create()
|
||||
{
|
||||
return new BaseMessages($this->messages);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models\Validation;
|
||||
|
||||
use App\Models\Validation\ModelValidationByName;
|
||||
use App\Models\Validation\Messages\UserMessagesFactory;
|
||||
|
||||
class UserValidationByMatrixUsername extends ModelValidationByName
|
||||
{
|
||||
public function __construct(string $name)
|
||||
{
|
||||
parent::__construct(
|
||||
$name,
|
||||
'App\Models\User',
|
||||
(new UserMessagesFactory())->create(),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user