From 9163f91f24ae3303067a461c5cc36954f48eca83 Mon Sep 17 00:00:00 2001 From: Lupacescu Eduard Date: Tue, 21 Jan 2020 14:39:58 +0200 Subject: [PATCH 01/12] Plain custom CRUD --- .../Requests/InteractWithRepositories.php | 31 +++-- src/Repositories/Crudable.php | 129 ++++++++++++++++-- 2 files changed, 135 insertions(+), 25 deletions(-) diff --git a/src/Http/Requests/InteractWithRepositories.php b/src/Http/Requests/InteractWithRepositories.php index c426bfc1c..18a4df474 100644 --- a/src/Http/Requests/InteractWithRepositories.php +++ b/src/Http/Requests/InteractWithRepositories.php @@ -31,13 +31,12 @@ public function authorize() /** * Get the class name of the repository being requested. * + * @param null $key * @return Repository - * @throws EntityNotFoundException - * @throws UnauthorizedException */ - public function repository() + public function repository($key = null) { - return tap(Restify::repositoryForKey($this->route('repository')), function ($repository) { + return tap(Restify::repositoryForKey($key ?? $this->route('repository')), function ($repository) { if (is_null($repository)) { throw new EntityNotFoundException(__('Repository :name not found.', [ 'name' => $repository, @@ -109,11 +108,12 @@ public function isResolvedByRestify() * As a model it could accept either a model instance, a collection or even paginated collection. * * @param $model + * @param null $uriKey * @return Repository */ - public function newRepositoryWith($model) + public function newRepositoryWith($model, $uriKey = null) { - $repository = $this->repository(); + $repository = $this->repository($uriKey); return $repository::resolveWith($model); } @@ -121,25 +121,27 @@ public function newRepositoryWith($model) /** * Get a new, scopeless query builder for the underlying model. * + * @param null $uriKey * @return \Illuminate\Database\Eloquent\Builder * @throws EntityNotFoundException * @throws UnauthorizedException */ - public function newQueryWithoutScopes() + public function newQueryWithoutScopes($uriKey = null) { - return $this->model()->newQueryWithoutScopes(); + return $this->model($uriKey)->newQueryWithoutScopes(); } /** * Get a new instance of the underlying model. * + * @param null $uriKey * @return \Illuminate\Database\Eloquent\Model * @throws EntityNotFoundException * @throws UnauthorizedException */ - public function model() + public function model($uriKey = null) { - $repository = $this->repository(); + $repository = $this->repository($uriKey); return $repository::newModel(); } @@ -147,12 +149,15 @@ public function model() /** * Get the query to find the model instance for the request. * - * @param mixed|null $repositoryId + * @param mixed|null $repositoryId + * @param null $uriKey * @return \Illuminate\Database\Eloquent\Builder + * @throws EntityNotFoundException + * @throws UnauthorizedException */ - public function findModelQuery($repositoryId = null) + public function findModelQuery($repositoryId = null, $uriKey = null) { - return $this->newQueryWithoutScopes()->whereKey( + return $this->newQueryWithoutScopes($uriKey)->whereKey( $repositoryId ?? request('repositoryId') ); } diff --git a/src/Repositories/Crudable.php b/src/Repositories/Crudable.php index 3f53c80a4..5deb023b9 100644 --- a/src/Repositories/Crudable.php +++ b/src/Repositories/Crudable.php @@ -5,6 +5,9 @@ use Binaryk\LaravelRestify\Contracts\RestifySearchable; use Binaryk\LaravelRestify\Controllers\RestResponse; use Binaryk\LaravelRestify\Exceptions\UnauthorizedException; +use Binaryk\LaravelRestify\Http\Requests\RepositoryDestroyRequest; +use Binaryk\LaravelRestify\Http\Requests\RepositoryStoreRequest; +use Binaryk\LaravelRestify\Http\Requests\RepositoryUpdateRequest; use Binaryk\LaravelRestify\Http\Requests\RestifyRequest; use Binaryk\LaravelRestify\Restify; use Binaryk\LaravelRestify\Services\Search\SearchService; @@ -21,7 +24,7 @@ trait Crudable { /** - * @param RestifyRequest $request + * @param RestifyRequest $request * @return JsonResponse * @throws \Binaryk\LaravelRestify\Exceptions\InstanceOfException * @throws \Throwable @@ -62,7 +65,7 @@ public function index(RestifyRequest $request) } /** - * @param RestifyRequest $request + * @param RestifyRequest $request * @return JsonResponse */ public function show(RestifyRequest $request, $repositoryId) @@ -84,7 +87,7 @@ public function show(RestifyRequest $request, $repositoryId) } /** - * @param RestifyRequest $request + * @param RestifyRequest $request * @return JsonResponse */ public function store(RestifyRequest $request) @@ -108,13 +111,15 @@ public function store(RestifyRequest $request) return $model; }); + $this->resource = $model; + return $this->response('', RestResponse::REST_RESPONSE_CREATED_CODE) ->model($model) - ->header('Location', Restify::path().'/'.static::uriKey().'/'.$model->id); + ->header('Location', Restify::path() . '/' . static::uriKey() . '/' . $model->id); } /** - * @param RestifyRequest $request + * @param RestifyRequest $request * @param $model * @return JsonResponse * @throws \Illuminate\Auth\Access\AuthorizationException @@ -124,19 +129,19 @@ public function update(RestifyRequest $request, $repositoryId) { $this->allowToUpdate($request); - DB::transaction(function () use ($request) { + $this->resource = DB::transaction(function () use ($request) { $model = static::fillWhenUpdate($request, $this->resource); $model->save(); - return $this; + return $model; }); return response()->json($this->jsonSerialize(), RestResponse::REST_RESPONSE_UPDATED_CODE); } /** - * @param RestifyRequest $request + * @param RestifyRequest $request * @return JsonResponse * @throws \Illuminate\Auth\Access\AuthorizationException */ @@ -153,7 +158,7 @@ public function destroy(RestifyRequest $request, $repositoryId) } /** - * @param RestifyRequest $request + * @param RestifyRequest $request * @return mixed * @throws \Illuminate\Auth\Access\AuthorizationException * @throws ValidationException @@ -168,7 +173,7 @@ public function allowToUpdate(RestifyRequest $request) } /** - * @param RestifyRequest $request + * @param RestifyRequest $request * @return mixed * @throws \Illuminate\Auth\Access\AuthorizationException * @throws ValidationException @@ -183,7 +188,7 @@ public function allowToStore(RestifyRequest $request) } /** - * @param RestifyRequest $request + * @param RestifyRequest $request * @throws \Illuminate\Auth\Access\AuthorizationException */ public function allowToDestroy(RestifyRequest $request) @@ -202,11 +207,111 @@ public function allowToShow($request) /** * @param $request - * @param Collection $items + * @param Collection $items * @throws \Illuminate\Auth\Access\AuthorizationException */ public function allowToViewAny($request, Collection $items) { $this->authorizeToShowAny($request); } + + /** + * @param array $payload + * @return mixed + * @throws AuthorizationException + * @throws ValidationException + */ + public static function storePlain(array $payload) + { + /** * @var RepositoryStoreRequest $request */ + $request = resolve(RepositoryStoreRequest::class); + $request->attributes->add($payload); + + + $repository = resolve(static::class); + + $repository->allowToStore($request); + + $repository->store($request); + + return $repository->resource; + } + + /** + * Update an entity with an array of payload + * + * @param array $payload + * @param $id + * @return mixed + * @throws AuthorizationException + * @throws UnauthorizedException + * @throws ValidationException + * @throws \Binaryk\LaravelRestify\Exceptions\Eloquent\EntityNotFoundException + */ + public static function updatePlain(array $payload, $id) + { + /** * @var RepositoryUpdateRequest $request */ + $request = resolve(RepositoryUpdateRequest::class); + $request->attributes->add($payload); + + $model = $request->findModelQuery($id, static::uriKey())->lockForUpdate()->firstOrFail(); + + /** + * @var Repository + */ + $repository = $request->newRepositoryWith($model, static::uriKey()); + + $repository->allowToUpdate($request); + + $repository->update($request, $id); + + return $repository->resource; + } + + /** + * Returns a plain model by key + * Used as: Book::showPlain(1) + * + * @param $key + * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator|\Illuminate\Database\Eloquent\Model + * @throws AuthorizationException + * @throws UnauthorizedException + * @throws \Binaryk\LaravelRestify\Exceptions\Eloquent\EntityNotFoundException + */ + public static function showPlain($key) + { + /** * @var RestifyRequest $request */ + $request = resolve(RestifyRequest::class); + + $repository = $request->newRepositoryWith($request->findModelQuery($key, static::uriKey())->firstOrFail(), static::uriKey()); + + $repository->allowToShow($request); + + $repository->show($request, $key); + + return $repository->resource; + } + + /** + * Validate deletion and delete entity + * + * @param $key + * @return mixed + * @throws AuthorizationException + * @throws UnauthorizedException + * @throws \Binaryk\LaravelRestify\Exceptions\Eloquent\EntityNotFoundException + */ + public static function destroyPlain($key) + { + /** * @var RepositoryDestroyRequest $request */ + $request = resolve(RepositoryDestroyRequest::class); + + $repository = $request->newRepositoryWith($request->findModelQuery($key, static::uriKey())->firstOrFail(), static::uriKey()); + + $repository->allowToDestroy($request); + + return DB::transaction(function () use ($repository) { + return $repository->resource->delete(); + }); + } } From 0f405dade9f53094abd28b0de28fdf239e32a6e0 Mon Sep 17 00:00:00 2001 From: Lupacescu Eduard Date: Tue, 21 Jan 2020 14:40:16 +0200 Subject: [PATCH 02/12] Apply fixes from StyleCI (#103) --- src/Repositories/Crudable.php | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/Repositories/Crudable.php b/src/Repositories/Crudable.php index 5deb023b9..e1cc05667 100644 --- a/src/Repositories/Crudable.php +++ b/src/Repositories/Crudable.php @@ -115,7 +115,7 @@ public function store(RestifyRequest $request) return $this->response('', RestResponse::REST_RESPONSE_CREATED_CODE) ->model($model) - ->header('Location', Restify::path() . '/' . static::uriKey() . '/' . $model->id); + ->header('Location', Restify::path().'/'.static::uriKey().'/'.$model->id); } /** @@ -227,7 +227,6 @@ public static function storePlain(array $payload) $request = resolve(RepositoryStoreRequest::class); $request->attributes->add($payload); - $repository = resolve(static::class); $repository->allowToStore($request); @@ -238,7 +237,7 @@ public static function storePlain(array $payload) } /** - * Update an entity with an array of payload + * Update an entity with an array of payload. * * @param array $payload * @param $id @@ -270,7 +269,7 @@ public static function updatePlain(array $payload, $id) /** * Returns a plain model by key - * Used as: Book::showPlain(1) + * Used as: Book::showPlain(1). * * @param $key * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator|\Illuminate\Database\Eloquent\Model @@ -293,7 +292,7 @@ public static function showPlain($key) } /** - * Validate deletion and delete entity + * Validate deletion and delete entity. * * @param $key * @return mixed From dc122d14524d63b88847fc6a34bea7746fa2165d Mon Sep 17 00:00:00 2001 From: Lupacescu Eduard Date: Tue, 21 Jan 2020 14:41:12 +0200 Subject: [PATCH 03/12] Docs --- src/Repositories/Crudable.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Repositories/Crudable.php b/src/Repositories/Crudable.php index 5deb023b9..f6c8d148e 100644 --- a/src/Repositories/Crudable.php +++ b/src/Repositories/Crudable.php @@ -216,6 +216,8 @@ public function allowToViewAny($request, Collection $items) } /** + * Validate input array and store a new entity + * * @param array $payload * @return mixed * @throws AuthorizationException From bb859d1086394adc321fc13a2d92d19d27b79a9b Mon Sep 17 00:00:00 2001 From: Lupacescu Eduard Date: Tue, 21 Jan 2020 14:41:43 +0200 Subject: [PATCH 04/12] Apply fixes from StyleCI (#105) --- src/Repositories/Crudable.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Repositories/Crudable.php b/src/Repositories/Crudable.php index 901dbb18b..0a73bc05f 100644 --- a/src/Repositories/Crudable.php +++ b/src/Repositories/Crudable.php @@ -216,7 +216,7 @@ public function allowToViewAny($request, Collection $items) } /** - * Validate input array and store a new entity + * Validate input array and store a new entity. * * @param array $payload * @return mixed From d933a4a4b0e4c6c005efe3a217000d32c051c283 Mon Sep 17 00:00:00 2001 From: Lupacescu Eduard Date: Tue, 21 Jan 2020 15:35:34 +0200 Subject: [PATCH 05/12] Call plain actions from controller actions in terms of avoiding too many calls in the stack --- src/Fields/Field.php | 4 +- src/Repositories/Crudable.php | 80 +++++++++++------------ src/Repositories/RepositoryFillFields.php | 4 +- src/Repositories/ValidatingTrait.php | 2 +- 4 files changed, 45 insertions(+), 45 deletions(-) diff --git a/src/Fields/Field.php b/src/Fields/Field.php index 1a43c325e..8bf3f2aae 100644 --- a/src/Fields/Field.php +++ b/src/Fields/Field.php @@ -118,8 +118,8 @@ public function fillAttribute(RestifyRequest $request, $model) */ protected function fillAttributeFromRequest(RestifyRequest $request, $model, $attribute) { - if ($request->exists($attribute)) { - $value = $request[$attribute]; + if ($request->exists($attribute) || $request->get($attribute)) { + $value = $request[$attribute] ?? $request->get($attribute); $model->{$attribute} = is_callable($this->storeCallback) ? call_user_func($this->storeCallback, $value, $request, $model) : $value; } diff --git a/src/Repositories/Crudable.php b/src/Repositories/Crudable.php index 901dbb18b..c60a8500a 100644 --- a/src/Repositories/Crudable.php +++ b/src/Repositories/Crudable.php @@ -66,17 +66,15 @@ public function index(RestifyRequest $request) /** * @param RestifyRequest $request + * @param $repositoryId * @return JsonResponse + * @throws AuthorizationException + * @throws UnauthorizedException + * @throws \Binaryk\LaravelRestify\Exceptions\Eloquent\EntityNotFoundException */ public function show(RestifyRequest $request, $repositoryId) { - /** - * Dive into the Search service to attach relations. - */ - $this->withResource(tap($this->resource, function ($query) use ($request) { - static::detailQuery($request, $query); - })->firstOrFail()); - + $this->resource = static::showPlain($repositoryId); try { $this->allowToShow($request); } catch (AuthorizationException $e) { @@ -89,6 +87,8 @@ public function show(RestifyRequest $request, $repositoryId) /** * @param RestifyRequest $request * @return JsonResponse + * @throws AuthorizationException + * @throws ValidationException */ public function store(RestifyRequest $request) { @@ -101,57 +101,44 @@ public function store(RestifyRequest $request) ->code(RestResponse::REST_RESPONSE_INVALID_CODE); } - $model = DB::transaction(function () use ($request) { - $model = self::fillWhenStore( - $request, self::newModel() - ); - - $model->save(); - - return $model; - }); - - $this->resource = $model; + $this->resource = static::storePlain($request->toArray()); return $this->response('', RestResponse::REST_RESPONSE_CREATED_CODE) - ->model($model) - ->header('Location', Restify::path().'/'.static::uriKey().'/'.$model->id); + ->model($this->resource) + ->header('Location', Restify::path() . '/' . static::uriKey() . '/' . $this->resource->id); } /** * @param RestifyRequest $request - * @param $model + * @param $repositoryId * @return JsonResponse - * @throws \Illuminate\Auth\Access\AuthorizationException + * @throws AuthorizationException + * @throws UnauthorizedException * @throws ValidationException + * @throws \Binaryk\LaravelRestify\Exceptions\Eloquent\EntityNotFoundException */ public function update(RestifyRequest $request, $repositoryId) { $this->allowToUpdate($request); - $this->resource = DB::transaction(function () use ($request) { - $model = static::fillWhenUpdate($request, $this->resource); - - $model->save(); - - return $model; - }); + $this->resource = static::updatePlain($request->all(), $repositoryId); return response()->json($this->jsonSerialize(), RestResponse::REST_RESPONSE_UPDATED_CODE); } /** * @param RestifyRequest $request + * @param $repositoryId * @return JsonResponse - * @throws \Illuminate\Auth\Access\AuthorizationException + * @throws AuthorizationException + * @throws UnauthorizedException + * @throws \Binaryk\LaravelRestify\Exceptions\Eloquent\EntityNotFoundException */ public function destroy(RestifyRequest $request, $repositoryId) { $this->allowToDestroy($request); - DB::transaction(function () use ($request) { - return $this->resource->delete(); - }); + static::destroyPlain($repositoryId); return $this->response() ->setStatusCode(RestResponse::REST_RESPONSE_DELETED_CODE); @@ -233,9 +220,15 @@ public static function storePlain(array $payload) $repository->allowToStore($request); - $repository->store($request); + return DB::transaction(function () use ($request) { + $model = self::fillWhenStore( + $request, self::newModel() + ); - return $repository->resource; + $model->save(); + + return $model; + }); } /** @@ -264,9 +257,13 @@ public static function updatePlain(array $payload, $id) $repository->allowToUpdate($request); - $repository->update($request, $id); + return DB::transaction(function () use ($request, $repository) { + $model = static::fillWhenUpdate($request, $repository->resource); - return $repository->resource; + $model->save(); + + return $model; + }); } /** @@ -284,12 +281,15 @@ public static function showPlain($key) /** * @var RestifyRequest $request */ $request = resolve(RestifyRequest::class); - $repository = $request->newRepositoryWith($request->findModelQuery($key, static::uriKey())->firstOrFail(), static::uriKey()); + /** + * Dive into the Search service to attach relations. + */ + $repository = $request->newRepositoryWith(tap($request->findModelQuery($key, static::uriKey())->firstOrFail(), function ($query) use ($request) { + static::detailQuery($request, $query); + })); $repository->allowToShow($request); - $repository->show($request, $key); - return $repository->resource; } diff --git a/src/Repositories/RepositoryFillFields.php b/src/Repositories/RepositoryFillFields.php index 26e793f9b..6873cbda2 100644 --- a/src/Repositories/RepositoryFillFields.php +++ b/src/Repositories/RepositoryFillFields.php @@ -23,11 +23,11 @@ public static function fillWhenStore(RestifyRequest $request, $model) { static::fillFields( $request, $model, - (new static($model))->collectFields($request) + static::resolveWith($model)->collectFields($request) ); static::fillExtra($request, $model, - (new static($model))->collectFields($request) + static::resolveWith($model)->collectFields($request) ); return $model; diff --git a/src/Repositories/ValidatingTrait.php b/src/Repositories/ValidatingTrait.php index 3ffecd0a3..d5ce515cc 100644 --- a/src/Repositories/ValidatingTrait.php +++ b/src/Repositories/ValidatingTrait.php @@ -29,7 +29,7 @@ abstract public static function newModel(); */ public static function validatorForStoring(RestifyRequest $request) { - $on = (new static(static::newModel())); + $on = static::resolveWith(static::newModel()); $messages = $on->collectFields($request)->flatMap(function ($k) { $messages = []; From f90d397555f536cd186565fe9217082b4ecb38cc Mon Sep 17 00:00:00 2001 From: Lupacescu Eduard Date: Tue, 21 Jan 2020 15:35:58 +0200 Subject: [PATCH 06/12] Apply fixes from StyleCI (#106) --- src/Repositories/Crudable.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Repositories/Crudable.php b/src/Repositories/Crudable.php index 53aaa91b0..640e6bae2 100644 --- a/src/Repositories/Crudable.php +++ b/src/Repositories/Crudable.php @@ -105,7 +105,7 @@ public function store(RestifyRequest $request) return $this->response('', RestResponse::REST_RESPONSE_CREATED_CODE) ->model($this->resource) - ->header('Location', Restify::path() . '/' . static::uriKey() . '/' . $this->resource->id); + ->header('Location', Restify::path().'/'.static::uriKey().'/'.$this->resource->id); } /** From 348b37e6be2e08fabf4397bf80903621b51cf651 Mon Sep 17 00:00:00 2001 From: Lupacescu Eduard Date: Tue, 21 Jan 2020 19:18:46 +0200 Subject: [PATCH 07/12] Rename debug into dump --- src/Controllers/RestResponse.php | 14 +++++++++++++- src/Exceptions/RestifyHandler.php | 2 +- src/Http/Controllers/RepositoryIndexController.php | 6 +++--- 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/src/Controllers/RestResponse.php b/src/Controllers/RestResponse.php index be073c2e5..e3cf59f77 100644 --- a/src/Controllers/RestResponse.php +++ b/src/Controllers/RestResponse.php @@ -10,6 +10,7 @@ use Illuminate\Database\Eloquent\Model; use Illuminate\Http\JsonResponse; use Illuminate\Support\Arr; +use Illuminate\Support\Facades\App; /** * Class RestResponse. @@ -580,7 +581,7 @@ public function getErrors() return $this->errors instanceof Arrayable ? $this->errors->toArray() : $this->errors; } - public function debug(\Exception $exception, $condition) + public function dump(\Exception $exception, $condition) { if ($condition) { $this->debug = true; @@ -595,6 +596,17 @@ public function debug(\Exception $exception, $condition) return $this; } + /** + * Debug the log if the environment is local + * + * @param \Exception $exception + * @return $this + */ + public function dumpLocal(\Exception $exception) + { + return $this->dump($exception, App::environment('production') === false); + } + /** * Set the JSON:API format for a single resource. * diff --git a/src/Exceptions/RestifyHandler.php b/src/Exceptions/RestifyHandler.php index 98d759073..4e5351121 100644 --- a/src/Exceptions/RestifyHandler.php +++ b/src/Exceptions/RestifyHandler.php @@ -109,7 +109,7 @@ public function render($request, Exception $exception) if (App::environment('production') === true) { $response->addError(__('messages.something_went_wrong')); } else { - $response->debug($exception, true); + $response->dump($exception, true); } $response->error(); } diff --git a/src/Http/Controllers/RepositoryIndexController.php b/src/Http/Controllers/RepositoryIndexController.php index 221fb2ac8..c186a3a98 100644 --- a/src/Http/Controllers/RepositoryIndexController.php +++ b/src/Http/Controllers/RepositoryIndexController.php @@ -28,11 +28,11 @@ public function handle(RestifyRequest $request) } catch (EntityNotFoundException $e) { return $this->response()->notFound() ->addError($e->getMessage()) - ->debug($e, $request->isDev()); + ->dump($e, $request->isDev()); } catch (UnauthorizedException $e) { - return $this->response()->forbidden()->addError($e->getMessage())->debug($e, $request->isDev()); + return $this->response()->forbidden()->addError($e->getMessage())->dump($e, $request->isDev()); } catch (InstanceOfException | \Throwable $e) { - return $this->response()->error()->debug($e, $request->isDev()); + return $this->response()->error()->dump($e, $request->isDev()); } } } From 4589449b348bf12154b6cbc1e946cf8a65531a37 Mon Sep 17 00:00:00 2001 From: Lupacescu Eduard Date: Tue, 21 Jan 2020 19:19:15 +0200 Subject: [PATCH 08/12] Apply fixes from StyleCI (#107) --- src/Controllers/RestResponse.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Controllers/RestResponse.php b/src/Controllers/RestResponse.php index e3cf59f77..4cc9aa3ea 100644 --- a/src/Controllers/RestResponse.php +++ b/src/Controllers/RestResponse.php @@ -597,7 +597,7 @@ public function dump(\Exception $exception, $condition) } /** - * Debug the log if the environment is local + * Debug the log if the environment is local. * * @param \Exception $exception * @return $this From dbb95ebb46eb79207049b5b78b75aa4751969bdf Mon Sep 17 00:00:00 2001 From: Lupacescu Eduard Date: Tue, 21 Jan 2020 20:00:09 +0200 Subject: [PATCH 09/12] Actions hooks --- src/Repositories/Crudable.php | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/src/Repositories/Crudable.php b/src/Repositories/Crudable.php index 640e6bae2..c96402dd3 100644 --- a/src/Repositories/Crudable.php +++ b/src/Repositories/Crudable.php @@ -103,6 +103,8 @@ public function store(RestifyRequest $request) $this->resource = static::storePlain($request->toArray()); + static::stored($this->resource); + return $this->response('', RestResponse::REST_RESPONSE_CREATED_CODE) ->model($this->resource) ->header('Location', Restify::path().'/'.static::uriKey().'/'.$this->resource->id); @@ -123,6 +125,8 @@ public function update(RestifyRequest $request, $repositoryId) $this->resource = static::updatePlain($request->all(), $repositoryId); + static::updated($this->resource); + return response()->json($this->jsonSerialize(), RestResponse::REST_RESPONSE_UPDATED_CODE); } @@ -138,7 +142,9 @@ public function destroy(RestifyRequest $request, $repositoryId) { $this->allowToDestroy($request); - static::destroyPlain($repositoryId); + $status = static::destroyPlain($repositoryId); + + static::deleted($status); return $this->response() ->setStatusCode(RestResponse::REST_RESPONSE_DELETED_CODE); @@ -315,4 +321,16 @@ public static function destroyPlain($key) return $repository->resource->delete(); }); } + + public static function stored($model) { + // + } + + public static function updated($model) { + // + } + + public static function deleted($status) { + // + } } From c2d40f6e4b301f718944e04af4017cfe61c40414 Mon Sep 17 00:00:00 2001 From: Lupacescu Eduard Date: Tue, 21 Jan 2020 20:00:33 +0200 Subject: [PATCH 10/12] Apply fixes from StyleCI (#108) --- src/Repositories/Crudable.php | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/Repositories/Crudable.php b/src/Repositories/Crudable.php index c96402dd3..dcca6116b 100644 --- a/src/Repositories/Crudable.php +++ b/src/Repositories/Crudable.php @@ -322,15 +322,18 @@ public static function destroyPlain($key) }); } - public static function stored($model) { + public static function stored($model) + { // } - public static function updated($model) { + public static function updated($model) + { // } - public static function deleted($status) { + public static function deleted($status) + { // } } From 96eeeed77f5e871744eac426f54b726f61716b69 Mon Sep 17 00:00:00 2001 From: Lupacescu Eduard Date: Tue, 21 Jan 2020 20:00:45 +0200 Subject: [PATCH 11/12] Docs --- src/Repositories/Crudable.php | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/Repositories/Crudable.php b/src/Repositories/Crudable.php index c96402dd3..8fd81c094 100644 --- a/src/Repositories/Crudable.php +++ b/src/Repositories/Crudable.php @@ -322,14 +322,23 @@ public static function destroyPlain($key) }); } + /** + * @param $model + */ public static function stored($model) { // } + /** + * @param $model + */ public static function updated($model) { // } + /** + * @param int $status + */ public static function deleted($status) { // } From bc3f5d64dc4c17eb8886754c1ceb80a5c2faab66 Mon Sep 17 00:00:00 2001 From: Lupacescu Eduard Date: Tue, 21 Jan 2020 20:01:44 +0200 Subject: [PATCH 12/12] Apply fixes from StyleCI (#109) --- src/Repositories/Crudable.php | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/Repositories/Crudable.php b/src/Repositories/Crudable.php index 8fd81c094..8b555a852 100644 --- a/src/Repositories/Crudable.php +++ b/src/Repositories/Crudable.php @@ -325,21 +325,24 @@ public static function destroyPlain($key) /** * @param $model */ - public static function stored($model) { + public static function stored($model) + { // } /** * @param $model */ - public static function updated($model) { + public static function updated($model) + { // } /** * @param int $status */ - public static function deleted($status) { + public static function deleted($status) + { // } }