From 40ec38b87aebead47c5fca4ac2cd583f6cb3eb64 Mon Sep 17 00:00:00 2001 From: php anonymous Date: Sun, 21 May 2023 12:41:15 +0300 Subject: [PATCH] update and add afterShow --- composer.json | 12 +-- src/Base/Api.php | 149 ++++++++++++++++++++-------------- src/Base/Traits/Queryable.php | 11 ++- 3 files changed, 103 insertions(+), 69 deletions(-) diff --git a/composer.json b/composer.json index 0f80996..c04039d 100644 --- a/composer.json +++ b/composer.json @@ -8,11 +8,7 @@ "email":"php.anonymous1@gmail.com", "homepage":"https://github.com/arabnewscms" } - ],"require": { - "php": "^7.3|^8.0", - "laravel/framework": "^8.0", - "nwidart/laravel-modules": "^9.0" - }, + ], "license": "MIT", "autoload": { "psr-4": { @@ -24,18 +20,18 @@ }, "extra": { "branch-alias": { - "dev-master": "1.0.0.x-dev" + "dev-master": "1.0.9.x-dev" }, "laravel": { "providers": [ "Lynx\\LynxProvider" ], "aliases": { - + } } }, - "version":"1.0.0", + "version":"1.0.9", "minimum-stability": "dev", "prefer-stable": true } diff --git a/src/Base/Api.php b/src/Base/Api.php index e10115c..89ae63b 100644 --- a/src/Base/Api.php +++ b/src/Base/Api.php @@ -1,8 +1,6 @@ definePolicy(); } - protected function can($fn) { + protected function can($fn,$model) { if (class_exists($this->policy)) { - return !auth() ->guard($this->guard)->user()->can($fn.'-'.$this->policy_key, $this->entity)?lynx()->status(403) + //.'-'.$this->policy_key + // dd(\auth() ->guard($this->guard)->user()->can($fn, $model)); + + return !auth() ->guard($this->guard)->user()->can($fn, $model)?lynx()->status(403) ->message(__('lynx.need_permission')) ->response():true; } else { @@ -61,9 +62,12 @@ public function indexAny() { $data = $this->appendQuery(); $data = $this->paginateIndex?$data->paginate(request('per_page', 15)): $data->get(); - - // Resource Collect every json field and can reuse in resource - $collection = $this->resourcesJson::collection($data)->toResponse(app('request'))->getData(); + if (!empty($this->resourcesJson)) { + // Resource Collect every json field and can reuse in resource + $collection = $this->resourcesJson::collection($data)->toResponse(app('request'))->getData(); + } else { + $collection = $data; + } return lynx()->data($collection) ->status(200) @@ -76,7 +80,7 @@ public function indexAny() { * @return Renderable */ public function index() { - $can = $this->can('viewAny'); + $can = $this->can('viewAny',$this->entity); if ($can !== true) { return $can; } @@ -85,8 +89,12 @@ public function index() { $data = $this->paginateIndex?$data->paginate(request('per_page', 15)): $data->get(); - // Resource Collect every json field and can reuse in resource - $collection = $this->resourcesJson::collection($data)->toResponse(app('request'))->getData(); + if (!empty($this->resourcesJson)) { + // Resource Collect every json field and can reuse in resource + $collection = $this->resourcesJson::collection($data)->toResponse(app('request'))->getData(); + } else { + $collection = $data; + } return lynx()->data($collection) ->status(200) @@ -101,20 +109,11 @@ public function index() { */ public function store() { - $can = $this->can('create'); + $can = $this->can('create',$this->entity); if ($can !== true) { return $can; } - if (count($this->rules('store')) == 0) { - $messageForDeveloper = __('lynx.must_add_rules', [ - 'columns' => implode(',', (new $this->entity)->getFillable()), - ]); - return lynx()->status(422) - ->message($messageForDeveloper) - ->response(); - } - $this->data = $this->validate(request(), $this->rules('store'), [], method_exists($this, 'niceName')? @@ -122,10 +121,18 @@ public function store() { ); $this->data = $this->beforeStore($this->data); - - $store = $this->entity::create($this->allInputsWithoutFiles()); - $this->afterStore($store); - return lynx()->data($this->FullJsonInStore?$store:['id' => $store->id]) + $entity = $this->entity; + // $store = new $entity(); + // foreach ($this->allInputsWithoutFiles() as $k => $v) { + // $store->$k = $v; + // } + + //$storeSave = $store->save(); + //dd($this->allInputsWithoutFiles()); + $storeSave = $entity::create($this->allInputsWithoutFiles()); + //dd($storeSave); + $this->afterStore($storeSave); + return lynx()->data($this->FullJsonInStore?$storeSave:['id' => $storeSave->id]) ->status(200) ->message(__('lynx.recored_added')) ->response(); @@ -137,17 +144,19 @@ public function store() { * @return Renderable */ public function show($id) { - $can = $this->can('view'); + $data = $this->appendShowQuery()->where('id', $id)->first(); + $can = $this->can('view',$data); if ($can !== true) { return $can; } - $data = $this->appendShowQuery()->where('id', $id)->first(); + if (is_null($data)) { return lynx()->status(404) ->message(__('lynx.not_found')) ->response(); } else { + $data = $this->afterShow($data); return lynx()->data($data)->response(); } } @@ -159,38 +168,54 @@ public function show($id) { * @return Renderable */ public function update($id) { - - $can = $this->can('update'); + $data = $this->entity::find($id); + $can = $this->can('update',$data); if ($can !== true) { return $can; } - if (count($this->rules('update', $id)) == 0) { - $messageForDeveloper = 'must be add rules method on your parent class or following fillable columns '.implode(',', (new $this->entity)->getFillable()); - return lynx()->status(422) - ->message($messageForDeveloper) - ->response(); - } - // Check Record is exist - if (is_null($data = $this->entity::find($id))) { + if (is_null($data)) { return lynx() ->status(404) ->message(__('lynx.not_found')) ->response(); } // Validation Errors + $this->data = $this->validate(request(), $this->rules('update'), [], - method_exists($this->entity, 'niceName')? - $this->entity::niceName():[] + method_exists($this, 'niceName')? + $this->niceName():[] ); $this->beforeUpdate($data); - - $update = $this->entity::where('id', $id)->update($this->allInputsWithoutFiles()); - $this->afterUpdate($data = $this->entity::find($id)); - return lynx()->data($this->FullJsonInUpdate?$data:['id' => $data->id]) + $fillability = []; + + foreach($this->allInputsWithoutFiles() as $key=>$val){ +if(in_array($key, app($this->entity)->getFillable())) { + $fillability[$key] = request($key); +} + } + + + $update = $this->entity::where('id',$id)->update($fillability); + + // foreach ($this->allInputsWithoutFiles() as $k => $v) { + // $update->{ $k} = $v; + // } + // $update->save(); + $data = $this->entity::find($id); + + $this->afterUpdate($data); + return lynx()->data( + $this->FullJsonInUpdate? + $data + : + [ + 'id' => $data->id, + ] + ) ->status(200) ->message(__('lynx.recored_updated')) ->response(); @@ -203,17 +228,19 @@ public function update($id) { */ public function destroy($id) { - $can = $this->can('delete'); - if ($can !== true) { - return $can; - } - - if ($this->withTrashed) { + if ($this->withTrashed) { $data = $this->entity::withTrashed()->find($id); } else { $data = $this->entity::find($id); } + $can = $this->can('delete',$data); + if ($can !== true) { + return $can; + } + + + // Check Record is exist if (is_null($data)) { return lynx() @@ -240,17 +267,18 @@ public function destroy($id) { */ public function forceDelete($id) { - $can = $this->can('forceDelete'); - if ($can !== true) { - return $can; - } - if ($this->withTrashed) { $data = $this->entity::withTrashed()->find($id); } else { $data = $this->entity::find($id); } + $can = $this->can('forceDelete',$data); + if ($can !== true) { + return $can; + } + + // Check Record is exist if (is_null($data)) { return lynx() @@ -273,17 +301,18 @@ public function forceDelete($id) { * @return Renderable */ public function restore($id) { + if ($this->withTrashed) { + $data = $this->entity::withTrashed()->find($id); + } else { + $data = $this->entity::find($id); + } - $can = $this->can('restore'); + $can = $this->can('restore',$data); if ($can !== true) { return $can; } - if ($this->withTrashed) { - $data = $this->entity::withTrashed()->find($id); - } else { - $data = $this->entity::find($id); - } + // Check Record is exist if (is_null($data)) { @@ -302,4 +331,4 @@ public function restore($id) { ->response(); } -} \ No newline at end of file +} diff --git a/src/Base/Traits/Queryable.php b/src/Base/Traits/Queryable.php index 7e33e64..fc36c59 100644 --- a/src/Base/Traits/Queryable.php +++ b/src/Base/Traits/Queryable.php @@ -47,6 +47,15 @@ public function beforeShow($entity):Object { return $entity; } + /** + * master of query method + * @return query methods + */ + public function afterShow($entity):Object { + // return new $this->resourcesJson($entity); + return $entity; + } + /** * master of query method * @return query methods @@ -90,4 +99,4 @@ public function appendShowQuery() { return $this->beforeShow(new $this->entity)->orderBy(request('orderBy', 'id'), request('sort', 'desc')); } -} \ No newline at end of file +}