diff --git a/composer.json b/composer.json index 1f87224..80b5da2 100644 --- a/composer.json +++ b/composer.json @@ -18,7 +18,8 @@ "require": { "php": "^8.1", "illuminate/contracts": "^10.0", - "laravel/sanctum": "^3.3" + "laravel/sanctum": "^3.3", + "spatie/laravel-permission": "^5.11" }, "require-dev": { "laravel/pint": "^1.0", diff --git a/routes/api.php b/routes/api.php index cc63144..37b24c3 100644 --- a/routes/api.php +++ b/routes/api.php @@ -48,6 +48,12 @@ Route::post('/logout', [AuthenticatedSessionController::class, 'destroy']) ->middleware('auth') ->name('logout'); + + Route::apiResource('users', \Fintech\Auth\Http\Controllers\UserController::class); +// Route::apiResource('roles', \Fintech\Auth\Http\Controllers\RoleController::class); +// Route::apiResource('permissions', \Fintech\Auth\Http\Resources\PermissionCollection::class); +// Route::apiResource('teams', \Fintech\Auth\Http\Controllers\TeamController::class); + Route::apiSingleton('users.profile', \Fintech\Auth\Http\Controllers\ProfileController::class); }); }); diff --git a/src/Auth.php b/src/Auth.php index 239cb26..bc8de3a 100644 --- a/src/Auth.php +++ b/src/Auth.php @@ -2,6 +2,47 @@ namespace Fintech\Auth; +use Illuminate\Contracts\Container\BindingResolutionException; + class Auth { + /** + * @return \Fintech\Auth\Services\UserService + * + * @throws BindingResolutionException + */ + public function user() + { + return app()->make(\Fintech\Auth\Services\UserService::class); + } + + /** + * @return \Fintech\Auth\Services\RoleService + * + * @throws BindingResolutionException + */ + public function role() + { + return app()->make(\Fintech\Auth\Services\RoleService::class); + } + + /** + * @return \Fintech\Auth\Services\PermissionService + * + * @throws BindingResolutionException + */ + public function permission() + { + return app()->make(\Fintech\Auth\Services\PermissionService::class); + } + + /** + * @return \Fintech\Auth\Services\TeamService + * + * @throws BindingResolutionException + */ + public function team() + { + return app()->make(\Fintech\Auth\Services\TeamService::class); + } } diff --git a/src/Exceptions/PermissionRepositoryException.php b/src/Exceptions/PermissionRepositoryException.php new file mode 100644 index 0000000..a201d46 --- /dev/null +++ b/src/Exceptions/PermissionRepositoryException.php @@ -0,0 +1,15 @@ +validated(); + + $permissionPaginate = \Auth::permission()->list($inputs); + + return new PermissionCollection($permissionPaginate); + + } catch (\Exception $exception) { + + return $this->failed($exception->getMessage()); + } + } + + /** + * @lrd:start + * Create a new permission resource in storage. + * @lrd:end + * + * @param StorePermissionRequest $request + * @return JsonResponse + * @throws StoreOperationException + */ + public function store(StorePermissionRequest $request): JsonResponse + { + try { + $inputs = $request->validated(); + + $permission = \Auth::permission()->create($inputs); + + if (!$permission) { + throw new StoreOperationException(); + } + + return $this->created([ + 'message' => __('auth::messages.resource.created', ['model' => 'Permission']), + 'id' => $permission->id + ]); + + } catch (\Exception $exception) { + + return $this->failed($exception->getMessage()); + } + } + + /** + * @lrd:start + * Return a specified permission resource found by id. + * @lrd:end + * + * @param string|int $id + * @return PermissionResource|JsonResponse + * @throws ResourceNotFoundException + */ + public function show(string|int $id): PermissionResource|JsonResponse + { + try { + + $permission = \Auth::permission()->read($id); + + if (!$permission) { + throw new ResourceNotFoundException(__('auth::messages.resource.notfound', ['model' => 'Permission', 'id' => strval($id)])); + } + + return new PermissionResource($permission); + + } catch (ResourceNotFoundException $exception) { + + return $this->notfound($exception->getMessage()); + + } catch (\Exception $exception) { + + return $this->failed($exception->getMessage()); + } + } + + /** + * @lrd:start + * Update a specified permission resource using id. + * @lrd:end + * + * @param UpdatePermissionRequest $request + * @param string|int $id + * @return JsonResponse + * @throws ResourceNotFoundException + * @throws UpdateOperationException + */ + public function update(UpdatePermissionRequest $request, string|int $id): JsonResponse + { + try { + + $permission = \Auth::permission()->read($id); + + if (!$permission) { + throw new ResourceNotFoundException(__('auth::messages.resource.notfound', ['model' => 'Permission', 'id' => strval($id)])); + } + + $inputs = $request->validated(); + + if (!\Auth::permission()->update($id, $inputs)) { + + throw new UpdateOperationException(); + } + + return $this->updated(__('auth::messages.resource.updated', ['model' => 'Permission'])); + + } catch (ResourceNotFoundException $exception) { + + return $this->notfound($exception->getMessage()); + + } catch (\Exception $exception) { + + return $this->failed($exception->getMessage()); + } + } + + /** + * @lrd:start + * Soft delete a specified permission resource using id. + * @lrd:end + * + * @param string|int $id + * @return JsonResponse + * @throws ResourceNotFoundException + * @throws DeleteOperationException + */ + public function destroy(string|int $id) + { + try { + + $permission = \Auth::permission()->read($id); + + if (!$permission) { + throw new ResourceNotFoundException(__('auth::messages.resource.notfound', ['model' => 'Permission', 'id' => strval($id)])); + } + + if (!\Auth::permission()->destroy($id)) { + + throw new DeleteOperationException(); + } + + return $this->deleted(__('auth::messages.resource.deleted', ['model' => 'Permission'])); + + } catch (ResourceNotFoundException $exception) { + + return $this->notfound($exception->getMessage()); + + } catch (\Exception $exception) { + + return $this->failed($exception->getMessage()); + } + } + + /** + * @lrd:start + * Restore the specified permission resource from trash. + * ** ```Soft Delete``` needs to enabled to use this feature** + * @lrd:end + * + * @param string|int $id + * @return JsonResponse + */ + public function restore(string|int $id) + { + try { + + $permission = \Auth::permission()->read($id, true); + + if (!$permission) { + throw new ResourceNotFoundException(__('auth::messages.resource.notfound', ['model' => 'Permission', 'id' => strval($id)])); + } + + if (!\Auth::permission()->restore($id)) { + + throw new RestoreOperationException(); + } + + return $this->restored(__('auth::messages.resource.restored', ['model' => 'Permission'])); + + } catch (ResourceNotFoundException $exception) { + + return $this->notfound($exception->getMessage()); + + } catch (\Exception $exception) { + + return $this->failed($exception->getMessage()); + } + } + + /** + * @lrd:start + * Create a exportable list of the permission resource as document. + * After export job is done system will fire export completed event + * + * @lrd:end + * + * @param IndexPermissionRequest $request + * @return JsonResponse + */ + public function export(IndexPermissionRequest $request): JsonResponse + { + try { + $inputs = $request->validated(); + + $permissionPaginate = \Auth::permission()->export($inputs); + + return $this->exported(__('auth::messages.resource.exported', ['model' => 'Permission'])); + + } catch (\Exception $exception) { + + return $this->failed($exception->getMessage()); + } + } + + /** + * @lrd:start + * Create a exportable list of the permission resource as document. + * After export job is done system will fire export completed event + * + * @lrd:end + * + * @param ImportPermissionRequest $request + * @return PermissionCollection|JsonResponse + */ + public function import(ImportPermissionRequest $request): JsonResponse + { + try { + $inputs = $request->validated(); + + $permissionPaginate = \Auth::permission()->list($inputs); + + return new PermissionCollection($permissionPaginate); + + } catch (\Exception $exception) { + + return $this->failed($exception->getMessage()); + } + } +} diff --git a/src/Http/Controllers/ProfileController.php b/src/Http/Controllers/ProfileController.php new file mode 100644 index 0000000..35754bf --- /dev/null +++ b/src/Http/Controllers/ProfileController.php @@ -0,0 +1,292 @@ +validated(); + + $userProfilePaginate = \Auth::userProfile()->list($inputs); + + return new ProfileCollection($userProfilePaginate); + + } catch (\Exception $exception) { + + return $this->failed($exception->getMessage()); + } + } + + /** + * @lrd:start + * Create a new userProfile resource in storage. + * @lrd:end + * + * @param StoreProfileRequest $request + * @return JsonResponse + * @throws StoreOperationException + */ + public function store(StoreProfileRequest $request): JsonResponse + { + try { + $inputs = $request->validated(); + + $userProfile = \Auth::userProfile()->create($inputs); + + if (!$userProfile) { + throw new StoreOperationException(); + } + + return $this->created([ + 'message' => __('auth::messages.resource.created', ['model' => 'UserProfile']), + 'id' => $userProfile->id + ]); + + } catch (\Exception $exception) { + + return $this->failed($exception->getMessage()); + } + } + + /** + * @lrd:start + * Return a specified userProfile resource found by id. + * @lrd:end + * + * @param string|int $id + * @return UserProfileResource|JsonResponse + * @throws ResourceNotFoundException + */ + public function show(string|int $id): UserProfileResource|JsonResponse + { + try { + + $userProfile = \Auth::userProfile()->read($id); + + if (!$userProfile) { + throw new ResourceNotFoundException(__('auth::messages.resource.notfound', ['model' => 'UserProfile', 'id' => strval($id)])); + } + + return new UserProfileResource($userProfile); + + } catch (ResourceNotFoundException $exception) { + + return $this->notfound($exception->getMessage()); + + } catch (\Exception $exception) { + + return $this->failed($exception->getMessage()); + } + } + + /** + * @lrd:start + * Update a specified userProfile resource using id. + * @lrd:end + * + * @param UpdateProfileRequest $request + * @param string|int $id + * @return JsonResponse + * @throws ResourceNotFoundException + * @throws UpdateOperationException + */ + public function update(UpdateProfileRequest $request, string|int $id): JsonResponse + { + try { + + $userProfile = \Auth::userProfile()->read($id); + + if (!$userProfile) { + throw new ResourceNotFoundException(__('auth::messages.resource.notfound', ['model' => 'UserProfile', 'id' => strval($id)])); + } + + $inputs = $request->validated(); + + if (!\Auth::userProfile()->update($id, $inputs)) { + + throw new UpdateOperationException(); + } + + return $this->updated(__('auth::messages.resource.updated', ['model' => 'UserProfile'])); + + } catch (ResourceNotFoundException $exception) { + + return $this->notfound($exception->getMessage()); + + } catch (\Exception $exception) { + + return $this->failed($exception->getMessage()); + } + } + + /** + * @lrd:start + * Soft delete a specified userProfile resource using id. + * @lrd:end + * + * @param string|int $id + * @return JsonResponse + * @throws ResourceNotFoundException + * @throws DeleteOperationException + */ + public function destroy(string|int $id) + { + try { + + $userProfile = \Auth::userProfile()->read($id); + + if (!$userProfile) { + throw new ResourceNotFoundException(__('auth::messages.resource.notfound', ['model' => 'UserProfile', 'id' => strval($id)])); + } + + if (!\Auth::userProfile()->destroy($id)) { + + throw new DeleteOperationException(); + } + + return $this->deleted(__('auth::messages.resource.deleted', ['model' => 'UserProfile'])); + + } catch (ResourceNotFoundException $exception) { + + return $this->notfound($exception->getMessage()); + + } catch (\Exception $exception) { + + return $this->failed($exception->getMessage()); + } + } + + /** + * @lrd:start + * Restore the specified userProfile resource from trash. + * ** ```Soft Delete``` needs to enabled to use this feature** + * @lrd:end + * + * @param string|int $id + * @return JsonResponse + */ + public function restore(string|int $id) + { + try { + + $userProfile = \Auth::userProfile()->read($id, true); + + if (!$userProfile) { + throw new ResourceNotFoundException(__('auth::messages.resource.notfound', ['model' => 'UserProfile', 'id' => strval($id)])); + } + + if (!\Auth::userProfile()->restore($id)) { + + throw new RestoreOperationException(); + } + + return $this->restored(__('auth::messages.resource.restored', ['model' => 'UserProfile'])); + + } catch (ResourceNotFoundException $exception) { + + return $this->notfound($exception->getMessage()); + + } catch (\Exception $exception) { + + return $this->failed($exception->getMessage()); + } + } + + /** + * @lrd:start + * Create a exportable list of the userProfile resource as document. + * After export job is done system will fire export completed event + * + * @lrd:end + * + * @param IndexProfileRequest $request + * @return JsonResponse + */ + public function export(IndexProfileRequest $request): JsonResponse + { + try { + $inputs = $request->validated(); + + $userProfilePaginate = \Auth::userProfile()->export($inputs); + + return $this->exported(__('auth::messages.resource.exported', ['model' => 'UserProfile'])); + + } catch (\Exception $exception) { + + return $this->failed($exception->getMessage()); + } + } + + /** + * @lrd:start + * Create a exportable list of the userProfile resource as document. + * After export job is done system will fire export completed event + * + * @lrd:end + * + * @param ImportProfileRequest $request + * @return ProfileCollection|JsonResponse + */ + public function import(ImportProfileRequest $request): JsonResponse + { + try { + $inputs = $request->validated(); + + $userProfilePaginate = \Auth::userProfile()->list($inputs); + + return new ProfileCollection($userProfilePaginate); + + } catch (\Exception $exception) { + + return $this->failed($exception->getMessage()); + } + } +} diff --git a/src/Http/Controllers/RoleController.php b/src/Http/Controllers/RoleController.php new file mode 100644 index 0000000..8f8ce0a --- /dev/null +++ b/src/Http/Controllers/RoleController.php @@ -0,0 +1,292 @@ +validated(); + + $rolePaginate = \Auth::role()->list($inputs); + + return new RoleCollection($rolePaginate); + + } catch (\Exception $exception) { + + return $this->failed($exception->getMessage()); + } + } + + /** + * @lrd:start + * Create a new role resource in storage. + * @lrd:end + * + * @param StoreRoleRequest $request + * @return JsonResponse + * @throws StoreOperationException + */ + public function store(StoreRoleRequest $request): JsonResponse + { + try { + $inputs = $request->validated(); + + $role = \Auth::role()->create($inputs); + + if (!$role) { + throw new StoreOperationException(); + } + + return $this->created([ + 'message' => __('auth::messages.resource.created', ['model' => 'Role']), + 'id' => $role->id + ]); + + } catch (\Exception $exception) { + + return $this->failed($exception->getMessage()); + } + } + + /** + * @lrd:start + * Return a specified role resource found by id. + * @lrd:end + * + * @param string|int $id + * @return RoleResource|JsonResponse + * @throws ResourceNotFoundException + */ + public function show(string|int $id): RoleResource|JsonResponse + { + try { + + $role = \Auth::role()->read($id); + + if (!$role) { + throw new ResourceNotFoundException(__('auth::messages.resource.notfound', ['model' => 'Role', 'id' => strval($id)])); + } + + return new RoleResource($role); + + } catch (ResourceNotFoundException $exception) { + + return $this->notfound($exception->getMessage()); + + } catch (\Exception $exception) { + + return $this->failed($exception->getMessage()); + } + } + + /** + * @lrd:start + * Update a specified role resource using id. + * @lrd:end + * + * @param UpdateRoleRequest $request + * @param string|int $id + * @return JsonResponse + * @throws ResourceNotFoundException + * @throws UpdateOperationException + */ + public function update(UpdateRoleRequest $request, string|int $id): JsonResponse + { + try { + + $role = \Auth::role()->read($id); + + if (!$role) { + throw new ResourceNotFoundException(__('auth::messages.resource.notfound', ['model' => 'Role', 'id' => strval($id)])); + } + + $inputs = $request->validated(); + + if (!\Auth::role()->update($id, $inputs)) { + + throw new UpdateOperationException(); + } + + return $this->updated(__('auth::messages.resource.updated', ['model' => 'Role'])); + + } catch (ResourceNotFoundException $exception) { + + return $this->notfound($exception->getMessage()); + + } catch (\Exception $exception) { + + return $this->failed($exception->getMessage()); + } + } + + /** + * @lrd:start + * Soft delete a specified role resource using id. + * @lrd:end + * + * @param string|int $id + * @return JsonResponse + * @throws ResourceNotFoundException + * @throws DeleteOperationException + */ + public function destroy(string|int $id) + { + try { + + $role = \Auth::role()->read($id); + + if (!$role) { + throw new ResourceNotFoundException(__('auth::messages.resource.notfound', ['model' => 'Role', 'id' => strval($id)])); + } + + if (!\Auth::role()->destroy($id)) { + + throw new DeleteOperationException(); + } + + return $this->deleted(__('auth::messages.resource.deleted', ['model' => 'Role'])); + + } catch (ResourceNotFoundException $exception) { + + return $this->notfound($exception->getMessage()); + + } catch (\Exception $exception) { + + return $this->failed($exception->getMessage()); + } + } + + /** + * @lrd:start + * Restore the specified role resource from trash. + * ** ```Soft Delete``` needs to enabled to use this feature** + * @lrd:end + * + * @param string|int $id + * @return JsonResponse + */ + public function restore(string|int $id) + { + try { + + $role = \Auth::role()->read($id, true); + + if (!$role) { + throw new ResourceNotFoundException(__('auth::messages.resource.notfound', ['model' => 'Role', 'id' => strval($id)])); + } + + if (!\Auth::role()->restore($id)) { + + throw new RestoreOperationException(); + } + + return $this->restored(__('auth::messages.resource.restored', ['model' => 'Role'])); + + } catch (ResourceNotFoundException $exception) { + + return $this->notfound($exception->getMessage()); + + } catch (\Exception $exception) { + + return $this->failed($exception->getMessage()); + } + } + + /** + * @lrd:start + * Create a exportable list of the role resource as document. + * After export job is done system will fire export completed event + * + * @lrd:end + * + * @param IndexRoleRequest $request + * @return JsonResponse + */ + public function export(IndexRoleRequest $request): JsonResponse + { + try { + $inputs = $request->validated(); + + $rolePaginate = \Auth::role()->export($inputs); + + return $this->exported(__('auth::messages.resource.exported', ['model' => 'Role'])); + + } catch (\Exception $exception) { + + return $this->failed($exception->getMessage()); + } + } + + /** + * @lrd:start + * Create a exportable list of the role resource as document. + * After export job is done system will fire export completed event + * + * @lrd:end + * + * @param ImportRoleRequest $request + * @return RoleCollection|JsonResponse + */ + public function import(ImportRoleRequest $request): JsonResponse + { + try { + $inputs = $request->validated(); + + $rolePaginate = \Auth::role()->list($inputs); + + return new RoleCollection($rolePaginate); + + } catch (\Exception $exception) { + + return $this->failed($exception->getMessage()); + } + } +} diff --git a/src/Http/Controllers/TeamController.php b/src/Http/Controllers/TeamController.php new file mode 100644 index 0000000..0570cb2 --- /dev/null +++ b/src/Http/Controllers/TeamController.php @@ -0,0 +1,292 @@ +validated(); + + $teamPaginate = \Auth::team()->list($inputs); + + return new TeamCollection($teamPaginate); + + } catch (\Exception $exception) { + + return $this->failed($exception->getMessage()); + } + } + + /** + * @lrd:start + * Create a new team resource in storage. + * @lrd:end + * + * @param StoreTeamRequest $request + * @return JsonResponse + * @throws StoreOperationException + */ + public function store(StoreTeamRequest $request): JsonResponse + { + try { + $inputs = $request->validated(); + + $team = \Auth::team()->create($inputs); + + if (!$team) { + throw new StoreOperationException(); + } + + return $this->created([ + 'message' => __('auth::messages.resource.created', ['model' => 'Team']), + 'id' => $team->id + ]); + + } catch (\Exception $exception) { + + return $this->failed($exception->getMessage()); + } + } + + /** + * @lrd:start + * Return a specified team resource found by id. + * @lrd:end + * + * @param string|int $id + * @return TeamResource|JsonResponse + * @throws ResourceNotFoundException + */ + public function show(string|int $id): TeamResource|JsonResponse + { + try { + + $team = \Auth::team()->read($id); + + if (!$team) { + throw new ResourceNotFoundException(__('auth::messages.resource.notfound', ['model' => 'Team', 'id' => strval($id)])); + } + + return new TeamResource($team); + + } catch (ResourceNotFoundException $exception) { + + return $this->notfound($exception->getMessage()); + + } catch (\Exception $exception) { + + return $this->failed($exception->getMessage()); + } + } + + /** + * @lrd:start + * Update a specified team resource using id. + * @lrd:end + * + * @param UpdateTeamRequest $request + * @param string|int $id + * @return JsonResponse + * @throws ResourceNotFoundException + * @throws UpdateOperationException + */ + public function update(UpdateTeamRequest $request, string|int $id): JsonResponse + { + try { + + $team = \Auth::team()->read($id); + + if (!$team) { + throw new ResourceNotFoundException(__('auth::messages.resource.notfound', ['model' => 'Team', 'id' => strval($id)])); + } + + $inputs = $request->validated(); + + if (!\Auth::team()->update($id, $inputs)) { + + throw new UpdateOperationException(); + } + + return $this->updated(__('auth::messages.resource.updated', ['model' => 'Team'])); + + } catch (ResourceNotFoundException $exception) { + + return $this->notfound($exception->getMessage()); + + } catch (\Exception $exception) { + + return $this->failed($exception->getMessage()); + } + } + + /** + * @lrd:start + * Soft delete a specified team resource using id. + * @lrd:end + * + * @param string|int $id + * @return JsonResponse + * @throws ResourceNotFoundException + * @throws DeleteOperationException + */ + public function destroy(string|int $id) + { + try { + + $team = \Auth::team()->read($id); + + if (!$team) { + throw new ResourceNotFoundException(__('auth::messages.resource.notfound', ['model' => 'Team', 'id' => strval($id)])); + } + + if (!\Auth::team()->destroy($id)) { + + throw new DeleteOperationException(); + } + + return $this->deleted(__('auth::messages.resource.deleted', ['model' => 'Team'])); + + } catch (ResourceNotFoundException $exception) { + + return $this->notfound($exception->getMessage()); + + } catch (\Exception $exception) { + + return $this->failed($exception->getMessage()); + } + } + + /** + * @lrd:start + * Restore the specified team resource from trash. + * ** ```Soft Delete``` needs to enabled to use this feature** + * @lrd:end + * + * @param string|int $id + * @return JsonResponse + */ + public function restore(string|int $id) + { + try { + + $team = \Auth::team()->read($id, true); + + if (!$team) { + throw new ResourceNotFoundException(__('auth::messages.resource.notfound', ['model' => 'Team', 'id' => strval($id)])); + } + + if (!\Auth::team()->restore($id)) { + + throw new RestoreOperationException(); + } + + return $this->restored(__('auth::messages.resource.restored', ['model' => 'Team'])); + + } catch (ResourceNotFoundException $exception) { + + return $this->notfound($exception->getMessage()); + + } catch (\Exception $exception) { + + return $this->failed($exception->getMessage()); + } + } + + /** + * @lrd:start + * Create a exportable list of the team resource as document. + * After export job is done system will fire export completed event + * + * @lrd:end + * + * @param IndexTeamRequest $request + * @return JsonResponse + */ + public function export(IndexTeamRequest $request): JsonResponse + { + try { + $inputs = $request->validated(); + + $teamPaginate = \Auth::team()->export($inputs); + + return $this->exported(__('auth::messages.resource.exported', ['model' => 'Team'])); + + } catch (\Exception $exception) { + + return $this->failed($exception->getMessage()); + } + } + + /** + * @lrd:start + * Create a exportable list of the team resource as document. + * After export job is done system will fire export completed event + * + * @lrd:end + * + * @param ImportTeamRequest $request + * @return TeamCollection|JsonResponse + */ + public function import(ImportTeamRequest $request): JsonResponse + { + try { + $inputs = $request->validated(); + + $teamPaginate = \Auth::team()->list($inputs); + + return new TeamCollection($teamPaginate); + + } catch (\Exception $exception) { + + return $this->failed($exception->getMessage()); + } + } +} diff --git a/src/Http/Controllers/UserController.php b/src/Http/Controllers/UserController.php new file mode 100644 index 0000000..2a4f9b4 --- /dev/null +++ b/src/Http/Controllers/UserController.php @@ -0,0 +1,293 @@ +validated(); + + $userPaginate = Auth::user()->list($inputs); + + return new UserCollection($userPaginate); + + } catch (\Exception $exception) { + + return $this->failed($exception->getMessage()); + } + } + + /** + * @lrd:start + * Create a new user resource in storage. + * @lrd:end + * + * @param StoreUserRequest $request + * @return JsonResponse + * @throws StoreOperationException + */ + public function store(StoreUserRequest $request): JsonResponse + { + try { + $inputs = $request->validated(); + + $user = Auth::user()->create($inputs); + + if (!$user) { + throw new StoreOperationException(); + } + + return $this->created([ + 'message' => __('auth::messages.resource.created', ['model' => 'User']), + 'id' => $user->id + ]); + + } catch (\Exception $exception) { + + return $this->failed($exception->getMessage()); + } + } + + /** + * @lrd:start + * Return a specified user resource found by id. + * @lrd:end + * + * @param string|int $id + * @return UserResource|JsonResponse + * @throws ResourceNotFoundException + */ + public function show(string|int $id): UserResource|JsonResponse + { + try { + + $user = Auth::user()->read($id); + + if (!$user) { + throw new ResourceNotFoundException(__('auth::messages.resource.notfound', ['model' => 'User', 'id' => strval($id)])); + } + + return new UserResource($user); + + } catch (ResourceNotFoundException $exception) { + + return $this->notfound($exception->getMessage()); + + } catch (\Exception $exception) { + + return $this->failed($exception->getMessage()); + } + } + + /** + * @lrd:start + * Update a specified user resource using id. + * @lrd:end + * + * @param UpdateUserRequest $request + * @param string|int $id + * @return JsonResponse + * @throws ResourceNotFoundException + * @throws UpdateOperationException + */ + public function update(UpdateUserRequest $request, string|int $id): JsonResponse + { + try { + + $user = Auth::user()->read($id); + + if (!$user) { + throw new ResourceNotFoundException(__('auth::messages.resource.notfound', ['model' => 'User', 'id' => strval($id)])); + } + + $inputs = $request->validated(); + + if (!Auth::user()->update($id, $inputs)) { + + throw new UpdateOperationException(); + } + + return $this->updated(__('auth::messages.resource.updated', ['model' => 'User'])); + + } catch (ResourceNotFoundException $exception) { + + return $this->notfound($exception->getMessage()); + + } catch (\Exception $exception) { + + return $this->failed($exception->getMessage()); + } + } + + /** + * @lrd:start + * Soft delete a specified user resource using id. + * @lrd:end + * + * @param string|int $id + * @return JsonResponse + * @throws ResourceNotFoundException + * @throws DeleteOperationException + */ + public function destroy(string|int $id) + { + try { + + $user = Auth::user()->read($id); + + if (!$user) { + throw new ResourceNotFoundException(__('auth::messages.resource.notfound', ['model' => 'User', 'id' => strval($id)])); + } + + if (!Auth::user()->destroy($id)) { + + throw new DeleteOperationException(); + } + + return $this->deleted(__('auth::messages.resource.deleted', ['model' => 'User'])); + + } catch (ResourceNotFoundException $exception) { + + return $this->notfound($exception->getMessage()); + + } catch (\Exception $exception) { + + return $this->failed($exception->getMessage()); + } + } + + /** + * @lrd:start + * Restore the specified user resource from trash. + * ** ```Soft Delete``` needs to enabled to use this feature** + * @lrd:end + * + * @param string|int $id + * @return JsonResponse + */ + public function restore(string|int $id) + { + try { + + $user = Auth::user()->read($id, true); + + if (!$user) { + throw new ResourceNotFoundException(__('auth::messages.resource.notfound', ['model' => 'User', 'id' => strval($id)])); + } + + if (!Auth::user()->restore($id)) { + + throw new RestoreOperationException(); + } + + return $this->restored(__('auth::messages.resource.restored', ['model' => 'User'])); + + } catch (ResourceNotFoundException $exception) { + + return $this->notfound($exception->getMessage()); + + } catch (\Exception $exception) { + + return $this->failed($exception->getMessage()); + } + } + + /** + * @lrd:start + * Create a exportable list of the user resource as document. + * After export job is done system will fire export completed event + * + * @lrd:end + * + * @param IndexUserRequest $request + * @return JsonResponse + */ + public function export(IndexUserRequest $request): JsonResponse + { + try { + $inputs = $request->validated(); + + $userPaginate = Auth::user()->export($inputs); + + return $this->exported(__('auth::messages.resource.exported', ['model' => 'User'])); + + } catch (\Exception $exception) { + + return $this->failed($exception->getMessage()); + } + } + + /** + * @lrd:start + * Create a exportable list of the user resource as document. + * After export job is done system will fire export completed event + * + * @lrd:end + * + * @param ImportUserRequest $request + * @return UserCollection|JsonResponse + */ + public function import(ImportUserRequest $request): JsonResponse + { + try { + $inputs = $request->validated(); + + $userPaginate = Auth::user()->list($inputs); + + return new UserCollection($userPaginate); + + } catch (\Exception $exception) { + + return $this->failed($exception->getMessage()); + } + } +} diff --git a/src/Http/Requests/ImportPermissionRequest.php b/src/Http/Requests/ImportPermissionRequest.php new file mode 100644 index 0000000..bba3801 --- /dev/null +++ b/src/Http/Requests/ImportPermissionRequest.php @@ -0,0 +1,53 @@ + + */ + public function rules(): array + { + return [ + // + ]; + } + + /** + * Get the validation attributes that apply to the request. + * + * @return array + */ + public function attributes() + { + return [ + // + ]; + } + + /** + * Get the validation messages that apply to the request. + * + * @return array + */ + public function messages() + { + return [ + // + ]; + } +} diff --git a/src/Http/Requests/ImportProfileRequest.php b/src/Http/Requests/ImportProfileRequest.php new file mode 100644 index 0000000..0be2aca --- /dev/null +++ b/src/Http/Requests/ImportProfileRequest.php @@ -0,0 +1,53 @@ + + */ + public function rules(): array + { + return [ + // + ]; + } + + /** + * Get the validation attributes that apply to the request. + * + * @return array + */ + public function attributes() + { + return [ + // + ]; + } + + /** + * Get the validation messages that apply to the request. + * + * @return array + */ + public function messages() + { + return [ + // + ]; + } +} diff --git a/src/Http/Requests/ImportRoleRequest.php b/src/Http/Requests/ImportRoleRequest.php new file mode 100644 index 0000000..8a8f3c9 --- /dev/null +++ b/src/Http/Requests/ImportRoleRequest.php @@ -0,0 +1,53 @@ + + */ + public function rules(): array + { + return [ + // + ]; + } + + /** + * Get the validation attributes that apply to the request. + * + * @return array + */ + public function attributes() + { + return [ + // + ]; + } + + /** + * Get the validation messages that apply to the request. + * + * @return array + */ + public function messages() + { + return [ + // + ]; + } +} diff --git a/src/Http/Requests/ImportTeamRequest.php b/src/Http/Requests/ImportTeamRequest.php new file mode 100644 index 0000000..b461b60 --- /dev/null +++ b/src/Http/Requests/ImportTeamRequest.php @@ -0,0 +1,53 @@ + + */ + public function rules(): array + { + return [ + // + ]; + } + + /** + * Get the validation attributes that apply to the request. + * + * @return array + */ + public function attributes() + { + return [ + // + ]; + } + + /** + * Get the validation messages that apply to the request. + * + * @return array + */ + public function messages() + { + return [ + // + ]; + } +} diff --git a/src/Http/Requests/ImportUserRequest.php b/src/Http/Requests/ImportUserRequest.php new file mode 100644 index 0000000..5b354a7 --- /dev/null +++ b/src/Http/Requests/ImportUserRequest.php @@ -0,0 +1,53 @@ + + */ + public function rules(): array + { + return [ + // + ]; + } + + /** + * Get the validation attributes that apply to the request. + * + * @return array + */ + public function attributes() + { + return [ + // + ]; + } + + /** + * Get the validation messages that apply to the request. + * + * @return array + */ + public function messages() + { + return [ + // + ]; + } +} diff --git a/src/Http/Requests/IndexPermissionRequest.php b/src/Http/Requests/IndexPermissionRequest.php new file mode 100644 index 0000000..8b25075 --- /dev/null +++ b/src/Http/Requests/IndexPermissionRequest.php @@ -0,0 +1,58 @@ + + */ + public function rules(): array + { + return [ + 'search' => ['string', 'nullable', 'max:255'], + 'per_page' => ['integer', 'nullable', 'min:1', 'max:255'], + 'paginate' => ['boolean'], + 'sort' => ['string', 'nullable', 'min:2', 'max:255'], + 'dir' => ['string', 'min:3', 'max:4'], + ]; + } + + /** + * Get the validation attributes that apply to the request. + * + * @return array + */ + public function attributes() + { + return [ + // + ]; + } + + /** + * Get the validation messages that apply to the request. + * + * @return array + */ + public function messages() + { + return [ + // + ]; + } +} diff --git a/src/Http/Requests/IndexProfileRequest.php b/src/Http/Requests/IndexProfileRequest.php new file mode 100644 index 0000000..c3b3c9b --- /dev/null +++ b/src/Http/Requests/IndexProfileRequest.php @@ -0,0 +1,58 @@ + + */ + public function rules(): array + { + return [ + 'search' => ['string', 'nullable', 'max:255'], + 'per_page' => ['integer', 'nullable', 'min:1', 'max:255'], + 'paginate' => ['boolean'], + 'sort' => ['string', 'nullable', 'min:2', 'max:255'], + 'dir' => ['string', 'min:3', 'max:4'], + ]; + } + + /** + * Get the validation attributes that apply to the request. + * + * @return array + */ + public function attributes() + { + return [ + // + ]; + } + + /** + * Get the validation messages that apply to the request. + * + * @return array + */ + public function messages() + { + return [ + // + ]; + } +} diff --git a/src/Http/Requests/IndexRoleRequest.php b/src/Http/Requests/IndexRoleRequest.php new file mode 100644 index 0000000..6f84d34 --- /dev/null +++ b/src/Http/Requests/IndexRoleRequest.php @@ -0,0 +1,58 @@ + + */ + public function rules(): array + { + return [ + 'search' => ['string', 'nullable', 'max:255'], + 'per_page' => ['integer', 'nullable', 'min:1', 'max:255'], + 'paginate' => ['boolean'], + 'sort' => ['string', 'nullable', 'min:2', 'max:255'], + 'dir' => ['string', 'min:3', 'max:4'], + ]; + } + + /** + * Get the validation attributes that apply to the request. + * + * @return array + */ + public function attributes() + { + return [ + // + ]; + } + + /** + * Get the validation messages that apply to the request. + * + * @return array + */ + public function messages() + { + return [ + // + ]; + } +} diff --git a/src/Http/Requests/IndexTeamRequest.php b/src/Http/Requests/IndexTeamRequest.php new file mode 100644 index 0000000..e905bbe --- /dev/null +++ b/src/Http/Requests/IndexTeamRequest.php @@ -0,0 +1,58 @@ + + */ + public function rules(): array + { + return [ + 'search' => ['string', 'nullable', 'max:255'], + 'per_page' => ['integer', 'nullable', 'min:1', 'max:255'], + 'paginate' => ['boolean'], + 'sort' => ['string', 'nullable', 'min:2', 'max:255'], + 'dir' => ['string', 'min:3', 'max:4'], + ]; + } + + /** + * Get the validation attributes that apply to the request. + * + * @return array + */ + public function attributes() + { + return [ + // + ]; + } + + /** + * Get the validation messages that apply to the request. + * + * @return array + */ + public function messages() + { + return [ + // + ]; + } +} diff --git a/src/Http/Requests/IndexUserRequest.php b/src/Http/Requests/IndexUserRequest.php new file mode 100644 index 0000000..d90a20b --- /dev/null +++ b/src/Http/Requests/IndexUserRequest.php @@ -0,0 +1,58 @@ + + */ + public function rules(): array + { + return [ + 'search' => ['string', 'nullable', 'max:255'], + 'per_page' => ['integer', 'nullable', 'min:1', 'max:255'], + 'paginate' => ['boolean'], + 'sort' => ['string', 'nullable', 'min:2', 'max:255'], + 'dir' => ['string', 'min:3', 'max:4'], + ]; + } + + /** + * Get the validation attributes that apply to the request. + * + * @return array + */ + public function attributes() + { + return [ + // + ]; + } + + /** + * Get the validation messages that apply to the request. + * + * @return array + */ + public function messages() + { + return [ + // + ]; + } +} diff --git a/src/Http/Requests/StorePermissionRequest.php b/src/Http/Requests/StorePermissionRequest.php new file mode 100644 index 0000000..7ab7c76 --- /dev/null +++ b/src/Http/Requests/StorePermissionRequest.php @@ -0,0 +1,53 @@ + + */ + public function rules(): array + { + return [ + // + ]; + } + + /** + * Get the validation attributes that apply to the request. + * + * @return array + */ + public function attributes() + { + return [ + // + ]; + } + + /** + * Get the validation messages that apply to the request. + * + * @return array + */ + public function messages() + { + return [ + // + ]; + } +} diff --git a/src/Http/Requests/StoreProfileRequest.php b/src/Http/Requests/StoreProfileRequest.php new file mode 100644 index 0000000..496a3d4 --- /dev/null +++ b/src/Http/Requests/StoreProfileRequest.php @@ -0,0 +1,53 @@ + + */ + public function rules(): array + { + return [ + // + ]; + } + + /** + * Get the validation attributes that apply to the request. + * + * @return array + */ + public function attributes() + { + return [ + // + ]; + } + + /** + * Get the validation messages that apply to the request. + * + * @return array + */ + public function messages() + { + return [ + // + ]; + } +} diff --git a/src/Http/Requests/StoreRoleRequest.php b/src/Http/Requests/StoreRoleRequest.php new file mode 100644 index 0000000..bc1b217 --- /dev/null +++ b/src/Http/Requests/StoreRoleRequest.php @@ -0,0 +1,53 @@ + + */ + public function rules(): array + { + return [ + // + ]; + } + + /** + * Get the validation attributes that apply to the request. + * + * @return array + */ + public function attributes() + { + return [ + // + ]; + } + + /** + * Get the validation messages that apply to the request. + * + * @return array + */ + public function messages() + { + return [ + // + ]; + } +} diff --git a/src/Http/Requests/StoreTeamRequest.php b/src/Http/Requests/StoreTeamRequest.php new file mode 100644 index 0000000..339e5fa --- /dev/null +++ b/src/Http/Requests/StoreTeamRequest.php @@ -0,0 +1,53 @@ + + */ + public function rules(): array + { + return [ + // + ]; + } + + /** + * Get the validation attributes that apply to the request. + * + * @return array + */ + public function attributes() + { + return [ + // + ]; + } + + /** + * Get the validation messages that apply to the request. + * + * @return array + */ + public function messages() + { + return [ + // + ]; + } +} diff --git a/src/Http/Requests/StoreUserRequest.php b/src/Http/Requests/StoreUserRequest.php new file mode 100644 index 0000000..aa18e7e --- /dev/null +++ b/src/Http/Requests/StoreUserRequest.php @@ -0,0 +1,53 @@ + + */ + public function rules(): array + { + return [ + // + ]; + } + + /** + * Get the validation attributes that apply to the request. + * + * @return array + */ + public function attributes() + { + return [ + // + ]; + } + + /** + * Get the validation messages that apply to the request. + * + * @return array + */ + public function messages() + { + return [ + // + ]; + } +} diff --git a/src/Http/Requests/UpdatePermissionRequest.php b/src/Http/Requests/UpdatePermissionRequest.php new file mode 100644 index 0000000..3ff7592 --- /dev/null +++ b/src/Http/Requests/UpdatePermissionRequest.php @@ -0,0 +1,53 @@ + + */ + public function rules(): array + { + return [ + // + ]; + } + + /** + * Get the validation attributes that apply to the request. + * + * @return array + */ + public function attributes() + { + return [ + // + ]; + } + + /** + * Get the validation messages that apply to the request. + * + * @return array + */ + public function messages() + { + return [ + // + ]; + } +} diff --git a/src/Http/Requests/UpdateProfileRequest.php b/src/Http/Requests/UpdateProfileRequest.php new file mode 100644 index 0000000..693315c --- /dev/null +++ b/src/Http/Requests/UpdateProfileRequest.php @@ -0,0 +1,53 @@ + + */ + public function rules(): array + { + return [ + // + ]; + } + + /** + * Get the validation attributes that apply to the request. + * + * @return array + */ + public function attributes() + { + return [ + // + ]; + } + + /** + * Get the validation messages that apply to the request. + * + * @return array + */ + public function messages() + { + return [ + // + ]; + } +} diff --git a/src/Http/Requests/UpdateRoleRequest.php b/src/Http/Requests/UpdateRoleRequest.php new file mode 100644 index 0000000..d0dd7f3 --- /dev/null +++ b/src/Http/Requests/UpdateRoleRequest.php @@ -0,0 +1,53 @@ + + */ + public function rules(): array + { + return [ + // + ]; + } + + /** + * Get the validation attributes that apply to the request. + * + * @return array + */ + public function attributes() + { + return [ + // + ]; + } + + /** + * Get the validation messages that apply to the request. + * + * @return array + */ + public function messages() + { + return [ + // + ]; + } +} diff --git a/src/Http/Requests/UpdateTeamRequest.php b/src/Http/Requests/UpdateTeamRequest.php new file mode 100644 index 0000000..430ee6f --- /dev/null +++ b/src/Http/Requests/UpdateTeamRequest.php @@ -0,0 +1,53 @@ + + */ + public function rules(): array + { + return [ + // + ]; + } + + /** + * Get the validation attributes that apply to the request. + * + * @return array + */ + public function attributes() + { + return [ + // + ]; + } + + /** + * Get the validation messages that apply to the request. + * + * @return array + */ + public function messages() + { + return [ + // + ]; + } +} diff --git a/src/Http/Requests/UpdateUserRequest.php b/src/Http/Requests/UpdateUserRequest.php new file mode 100644 index 0000000..bddb26e --- /dev/null +++ b/src/Http/Requests/UpdateUserRequest.php @@ -0,0 +1,53 @@ + + */ + public function rules(): array + { + return [ + // + ]; + } + + /** + * Get the validation attributes that apply to the request. + * + * @return array + */ + public function attributes() + { + return [ + // + ]; + } + + /** + * Get the validation messages that apply to the request. + * + * @return array + */ + public function messages() + { + return [ + // + ]; + } +} diff --git a/src/Http/Resources/PermissionCollection.php b/src/Http/Resources/PermissionCollection.php new file mode 100644 index 0000000..51a5657 --- /dev/null +++ b/src/Http/Resources/PermissionCollection.php @@ -0,0 +1,35 @@ + + */ + public function with(Request $request): array + { + return [ + 'meta' => [ + 'query' => $request->all(), + ], + ]; + } +} diff --git a/src/Http/Resources/PermissionResource.php b/src/Http/Resources/PermissionResource.php new file mode 100644 index 0000000..8591fed --- /dev/null +++ b/src/Http/Resources/PermissionResource.php @@ -0,0 +1,19 @@ + + */ + public function with(Request $request): array + { + return [ + 'meta' => [ + 'query' => $request->all(), + ], + ]; + } +} diff --git a/src/Http/Resources/RoleCollection.php b/src/Http/Resources/RoleCollection.php new file mode 100644 index 0000000..ca8f679 --- /dev/null +++ b/src/Http/Resources/RoleCollection.php @@ -0,0 +1,35 @@ + + */ + public function with(Request $request): array + { + return [ + 'meta' => [ + 'query' => $request->all(), + ], + ]; + } +} diff --git a/src/Http/Resources/RoleResource.php b/src/Http/Resources/RoleResource.php new file mode 100644 index 0000000..4f438e8 --- /dev/null +++ b/src/Http/Resources/RoleResource.php @@ -0,0 +1,19 @@ + + */ + public function with(Request $request): array + { + return [ + 'meta' => [ + 'query' => $request->all(), + ], + ]; + } +} diff --git a/src/Http/Resources/TeamResource.php b/src/Http/Resources/TeamResource.php new file mode 100644 index 0000000..9487ccb --- /dev/null +++ b/src/Http/Resources/TeamResource.php @@ -0,0 +1,19 @@ + + */ + public function with(Request $request): array + { + return [ + 'meta' => [ + 'query' => $request->all(), + ], + ]; + } +} diff --git a/src/Http/Resources/UserProfileResource.php b/src/Http/Resources/UserProfileResource.php new file mode 100644 index 0000000..7ab7bff --- /dev/null +++ b/src/Http/Resources/UserProfileResource.php @@ -0,0 +1,19 @@ +make(config('auth.country_model', \App\Models\Country::class)); + + if (!$model instanceof Model) { + throw new InvalidArgumentException("Eloquent repository require model class to be `Illuminate\Database\Eloquent\Model` instance."); + } + + $this->model = $model; + } + + /** + * return a list or pagination of items from + * filtered options + * + * @param array $filters + * @return LengthAwarePaginator|Builder[]|Collection + */ + public function list(array $filters = []) + { + $query = $this->model->newQuery(); + + //Handle Sorting + $query->orderBy($filters['sort'] ?? $this->model->getKeyName(), $filters['direction'] ?? 'asc'); + + //Prepare Output + return (isset($filters['paginate']) && $filters['paginate'] == true) + ? $query->paginate(($filters['per_page'] ?? 20)) + : $query->get(); + + } + + /** + * Create a new entry resource + * + * @param array $attributes + * @return Model|null + * @throws PermissionRepositoryException + */ + public function create(array $attributes = []) + { + try { + if ($this->model->saveOrFail($attributes)) { + + $this->model->refresh(); + + return $this->model; + } + } catch (\Throwable $e) { + + throw new PermissionRepositoryException($e->getMessage(), 0, $e); + } + + return null; + } + + /** + * find and update a resource attributes + * + * @param int|string $id + * @param array $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 string|int $id + * @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 + * + * @param string|int $id + * @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 + * + * @param string|int $id + * @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/RoleRepository.php b/src/Repositories/Eloquent/RoleRepository.php new file mode 100644 index 0000000..c9de2da --- /dev/null +++ b/src/Repositories/Eloquent/RoleRepository.php @@ -0,0 +1,205 @@ +make(config('auth.country_model', \App\Models\Country::class)); + + if (!$model instanceof Model) { + throw new InvalidArgumentException("Eloquent repository require model class to be `Illuminate\Database\Eloquent\Model` instance."); + } + + $this->model = $model; + } + + /** + * return a list or pagination of items from + * filtered options + * + * @param array $filters + * @return LengthAwarePaginator|Builder[]|Collection + */ + public function list(array $filters = []) + { + $query = $this->model->newQuery(); + + //Handle Sorting + $query->orderBy($filters['sort'] ?? $this->model->getKeyName(), $filters['direction'] ?? 'asc'); + + //Prepare Output + return (isset($filters['paginate']) && $filters['paginate'] == true) + ? $query->paginate(($filters['per_page'] ?? 20)) + : $query->get(); + + } + + /** + * Create a new entry resource + * + * @param array $attributes + * @return Model|null + * @throws RoleRepositoryException + */ + public function create(array $attributes = []) + { + try { + if ($this->model->saveOrFail($attributes)) { + + $this->model->refresh(); + + return $this->model; + } + } catch (\Throwable $e) { + + throw new RoleRepositoryException($e->getMessage(), 0, $e); + } + + return null; + } + + /** + * find and update a resource attributes + * + * @param int|string $id + * @param array $attributes + * @return Model|null + * @throws RoleRepositoryException + */ + 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 RoleRepositoryException($exception->getMessage(), 0, $exception); + } + + return null; + } + + /** + * find and delete a entry from records + * + * @param string|int $id + * @param bool $onlyTrashed + * @return bool|null + * @throws RoleRepositoryException + */ + 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 RoleRepositoryException($exception->getMessage(), 0, $exception); + } + + return null; + } + + /** + * find and delete a entry from records + * + * @param string|int $id + * @return bool|null + * @throws RoleRepositoryException + */ + 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 RoleRepositoryException($exception->getMessage(), 0, $exception); + } + + return null; + } + + /** + * find and restore a entry from records + * + * @param string|int $id + * @return bool|null + * @throws RoleRepositoryException + */ + 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 RoleRepositoryException($exception->getMessage(), 0, $exception); + } + + return null; + } +} diff --git a/src/Repositories/Eloquent/TeamRepository.php b/src/Repositories/Eloquent/TeamRepository.php new file mode 100644 index 0000000..13f580a --- /dev/null +++ b/src/Repositories/Eloquent/TeamRepository.php @@ -0,0 +1,205 @@ +make(config('auth.country_model', \App\Models\Country::class)); + + if (!$model instanceof Model) { + throw new InvalidArgumentException("Eloquent repository require model class to be `Illuminate\Database\Eloquent\Model` instance."); + } + + $this->model = $model; + } + + /** + * return a list or pagination of items from + * filtered options + * + * @param array $filters + * @return LengthAwarePaginator|Builder[]|Collection + */ + public function list(array $filters = []) + { + $query = $this->model->newQuery(); + + //Handle Sorting + $query->orderBy($filters['sort'] ?? $this->model->getKeyName(), $filters['direction'] ?? 'asc'); + + //Prepare Output + return (isset($filters['paginate']) && $filters['paginate'] == true) + ? $query->paginate(($filters['per_page'] ?? 20)) + : $query->get(); + + } + + /** + * Create a new entry resource + * + * @param array $attributes + * @return Model|null + * @throws TeamRepositoryException + */ + public function create(array $attributes = []) + { + try { + if ($this->model->saveOrFail($attributes)) { + + $this->model->refresh(); + + return $this->model; + } + } catch (\Throwable $e) { + + throw new TeamRepositoryException($e->getMessage(), 0, $e); + } + + return null; + } + + /** + * find and update a resource attributes + * + * @param int|string $id + * @param array $attributes + * @return Model|null + * @throws TeamRepositoryException + */ + 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 TeamRepositoryException($exception->getMessage(), 0, $exception); + } + + return null; + } + + /** + * find and delete a entry from records + * + * @param string|int $id + * @param bool $onlyTrashed + * @return bool|null + * @throws TeamRepositoryException + */ + 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 TeamRepositoryException($exception->getMessage(), 0, $exception); + } + + return null; + } + + /** + * find and delete a entry from records + * + * @param string|int $id + * @return bool|null + * @throws TeamRepositoryException + */ + 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 TeamRepositoryException($exception->getMessage(), 0, $exception); + } + + return null; + } + + /** + * find and restore a entry from records + * + * @param string|int $id + * @return bool|null + * @throws TeamRepositoryException + */ + 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 TeamRepositoryException($exception->getMessage(), 0, $exception); + } + + return null; + } +} diff --git a/src/Repositories/Eloquent/UserProfileRepository.php b/src/Repositories/Eloquent/UserProfileRepository.php new file mode 100644 index 0000000..efb0f43 --- /dev/null +++ b/src/Repositories/Eloquent/UserProfileRepository.php @@ -0,0 +1,205 @@ +make(config('auth.country_model', \App\Models\Country::class)); + + if (!$model instanceof Model) { + throw new InvalidArgumentException("Eloquent repository require model class to be `Illuminate\Database\Eloquent\Model` instance."); + } + + $this->model = $model; + } + + /** + * return a list or pagination of items from + * filtered options + * + * @param array $filters + * @return LengthAwarePaginator|Builder[]|Collection + */ + public function list(array $filters = []) + { + $query = $this->model->newQuery(); + + //Handle Sorting + $query->orderBy($filters['sort'] ?? $this->model->getKeyName(), $filters['direction'] ?? 'asc'); + + //Prepare Output + return (isset($filters['paginate']) && $filters['paginate'] == true) + ? $query->paginate(($filters['per_page'] ?? 20)) + : $query->get(); + + } + + /** + * Create a new entry resource + * + * @param array $attributes + * @return Model|null + * @throws UserProfileRepositoryException + */ + public function create(array $attributes = []) + { + try { + if ($this->model->saveOrFail($attributes)) { + + $this->model->refresh(); + + return $this->model; + } + } catch (\Throwable $e) { + + throw new UserProfileRepositoryException($e->getMessage(), 0, $e); + } + + return null; + } + + /** + * find and update a resource attributes + * + * @param int|string $id + * @param array $attributes + * @return Model|null + * @throws UserProfileRepositoryException + */ + 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 UserProfileRepositoryException($exception->getMessage(), 0, $exception); + } + + return null; + } + + /** + * find and delete a entry from records + * + * @param string|int $id + * @param bool $onlyTrashed + * @return bool|null + * @throws UserProfileRepositoryException + */ + 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 UserProfileRepositoryException($exception->getMessage(), 0, $exception); + } + + return null; + } + + /** + * find and delete a entry from records + * + * @param string|int $id + * @return bool|null + * @throws UserProfileRepositoryException + */ + 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 UserProfileRepositoryException($exception->getMessage(), 0, $exception); + } + + return null; + } + + /** + * find and restore a entry from records + * + * @param string|int $id + * @return bool|null + * @throws UserProfileRepositoryException + */ + 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 UserProfileRepositoryException($exception->getMessage(), 0, $exception); + } + + return null; + } +} diff --git a/src/Repositories/Eloquent/UserRepository.php b/src/Repositories/Eloquent/UserRepository.php new file mode 100644 index 0000000..a19e9ca --- /dev/null +++ b/src/Repositories/Eloquent/UserRepository.php @@ -0,0 +1,205 @@ +make(config('auth.country_model', \App\Models\Country::class)); + + if (!$model instanceof Model) { + throw new InvalidArgumentException("Eloquent repository require model class to be `Illuminate\Database\Eloquent\Model` instance."); + } + + $this->model = $model; + } + + /** + * return a list or pagination of items from + * filtered options + * + * @param array $filters + * @return LengthAwarePaginator|Builder[]|Collection + */ + public function list(array $filters = []) + { + $query = $this->model->newQuery(); + + //Handle Sorting + $query->orderBy($filters['sort'] ?? $this->model->getKeyName(), $filters['direction'] ?? 'asc'); + + //Prepare Output + return (isset($filters['paginate']) && $filters['paginate'] == true) + ? $query->paginate(($filters['per_page'] ?? 20)) + : $query->get(); + + } + + /** + * Create a new entry resource + * + * @param array $attributes + * @return Model|null + * @throws UserRepositoryException + */ + public function create(array $attributes = []) + { + try { + if ($this->model->saveOrFail($attributes)) { + + $this->model->refresh(); + + return $this->model; + } + } catch (\Throwable $e) { + + throw new UserRepositoryException($e->getMessage(), 0, $e); + } + + return null; + } + + /** + * find and update a resource attributes + * + * @param int|string $id + * @param array $attributes + * @return Model|null + * @throws UserRepositoryException + */ + 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 UserRepositoryException($exception->getMessage(), 0, $exception); + } + + return null; + } + + /** + * find and delete a entry from records + * + * @param string|int $id + * @param bool $onlyTrashed + * @return bool|null + * @throws UserRepositoryException + */ + 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 UserRepositoryException($exception->getMessage(), 0, $exception); + } + + return null; + } + + /** + * find and delete a entry from records + * + * @param string|int $id + * @return bool|null + * @throws UserRepositoryException + */ + 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 UserRepositoryException($exception->getMessage(), 0, $exception); + } + + return null; + } + + /** + * find and restore a entry from records + * + * @param string|int $id + * @return bool|null + * @throws UserRepositoryException + */ + 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 UserRepositoryException($exception->getMessage(), 0, $exception); + } + + return null; + } +} diff --git a/src/Repositories/Mongodb/PermissionRepository.php b/src/Repositories/Mongodb/PermissionRepository.php new file mode 100644 index 0000000..2441221 --- /dev/null +++ b/src/Repositories/Mongodb/PermissionRepository.php @@ -0,0 +1,205 @@ +make(config('auth.country_model', \App\Models\Country::class)); + + if (!$model instanceof Model) { + throw new InvalidArgumentException("Eloquent repository require model class to be `Illuminate\Database\Eloquent\Model` instance."); + } + + $this->model = $model; + } + + /** + * return a list or pagination of items from + * filtered options + * + * @param array $filters + * @return LengthAwarePaginator|Builder[]|Collection + */ + public function list(array $filters = []) + { + $query = $this->model->newQuery(); + + //Handle Sorting + $query->orderBy($filters['sort'] ?? $this->model->getKeyName(), $filters['direction'] ?? 'asc'); + + //Prepare Output + return (isset($filters['paginate']) && $filters['paginate'] == true) + ? $query->paginate(($filters['per_page'] ?? 20)) + : $query->get(); + + } + + /** + * Create a new entry resource + * + * @param array $attributes + * @return Model|null + * @throws PermissionRepositoryException + */ + public function create(array $attributes = []) + { + try { + if ($this->model->saveOrFail($attributes)) { + + $this->model->refresh(); + + return $this->model; + } + } catch (\Throwable $e) { + + throw new PermissionRepositoryException($e->getMessage(), 0, $e); + } + + return null; + } + + /** + * find and update a resource attributes + * + * @param int|string $id + * @param array $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 string|int $id + * @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 + * + * @param string|int $id + * @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 + * + * @param string|int $id + * @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/Mongodb/RoleRepository.php b/src/Repositories/Mongodb/RoleRepository.php new file mode 100644 index 0000000..1852b98 --- /dev/null +++ b/src/Repositories/Mongodb/RoleRepository.php @@ -0,0 +1,205 @@ +make(config('auth.country_model', \App\Models\Country::class)); + + if (!$model instanceof Model) { + throw new InvalidArgumentException("Eloquent repository require model class to be `Illuminate\Database\Eloquent\Model` instance."); + } + + $this->model = $model; + } + + /** + * return a list or pagination of items from + * filtered options + * + * @param array $filters + * @return LengthAwarePaginator|Builder[]|Collection + */ + public function list(array $filters = []) + { + $query = $this->model->newQuery(); + + //Handle Sorting + $query->orderBy($filters['sort'] ?? $this->model->getKeyName(), $filters['direction'] ?? 'asc'); + + //Prepare Output + return (isset($filters['paginate']) && $filters['paginate'] == true) + ? $query->paginate(($filters['per_page'] ?? 20)) + : $query->get(); + + } + + /** + * Create a new entry resource + * + * @param array $attributes + * @return Model|null + * @throws RoleRepositoryException + */ + public function create(array $attributes = []) + { + try { + if ($this->model->saveOrFail($attributes)) { + + $this->model->refresh(); + + return $this->model; + } + } catch (\Throwable $e) { + + throw new RoleRepositoryException($e->getMessage(), 0, $e); + } + + return null; + } + + /** + * find and update a resource attributes + * + * @param int|string $id + * @param array $attributes + * @return Model|null + * @throws RoleRepositoryException + */ + 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 RoleRepositoryException($exception->getMessage(), 0, $exception); + } + + return null; + } + + /** + * find and delete a entry from records + * + * @param string|int $id + * @param bool $onlyTrashed + * @return bool|null + * @throws RoleRepositoryException + */ + 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 RoleRepositoryException($exception->getMessage(), 0, $exception); + } + + return null; + } + + /** + * find and delete a entry from records + * + * @param string|int $id + * @return bool|null + * @throws RoleRepositoryException + */ + 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 RoleRepositoryException($exception->getMessage(), 0, $exception); + } + + return null; + } + + /** + * find and restore a entry from records + * + * @param string|int $id + * @return bool|null + * @throws RoleRepositoryException + */ + 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 RoleRepositoryException($exception->getMessage(), 0, $exception); + } + + return null; + } +} diff --git a/src/Repositories/Mongodb/TeamRepository.php b/src/Repositories/Mongodb/TeamRepository.php new file mode 100644 index 0000000..e9156c0 --- /dev/null +++ b/src/Repositories/Mongodb/TeamRepository.php @@ -0,0 +1,205 @@ +make(config('auth.country_model', \App\Models\Country::class)); + + if (!$model instanceof Model) { + throw new InvalidArgumentException("Eloquent repository require model class to be `Illuminate\Database\Eloquent\Model` instance."); + } + + $this->model = $model; + } + + /** + * return a list or pagination of items from + * filtered options + * + * @param array $filters + * @return LengthAwarePaginator|Builder[]|Collection + */ + public function list(array $filters = []) + { + $query = $this->model->newQuery(); + + //Handle Sorting + $query->orderBy($filters['sort'] ?? $this->model->getKeyName(), $filters['direction'] ?? 'asc'); + + //Prepare Output + return (isset($filters['paginate']) && $filters['paginate'] == true) + ? $query->paginate(($filters['per_page'] ?? 20)) + : $query->get(); + + } + + /** + * Create a new entry resource + * + * @param array $attributes + * @return Model|null + * @throws TeamRepositoryException + */ + public function create(array $attributes = []) + { + try { + if ($this->model->saveOrFail($attributes)) { + + $this->model->refresh(); + + return $this->model; + } + } catch (\Throwable $e) { + + throw new TeamRepositoryException($e->getMessage(), 0, $e); + } + + return null; + } + + /** + * find and update a resource attributes + * + * @param int|string $id + * @param array $attributes + * @return Model|null + * @throws TeamRepositoryException + */ + 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 TeamRepositoryException($exception->getMessage(), 0, $exception); + } + + return null; + } + + /** + * find and delete a entry from records + * + * @param string|int $id + * @param bool $onlyTrashed + * @return bool|null + * @throws TeamRepositoryException + */ + 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 TeamRepositoryException($exception->getMessage(), 0, $exception); + } + + return null; + } + + /** + * find and delete a entry from records + * + * @param string|int $id + * @return bool|null + * @throws TeamRepositoryException + */ + 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 TeamRepositoryException($exception->getMessage(), 0, $exception); + } + + return null; + } + + /** + * find and restore a entry from records + * + * @param string|int $id + * @return bool|null + * @throws TeamRepositoryException + */ + 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 TeamRepositoryException($exception->getMessage(), 0, $exception); + } + + return null; + } +} diff --git a/src/Repositories/Mongodb/UserProfileRepository.php b/src/Repositories/Mongodb/UserProfileRepository.php new file mode 100644 index 0000000..e069e05 --- /dev/null +++ b/src/Repositories/Mongodb/UserProfileRepository.php @@ -0,0 +1,205 @@ +make(config('auth.country_model', \App\Models\Country::class)); + + if (!$model instanceof Model) { + throw new InvalidArgumentException("Eloquent repository require model class to be `Illuminate\Database\Eloquent\Model` instance."); + } + + $this->model = $model; + } + + /** + * return a list or pagination of items from + * filtered options + * + * @param array $filters + * @return LengthAwarePaginator|Builder[]|Collection + */ + public function list(array $filters = []) + { + $query = $this->model->newQuery(); + + //Handle Sorting + $query->orderBy($filters['sort'] ?? $this->model->getKeyName(), $filters['direction'] ?? 'asc'); + + //Prepare Output + return (isset($filters['paginate']) && $filters['paginate'] == true) + ? $query->paginate(($filters['per_page'] ?? 20)) + : $query->get(); + + } + + /** + * Create a new entry resource + * + * @param array $attributes + * @return Model|null + * @throws UserProfileRepositoryException + */ + public function create(array $attributes = []) + { + try { + if ($this->model->saveOrFail($attributes)) { + + $this->model->refresh(); + + return $this->model; + } + } catch (\Throwable $e) { + + throw new UserProfileRepositoryException($e->getMessage(), 0, $e); + } + + return null; + } + + /** + * find and update a resource attributes + * + * @param int|string $id + * @param array $attributes + * @return Model|null + * @throws UserProfileRepositoryException + */ + 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 UserProfileRepositoryException($exception->getMessage(), 0, $exception); + } + + return null; + } + + /** + * find and delete a entry from records + * + * @param string|int $id + * @param bool $onlyTrashed + * @return bool|null + * @throws UserProfileRepositoryException + */ + 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 UserProfileRepositoryException($exception->getMessage(), 0, $exception); + } + + return null; + } + + /** + * find and delete a entry from records + * + * @param string|int $id + * @return bool|null + * @throws UserProfileRepositoryException + */ + 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 UserProfileRepositoryException($exception->getMessage(), 0, $exception); + } + + return null; + } + + /** + * find and restore a entry from records + * + * @param string|int $id + * @return bool|null + * @throws UserProfileRepositoryException + */ + 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 UserProfileRepositoryException($exception->getMessage(), 0, $exception); + } + + return null; + } +} diff --git a/src/Repositories/Mongodb/UserRepository.php b/src/Repositories/Mongodb/UserRepository.php new file mode 100644 index 0000000..e710855 --- /dev/null +++ b/src/Repositories/Mongodb/UserRepository.php @@ -0,0 +1,205 @@ +make(config('auth.country_model', \App\Models\Country::class)); + + if (!$model instanceof Model) { + throw new InvalidArgumentException("Eloquent repository require model class to be `Illuminate\Database\Eloquent\Model` instance."); + } + + $this->model = $model; + } + + /** + * return a list or pagination of items from + * filtered options + * + * @param array $filters + * @return LengthAwarePaginator|Builder[]|Collection + */ + public function list(array $filters = []) + { + $query = $this->model->newQuery(); + + //Handle Sorting + $query->orderBy($filters['sort'] ?? $this->model->getKeyName(), $filters['direction'] ?? 'asc'); + + //Prepare Output + return (isset($filters['paginate']) && $filters['paginate'] == true) + ? $query->paginate(($filters['per_page'] ?? 20)) + : $query->get(); + + } + + /** + * Create a new entry resource + * + * @param array $attributes + * @return Model|null + * @throws UserRepositoryException + */ + public function create(array $attributes = []) + { + try { + if ($this->model->saveOrFail($attributes)) { + + $this->model->refresh(); + + return $this->model; + } + } catch (\Throwable $e) { + + throw new UserRepositoryException($e->getMessage(), 0, $e); + } + + return null; + } + + /** + * find and update a resource attributes + * + * @param int|string $id + * @param array $attributes + * @return Model|null + * @throws UserRepositoryException + */ + 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 UserRepositoryException($exception->getMessage(), 0, $exception); + } + + return null; + } + + /** + * find and delete a entry from records + * + * @param string|int $id + * @param bool $onlyTrashed + * @return bool|null + * @throws UserRepositoryException + */ + 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 UserRepositoryException($exception->getMessage(), 0, $exception); + } + + return null; + } + + /** + * find and delete a entry from records + * + * @param string|int $id + * @return bool|null + * @throws UserRepositoryException + */ + 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 UserRepositoryException($exception->getMessage(), 0, $exception); + } + + return null; + } + + /** + * find and restore a entry from records + * + * @param string|int $id + * @return bool|null + * @throws UserRepositoryException + */ + 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 UserRepositoryException($exception->getMessage(), 0, $exception); + } + + return null; + } +} diff --git a/src/Services/PermissionService.php b/src/Services/PermissionService.php new file mode 100644 index 0000000..0b0b874 --- /dev/null +++ b/src/Services/PermissionService.php @@ -0,0 +1,60 @@ +permissionRepository->list($filters); + + //Do Business Stuff + + return $countryList; + + } + + public function create(array $inputs = []) + { + return $this->permissionRepository->create($inputs); + } + + public function read($id) + { + return $this->permissionRepository->read($id); + } + + public function update($id, array $inputs = []) + { + return $this->permissionRepository->update($id, $inputs); + } + + public function destroy($id) + { + return $this->permissionRepository->delete($id); + } + + public function restore($id) + { + return $this->permissionRepository->restore($id); + } +} diff --git a/src/Services/RoleService.php b/src/Services/RoleService.php new file mode 100644 index 0000000..5885c11 --- /dev/null +++ b/src/Services/RoleService.php @@ -0,0 +1,60 @@ +roleRepository->list($filters); + + //Do Business Stuff + + return $countryList; + + } + + public function create(array $inputs = []) + { + return $this->roleRepository->create($inputs); + } + + public function read($id) + { + return $this->roleRepository->read($id); + } + + public function update($id, array $inputs = []) + { + return $this->roleRepository->update($id, $inputs); + } + + public function destroy($id) + { + return $this->roleRepository->delete($id); + } + + public function restore($id) + { + return $this->roleRepository->restore($id); + } +} diff --git a/src/Services/TeamService.php b/src/Services/TeamService.php new file mode 100644 index 0000000..d013176 --- /dev/null +++ b/src/Services/TeamService.php @@ -0,0 +1,60 @@ +teamRepository->list($filters); + + //Do Business Stuff + + return $countryList; + + } + + public function create(array $inputs = []) + { + return $this->teamRepository->create($inputs); + } + + public function read($id) + { + return $this->teamRepository->read($id); + } + + public function update($id, array $inputs = []) + { + return $this->teamRepository->update($id, $inputs); + } + + public function destroy($id) + { + return $this->teamRepository->delete($id); + } + + public function restore($id) + { + return $this->teamRepository->restore($id); + } +} diff --git a/src/Services/UserProfileService.php b/src/Services/UserProfileService.php new file mode 100644 index 0000000..b712aa7 --- /dev/null +++ b/src/Services/UserProfileService.php @@ -0,0 +1,60 @@ +userProfileRepository->list($filters); + + //Do Business Stuff + + return $countryList; + + } + + public function create(array $inputs = []) + { + return $this->userProfileRepository->create($inputs); + } + + public function read($id) + { + return $this->userProfileRepository->read($id); + } + + public function update($id, array $inputs = []) + { + return $this->userProfileRepository->update($id, $inputs); + } + + public function destroy($id) + { + return $this->userProfileRepository->delete($id); + } + + public function restore($id) + { + return $this->userProfileRepository->restore($id); + } +} diff --git a/src/Services/UserService.php b/src/Services/UserService.php new file mode 100644 index 0000000..dfc38c5 --- /dev/null +++ b/src/Services/UserService.php @@ -0,0 +1,60 @@ +userRepository->list($filters); + + //Do Business Stuff + + return $countryList; + + } + + public function create(array $inputs = []) + { + return $this->userRepository->create($inputs); + } + + public function read($id) + { + return $this->userRepository->read($id); + } + + public function update($id, array $inputs = []) + { + return $this->userRepository->update($id, $inputs); + } + + public function destroy($id) + { + return $this->userRepository->delete($id); + } + + public function restore($id) + { + return $this->userRepository->restore($id); + } +} diff --git a/test.php b/test.php new file mode 100644 index 0000000..301d5e1 --- /dev/null +++ b/test.php @@ -0,0 +1,3 @@ +