diff --git a/composer.json b/composer.json index 780014a..68136da 100644 --- a/composer.json +++ b/composer.json @@ -17,7 +17,7 @@ ], "require": { "php": "^8.1", - "fintech/core": "^0.0.1", + "fintech/core": "*", "illuminate/contracts": "^10.0", "laravel/sanctum": "^3.3", "owen-it/laravel-auditing": "^13.5", diff --git a/src/Http/Requests/UpdatePermissionRequest.php b/src/Http/Requests/UpdatePermissionRequest.php index b8aa887..9ceabf3 100644 --- a/src/Http/Requests/UpdatePermissionRequest.php +++ b/src/Http/Requests/UpdatePermissionRequest.php @@ -3,6 +3,7 @@ namespace Fintech\Auth\Http\Requests; use Illuminate\Foundation\Http\FormRequest; +use Illuminate\Validation\Rule; class UpdatePermissionRequest extends FormRequest { @@ -21,8 +22,13 @@ public function authorize(): bool */ public function rules(): array { + // dd($this->route('permission')); + + $uniqueRule = 'unique:'.config('fintech.auth.permission_model', \Fintech\Auth\Models\Permission::class).',name,'; + return [ - // + 'name' => ['required', 'string', 'min:5', 'max:255', $uniqueRule], + 'guard_name' => ['required', 'string', Rule::in(array_keys(config('auth.guards', ['web', 'api'])))], ]; } diff --git a/src/Interfaces/PermissionRepository.php b/src/Interfaces/PermissionRepository.php index f077cf6..18e7c02 100644 --- a/src/Interfaces/PermissionRepository.php +++ b/src/Interfaces/PermissionRepository.php @@ -48,7 +48,7 @@ public function update(int|string $id, array $attributes = []); * * @throws PermissionRepositoryException */ - public function read(int|string $id, $onlyTrashed = false); + public function find(int|string $id, $onlyTrashed = false); /** * find and delete a entry from records diff --git a/src/Repositories/Eloquent/PermissionRepository.php b/src/Repositories/Eloquent/PermissionRepository.php index 8e09792..51e2d07 100644 --- a/src/Repositories/Eloquent/PermissionRepository.php +++ b/src/Repositories/Eloquent/PermissionRepository.php @@ -2,8 +2,8 @@ namespace Fintech\Auth\Repositories\Eloquent; -use Fintech\Auth\Exceptions\PermissionRepositoryException; use Fintech\Auth\Interfaces\PermissionRepository as InterfacesPermissionRepository; +use Fintech\Core\Repositories\EloquentRepository; use Illuminate\Contracts\Database\Eloquent\Builder; use Illuminate\Contracts\Pagination\LengthAwarePaginator; use Illuminate\Database\Eloquent\Collection; @@ -13,10 +13,8 @@ /** * Class PermissionRepository */ -class PermissionRepository implements InterfacesPermissionRepository +class PermissionRepository extends EloquentRepository implements InterfacesPermissionRepository { - private Model $model; - public function __construct() { $model = app()->make(config('fintech.auth.permission_model', \Fintech\Auth\Models\Permission::class)); @@ -39,8 +37,7 @@ public function list(array $filters = []) $query = $this->model->newQuery(); if (isset($filters['search']) && ! empty($filters['search'])) { - $query->where('name', 'like', "%{$filters['search']}%") - ->orWhere('guard_name', 'like', "%{$filters['search']}%"); + $query->where('name', 'like', "%{$filters['search']}%"); } //Handle Sorting @@ -52,157 +49,4 @@ public function list(array $filters = []) : $query->get(); } - - /** - * Create a new entry resource - * - * @return Model|null - * - * @throws PermissionRepositoryException - */ - public function create(array $attributes = []) - { - try { - $this->model->fill($attributes); - if ($this->model->saveOrFail()) { - - $this->model->refresh(); - - return $this->model; - } - } catch (\Throwable $e) { - - throw new PermissionRepositoryException($e->getMessage(), 0, $e); - } - - return null; - } - - /** - * find and update a resource attributes - * - * @return Model|null - * - * @throws PermissionRepositoryException - */ - public function update(int|string $id, array $attributes = []) - { - try { - - $this->model = $this->model->findOrFail($id); - - } catch (\Throwable $exception) { - - throw new ModelNotFoundException($exception->getMessage(), 0, $exception); - } - - try { - if ($this->model->updateOrFail($attributes)) { - - $this->model->refresh(); - - return $this->model; - } - } catch (\Throwable $exception) { - - throw new PermissionRepositoryException($exception->getMessage(), 0, $exception); - } - - return null; - } - - /** - * find and delete a entry from records - * - * @param bool $onlyTrashed - * @return bool|null - * - * @throws PermissionRepositoryException - */ - public function read(int|string $id, $onlyTrashed = false) - { - try { - - $this->model = $this->model->findOrFail($id); - - } catch (\Throwable $exception) { - - throw new ModelNotFoundException($exception->getMessage(), 0, $exception); - } - - try { - - return $this->model->deleteOrFail(); - - } catch (\Throwable $exception) { - - throw new PermissionRepositoryException($exception->getMessage(), 0, $exception); - } - - return null; - } - - /** - * find and delete a entry from records - * - * @return bool|null - * - * @throws PermissionRepositoryException - */ - public function delete(int|string $id) - { - try { - - $this->model = $this->model->findOrFail($id); - - } catch (\Throwable $exception) { - - throw new ModelNotFoundException($exception->getMessage(), 0, $exception); - } - - try { - - return $this->model->deleteOrFail(); - - } catch (\Throwable $exception) { - - throw new PermissionRepositoryException($exception->getMessage(), 0, $exception); - } - - return null; - } - - /** - * find and restore a entry from records - * - * @return bool|null - * - * @throws PermissionRepositoryException - */ - public function restore(int|string $id) - { - if (! method_exists($this->model, 'restore')) { - throw new InvalidArgumentException('This model does not have `Illuminate\Database\Eloquent\SoftDeletes` trait to perform restoration.'); - } - - try { - - $this->model = $this->model->onlyTrashed()->findOrFail($id); - - } catch (\Throwable $exception) { - - throw new ModelNotFoundException($exception->getMessage(), 0, $exception); - } - - try { - - return $this->model->deleteOrFail(); - - } catch (\Throwable $exception) { - - throw new PermissionRepositoryException($exception->getMessage(), 0, $exception); - } - - return null; - } } diff --git a/src/Repositories/Eloquent/ProfileRepository.php b/src/Repositories/Eloquent/ProfileRepository.php index d118d30..832c574 100644 --- a/src/Repositories/Eloquent/ProfileRepository.php +++ b/src/Repositories/Eloquent/ProfileRepository.php @@ -58,17 +58,13 @@ public function list(array $filters = []) */ public function create(array $attributes = []) { - try { - $this->model->fill($attributes); - if ($this->model->saveOrFail()) { + $this->model->fill($attributes); - $this->model->refresh(); + if ($this->model->saveOrFail()) { - return $this->model; - } - } catch (\Throwable $e) { + $this->model->refresh(); - throw new ProfileRepositoryException($e->getMessage(), 0, $e); + return $this->model; } return null; diff --git a/src/Repositories/Eloquent/RoleRepository.php b/src/Repositories/Eloquent/RoleRepository.php index d8c8162..18b121c 100644 --- a/src/Repositories/Eloquent/RoleRepository.php +++ b/src/Repositories/Eloquent/RoleRepository.php @@ -56,17 +56,13 @@ public function list(array $filters = []) */ public function create(array $attributes = []) { - try { - $this->model->fill($attributes); - if ($this->model->saveOrFail()) { + $this->model->fill($attributes); - $this->model->refresh(); + if ($this->model->saveOrFail()) { - return $this->model; - } - } catch (Throwable $e) { + $this->model->refresh(); - throw new RoleRepositoryException($e->getMessage(), 0, $e); + return $this->model; } return null; diff --git a/src/Repositories/Eloquent/TeamRepository.php b/src/Repositories/Eloquent/TeamRepository.php index 5c0c5fc..3db4940 100644 --- a/src/Repositories/Eloquent/TeamRepository.php +++ b/src/Repositories/Eloquent/TeamRepository.php @@ -6,6 +6,7 @@ use Fintech\Auth\Interfaces\TeamRepository as InterfacesTeamRepository; use Fintech\Auth\Models\Team; use Illuminate\Contracts\Pagination\LengthAwarePaginator; +use Illuminate\Database\Eloquent\ModelNotFoundException; use InvalidArgumentException; use Throwable; @@ -56,17 +57,13 @@ public function list(array $filters = []) */ public function create(array $attributes = []) { - try { - $this->model->fill($attributes); - if ($this->model->saveOrFail()) { + $this->model->fill($attributes); - $this->model->refresh(); + if ($this->model->saveOrFail()) { - return $this->model; - } - } catch (Throwable $e) { + $this->model->refresh(); - throw new TeamRepositoryException($e->getMessage(), 0, $e); + return $this->model; } return null; diff --git a/src/Repositories/Eloquent/UserRepository.php b/src/Repositories/Eloquent/UserRepository.php index 18bf171..6e13a53 100644 --- a/src/Repositories/Eloquent/UserRepository.php +++ b/src/Repositories/Eloquent/UserRepository.php @@ -64,19 +64,13 @@ public function list(array $filters = []) */ public function create(array $attributes = []) { - try { - - $this->model->fill($attributes); + $this->model->fill($attributes); - if ($this->model->saveOrFail()) { + if ($this->model->saveOrFail()) { - $this->model->refresh(); + $this->model->refresh(); - return $this->model; - } - } catch (Throwable $e) { - - throw new UserRepositoryException($e->getMessage(), 0, $e); + return $this->model; } return null; diff --git a/src/Repositories/Mongodb/PermissionRepository.php b/src/Repositories/Mongodb/PermissionRepository.php index 32abe6b..04d2ad6 100644 --- a/src/Repositories/Mongodb/PermissionRepository.php +++ b/src/Repositories/Mongodb/PermissionRepository.php @@ -56,17 +56,13 @@ public function list(array $filters = []) */ public function create(array $attributes = []) { - try { - $this->model->fill($attributes); - if ($this->model->saveOrFail()) { + $this->model->fill($attributes); - $this->model->refresh(); + if ($this->model->saveOrFail()) { - return $this->model; - } - } catch (Throwable $e) { + $this->model->refresh(); - throw new PermissionRepositoryException($e->getMessage(), 0, $e); + return $this->model; } return null; @@ -113,7 +109,7 @@ public function update(int|string $id, array $attributes = []) * * @throws PermissionRepositoryException */ - public function read(int|string $id, $onlyTrashed = false) + public function find(int|string $id, $onlyTrashed = false) { try { diff --git a/src/Repositories/Mongodb/ProfileRepository.php b/src/Repositories/Mongodb/ProfileRepository.php index a4273c3..9fefae5 100644 --- a/src/Repositories/Mongodb/ProfileRepository.php +++ b/src/Repositories/Mongodb/ProfileRepository.php @@ -57,17 +57,13 @@ public function list(array $filters = []) */ public function create(array $attributes = []) { - try { - $this->model->fill($attributes); - if ($this->model->saveOrFail()) { + $this->model->fill($attributes); - $this->model->refresh(); + if ($this->model->saveOrFail()) { - return $this->model; - } - } catch (Throwable $e) { + $this->model->refresh(); - throw new ProfileRepositoryException($e->getMessage(), 0, $e); + return $this->model; } return null; diff --git a/src/Repositories/Mongodb/RoleRepository.php b/src/Repositories/Mongodb/RoleRepository.php index 8a6cd26..e1e324b 100644 --- a/src/Repositories/Mongodb/RoleRepository.php +++ b/src/Repositories/Mongodb/RoleRepository.php @@ -56,17 +56,13 @@ public function list(array $filters = []) */ public function create(array $attributes = []) { - try { - $this->model->fill($attributes); - if ($this->model->saveOrFail()) { + $this->model->fill($attributes); - $this->model->refresh(); + if ($this->model->saveOrFail()) { - return $this->model; - } - } catch (Throwable $e) { + $this->model->refresh(); - throw new RoleRepositoryException($e->getMessage(), 0, $e); + return $this->model; } return null; diff --git a/src/Repositories/Mongodb/TeamRepository.php b/src/Repositories/Mongodb/TeamRepository.php index a01adaa..a3bb6f2 100644 --- a/src/Repositories/Mongodb/TeamRepository.php +++ b/src/Repositories/Mongodb/TeamRepository.php @@ -56,17 +56,13 @@ public function list(array $filters = []) */ public function create(array $attributes = []) { - try { - $this->model->fill($attributes); - if ($this->model->saveOrFail()) { + $this->model->fill($attributes); - $this->model->refresh(); + if ($this->model->saveOrFail()) { - return $this->model; - } - } catch (Throwable $e) { + $this->model->refresh(); - throw new TeamRepositoryException($e->getMessage(), 0, $e); + return $this->model; } return null; diff --git a/src/Repositories/Mongodb/UserRepository.php b/src/Repositories/Mongodb/UserRepository.php index 0dc279e..b9238f0 100644 --- a/src/Repositories/Mongodb/UserRepository.php +++ b/src/Repositories/Mongodb/UserRepository.php @@ -6,6 +6,7 @@ use Fintech\Auth\Interfaces\UserRepository as InterfacesUserRepository; use Fintech\Auth\Models\User; use Illuminate\Contracts\Pagination\LengthAwarePaginator; +use Illuminate\Database\Eloquent\Model; use InvalidArgumentException; use Throwable; @@ -55,22 +56,16 @@ public function list(array $filters = []) * Create a new entry resource * * @return Model|null - * - * @throws UserRepositoryException */ public function create(array $attributes = []) { - try { - $this->model->fill($attributes); - if ($this->model->saveOrFail()) { + $this->model->fill($attributes); - $this->model->refresh(); + if ($this->model->saveOrFail()) { - return $this->model; - } - } catch (Throwable $e) { + $this->model->refresh(); - throw new UserRepositoryException($e->getMessage(), 0, $e); + return $this->model; } return null;