From bed081881fc6c7068c31b0411178c6d72b9cd66e Mon Sep 17 00:00:00 2001 From: panabonic Date: Sun, 19 Nov 2023 14:38:18 +0300 Subject: [PATCH] include trashed pages functionality added --- app/Http/Controllers/Api/Pages/PagesController.php | 7 +++++-- app/Transformers/Pages/PageTransformer.php | 12 +++++++----- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/app/Http/Controllers/Api/Pages/PagesController.php b/app/Http/Controllers/Api/Pages/PagesController.php index 497bf70..549b610 100644 --- a/app/Http/Controllers/Api/Pages/PagesController.php +++ b/app/Http/Controllers/Api/Pages/PagesController.php @@ -34,6 +34,7 @@ class PagesController extends Controller { public function index(Request $request): JsonResponse { $filters = collect($request->has('filters') ? json_decode($request->get('filters'), true) : [])->filter(function($val) {return $val;}); $query = $this->model->query(); + if ($request->get('with_trashed')) $query->withTrashed(); $service = FiltersService::getService('pages'); $service->applyFilters($query, $filters); $paginator = $query->paginate(config('app.pagination_limit')); @@ -41,8 +42,10 @@ class PagesController extends Controller { } public function show(Request $request, $id): JsonResponse { - $model = $this->model->byUuid($id)->firstOrFail(); - return fractal($model, new PageTransformer())->respond(); + $query = $this->model->byUuid($id); + if ($request->get('with_trashed')) $query->withTrashed(); + $model = $query->firstOrFail(); + return fractal($model, new PageTransformer($request->get('with_trashed')))->respond(); } public function move(Request $request, $id): JsonResponse { diff --git a/app/Transformers/Pages/PageTransformer.php b/app/Transformers/Pages/PageTransformer.php index d45b494..25d66e1 100644 --- a/app/Transformers/Pages/PageTransformer.php +++ b/app/Transformers/Pages/PageTransformer.php @@ -14,6 +14,12 @@ use League\Fractal\Resource\Primitive; use League\Fractal\TransformerAbstract; class PageTransformer extends TransformerAbstract { + public bool $withTrashed = false; + + public function __construct(bool $withTrashed = false) { + $this->withTrashed = $withTrashed; + } + protected array $defaultIncludes = [ ]; @@ -43,11 +49,7 @@ class PageTransformer extends TransformerAbstract { } public function includeChildren(Page $model): Collection { - return $this->collection($model->children, new PageTransformer()); - } - - public function includeChildrenWithTrashed(Page $model): Collection { - return $this->collection($model->children()->withTrashed()->get, new PageTransformer()); + return $this->collection($this->withTrashed ? $model->children()->withTrashed()->get() : $model->children, new PageTransformer($this->withTrashed)); } public function includeParent(Page $model): ?Item {