From 9debfc3bc8050bdd573215dbcc60c962411abf36 Mon Sep 17 00:00:00 2001 From: Mohammad Hafijul Islam Date: Mon, 2 Oct 2023 00:37:57 +0600 Subject: [PATCH 1/5] LP-28 create operation generic exception added --- src/Http/Requests/UpdatePermissionRequest.php | 8 +++++- .../Eloquent/PermissionRepository.php | 25 ++++++------------- .../Eloquent/ProfileRepository.php | 12 +++------ src/Repositories/Eloquent/RoleRepository.php | 12 +++------ src/Repositories/Eloquent/TeamRepository.php | 12 +++------ src/Repositories/Eloquent/UserRepository.php | 14 +++-------- .../Mongodb/PermissionRepository.php | 12 +++------ .../Mongodb/ProfileRepository.php | 12 +++------ src/Repositories/Mongodb/RoleRepository.php | 12 +++------ src/Repositories/Mongodb/TeamRepository.php | 12 +++------ src/Repositories/Mongodb/UserRepository.php | 15 +++++------ 11 files changed, 53 insertions(+), 93 deletions(-) diff --git a/src/Http/Requests/UpdatePermissionRequest.php b/src/Http/Requests/UpdatePermissionRequest.php index b8aa887..1990b0c 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/Repositories/Eloquent/PermissionRepository.php b/src/Repositories/Eloquent/PermissionRepository.php index 8e09792..a58dbff 100644 --- a/src/Repositories/Eloquent/PermissionRepository.php +++ b/src/Repositories/Eloquent/PermissionRepository.php @@ -8,6 +8,7 @@ use Illuminate\Contracts\Pagination\LengthAwarePaginator; use Illuminate\Database\Eloquent\Collection; use Illuminate\Database\Eloquent\Model; +use Illuminate\Database\Eloquent\ModelNotFoundException; use InvalidArgumentException; /** @@ -56,24 +57,21 @@ public function list(array $filters = []) /** * Create a new entry resource * + * @param array $attributes * @return Model|null * - * @throws PermissionRepositoryException + * @throws \Throwable */ 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; } @@ -89,11 +87,13 @@ public function update(int|string $id, array $attributes = []) { try { - $this->model = $this->model->findOrFail($id); + $this->model = $this->model->find($id); } catch (\Throwable $exception) { - throw new ModelNotFoundException($exception->getMessage(), 0, $exception); + dd($this->m$exception->getMessage()); + +// throw new ModelNotFoundException($exception->getMessage(), 0, $exception); } try { @@ -130,15 +130,6 @@ public function read(int|string $id, $onlyTrashed = false) 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..f18122d 100644 --- a/src/Repositories/Eloquent/TeamRepository.php +++ b/src/Repositories/Eloquent/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/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..b29fc0c 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; 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..9db3038 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; @@ -54,23 +55,19 @@ public function list(array $filters = []) /** * Create a new entry resource * + * @param array $attributes * @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; From 1809d7843c46f47fdda47f3a0ae410480042959c Mon Sep 17 00:00:00 2001 From: Mohammad Hafijul Islam Date: Mon, 2 Oct 2023 03:30:38 +0600 Subject: [PATCH 2/5] LP-28 permission repository optimization working --- src/Interfaces/PermissionRepository.php | 2 +- .../Eloquent/PermissionRepository.php | 153 +----------------- src/Repositories/Eloquent/TeamRepository.php | 1 + .../Mongodb/PermissionRepository.php | 2 +- 4 files changed, 6 insertions(+), 152 deletions(-) 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 a58dbff..5f9f9dc 100644 --- a/src/Repositories/Eloquent/PermissionRepository.php +++ b/src/Repositories/Eloquent/PermissionRepository.php @@ -2,22 +2,19 @@ namespace Fintech\Auth\Repositories\Eloquent; -use Fintech\Auth\Exceptions\PermissionRepositoryException; use Fintech\Auth\Interfaces\PermissionRepository as InterfacesPermissionRepository; use Illuminate\Contracts\Database\Eloquent\Builder; use Illuminate\Contracts\Pagination\LengthAwarePaginator; use Illuminate\Database\Eloquent\Collection; use Illuminate\Database\Eloquent\Model; -use Illuminate\Database\Eloquent\ModelNotFoundException; + use InvalidArgumentException; /** * 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)); @@ -40,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 @@ -53,147 +49,4 @@ public function list(array $filters = []) : $query->get(); } - - /** - * Create a new entry resource - * - * @param array $attributes - * @return Model|null - * - * @throws \Throwable - */ - public function create(array $attributes = []) - { - $this->model->fill($attributes); - - if ($this->model->saveOrFail()) { - - $this->model->refresh(); - - return $this->model; - } - - 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->find($id); - - } catch (\Throwable $exception) { - - dd($this->m$exception->getMessage()); - -// 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); - } - - 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/TeamRepository.php b/src/Repositories/Eloquent/TeamRepository.php index f18122d..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; diff --git a/src/Repositories/Mongodb/PermissionRepository.php b/src/Repositories/Mongodb/PermissionRepository.php index b29fc0c..04d2ad6 100644 --- a/src/Repositories/Mongodb/PermissionRepository.php +++ b/src/Repositories/Mongodb/PermissionRepository.php @@ -109,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 { From f21ed4ada8cf42d5e41119d405fd36bbc6f35630 Mon Sep 17 00:00:00 2001 From: Mohammad Hafijul Islam Date: Mon, 2 Oct 2023 03:36:45 +0600 Subject: [PATCH 3/5] LP-28 eloquent namespace issue fixed --- src/Repositories/Eloquent/PermissionRepository.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Repositories/Eloquent/PermissionRepository.php b/src/Repositories/Eloquent/PermissionRepository.php index 5f9f9dc..eec40df 100644 --- a/src/Repositories/Eloquent/PermissionRepository.php +++ b/src/Repositories/Eloquent/PermissionRepository.php @@ -3,6 +3,7 @@ namespace Fintech\Auth\Repositories\Eloquent; 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; From c9d2ce2086e21ff6f7f1a280bd801e440927cfee Mon Sep 17 00:00:00 2001 From: hafijul233 Date: Sun, 1 Oct 2023 21:55:14 +0000 Subject: [PATCH 4/5] Fix styling --- src/Http/Requests/UpdatePermissionRequest.php | 4 ++-- src/Repositories/Eloquent/PermissionRepository.php | 1 - src/Repositories/Mongodb/UserRepository.php | 2 -- 3 files changed, 2 insertions(+), 5 deletions(-) diff --git a/src/Http/Requests/UpdatePermissionRequest.php b/src/Http/Requests/UpdatePermissionRequest.php index 1990b0c..9ceabf3 100644 --- a/src/Http/Requests/UpdatePermissionRequest.php +++ b/src/Http/Requests/UpdatePermissionRequest.php @@ -22,9 +22,9 @@ public function authorize(): bool */ public function rules(): array { -// dd($this->route('permission')); + // dd($this->route('permission')); - $uniqueRule = 'unique:' . config('fintech.auth.permission_model', \Fintech\Auth\Models\Permission::class) . ',name,'; + $uniqueRule = 'unique:'.config('fintech.auth.permission_model', \Fintech\Auth\Models\Permission::class).',name,'; return [ 'name' => ['required', 'string', 'min:5', 'max:255', $uniqueRule], diff --git a/src/Repositories/Eloquent/PermissionRepository.php b/src/Repositories/Eloquent/PermissionRepository.php index eec40df..51e2d07 100644 --- a/src/Repositories/Eloquent/PermissionRepository.php +++ b/src/Repositories/Eloquent/PermissionRepository.php @@ -8,7 +8,6 @@ use Illuminate\Contracts\Pagination\LengthAwarePaginator; use Illuminate\Database\Eloquent\Collection; use Illuminate\Database\Eloquent\Model; - use InvalidArgumentException; /** diff --git a/src/Repositories/Mongodb/UserRepository.php b/src/Repositories/Mongodb/UserRepository.php index 9db3038..b9238f0 100644 --- a/src/Repositories/Mongodb/UserRepository.php +++ b/src/Repositories/Mongodb/UserRepository.php @@ -55,9 +55,7 @@ public function list(array $filters = []) /** * Create a new entry resource * - * @param array $attributes * @return Model|null - * */ public function create(array $attributes = []) { From b1a4de10485ab7aac8564c3e11be9dee84826d8c Mon Sep 17 00:00:00 2001 From: Mohammad Hafijul Islam Date: Mon, 2 Oct 2023 04:11:54 +0600 Subject: [PATCH 5/5] LP-28 fintech core version lock disabled --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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",