diff --git a/app/Http/Controllers/Api/GroupController.php b/app/Http/Controllers/Api/GroupController.php new file mode 100644 index 0000000..9a76d13 --- /dev/null +++ b/app/Http/Controllers/Api/GroupController.php @@ -0,0 +1,31 @@ +get()); + } + + + /** + * Display the specified resource. + */ + public function show(Group $group) + { + // + return GroupCollection::make($group); + } +} diff --git a/app/Http/Controllers/Api/PostController.php b/app/Http/Controllers/Api/PostController.php new file mode 100644 index 0000000..09ccb06 --- /dev/null +++ b/app/Http/Controllers/Api/PostController.php @@ -0,0 +1,66 @@ + $this->bgOriginalUrl(), 'svg' => $this->svgUrl(), 'icons' => $this->icon, - 'products' => $this->when($request->input('loadProduct', true), ProductResource::collection($this->products()->paginate($request->input('per_page', 20)))->additional(['request' => $request['loadCategory']])), + 'products' => $this->when($request->input('loadProduct', true), + ProductResource::collection($this->products()->paginate($request->input('per_page', 20))) + ->additional(['request' => $request['loadCategory']])), 'products_pages_count' => ceil($this->products()->count() / $request->input('per_page', 20) ), ]; } diff --git a/app/Http/Resources/GroupCollection.php b/app/Http/Resources/GroupCollection.php new file mode 100644 index 0000000..eaa2a7e --- /dev/null +++ b/app/Http/Resources/GroupCollection.php @@ -0,0 +1,44 @@ + + */ + public function toArray(Request $request): array + { + /** + * @var $this Group + */ + + if (!$request['loadGroup']) + $request->merge([ + 'loadGroup' => false + ]); + return [ + 'id' => $this->id, + 'name' => $this->name, + 'slug' => $this->slug, + 'subtitle' => $this->subtitle, + 'description' => $this->description, + 'sort' => $this->sort, + 'image' => $this->imgUrl(), + 'image_original' => $this->imgOriginalUrl(), + 'bg' => $this->bgUrl(), + 'bg_original' => $this->bgOriginalUrl(), + 'posts' => $this->when($request->input('loadPost', true), + PostResource::collection($this->posts()->paginate($request->input('per_page', 20))) + ->additional(['request' => $request['loadGroup']])), + 'posts_pages_count' => ceil($this->posts()->count() / $request->input('per_page', 20) ), + ]; + } +} diff --git a/app/Http/Resources/GroupsCollection.php b/app/Http/Resources/GroupsCollection.php new file mode 100644 index 0000000..39a73d6 --- /dev/null +++ b/app/Http/Resources/GroupsCollection.php @@ -0,0 +1,35 @@ + + */ + public function toArray(Request $request): array + { + /** + * @var $this Group + */ + return[ + 'id' => $this->id, + 'name' => $this->name, + 'slug' => $this->slug, + 'subtitle' => $this->subtitle, + 'description' => $this->description, + 'sort' => $this->sort, + 'image' => $this->imgUrl(), + 'image_original' => $this->imgOriginalUrl(), + 'bg' => $this->bgUrl(), + 'bg_original' => $this->bgOriginalUrl(), + ]; + } +} diff --git a/app/Http/Resources/PostResource.php b/app/Http/Resources/PostResource.php index ad8f8e0..4bdf9d1 100644 --- a/app/Http/Resources/PostResource.php +++ b/app/Http/Resources/PostResource.php @@ -23,7 +23,9 @@ class PostResource extends JsonResource 'slug' => $this->slug, 'subtitle' => $this->subtitle, 'body' => $this->body, - 'group' => $this->load('groups'), + 'group' => GroupsCollection::make($this->mainGroup), + 'groups'=> GroupsCollection::collection($this->groups), +// 'group' => $this->load('groups'), 'author' => $this->load('author'), 'view' => $this->view, 'is_pinned' => $this->is_pinned, @@ -31,8 +33,9 @@ class PostResource extends JsonResource 'like' => $this->like, 'dislike' => $this->dislike, 'icon' => $this->icon, - 'created_at' => $this->created_at, 'image' => $this->imgUrl(), + 'created_at' => $this->created_at, + 'updated_at' => $this->updated_at, ]; } diff --git a/routes/api.php b/routes/api.php index 1f5e9f2..04b817f 100644 --- a/routes/api.php +++ b/routes/api.php @@ -36,7 +36,9 @@ Route::prefix('v1')->name('v1.')->group( Route::get('states', [\App\Http\Controllers\Api\StateController::class, 'index'])->name('state.index'); Route::get('state/{state}', [\App\Http\Controllers\Api\StateController::class, 'show'])->name('state.show'); Route::get('categories', [\App\Http\Controllers\Api\CategoryController::class, 'index'])->name('category.index'); + Route::get('groups', [\App\Http\Controllers\Api\GroupController::class, 'index'])->name('group.index'); Route::get('category/{category}', [\App\Http\Controllers\Api\CategoryController::class, 'show'])->name('category.show'); + Route::get('group/{group}', [\App\Http\Controllers\Api\GroupController::class, 'show'])->name('group.show'); Route::get('products', [\App\Http\Controllers\Api\ProductController::class, 'index'])->name('product.index'); Route::get('category/props/{category}', [\App\Http\Controllers\Api\CategoryController::class, 'props'])->name('category.prop'); Route::post('morph/search', [\App\Http\Controllers\Api\MorphController::class, 'search'])->name('morph.search');