wip
This commit is contained in:
73
app/Http/Controllers/Api/V1/Category/CategoryController.php
Normal file
73
app/Http/Controllers/Api/V1/Category/CategoryController.php
Normal file
@@ -0,0 +1,73 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api\V1\Category;
|
||||
|
||||
use App\Http\Controllers\Api\V1\Category\Resources\CategoryResource;
|
||||
use App\Http\Controllers\Api\V1\Product\Resources\ProductResource;
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Requests\Api\V1\Category\CategoryIndexRequest;
|
||||
use App\Http\Requests\Api\V1\Product\BasicProductIndexRequest;
|
||||
use App\Models\Ecommerce\Product\Category\Category;
|
||||
use App\Repositories\Ecommerce\Product\Category\CategoryRepository;
|
||||
use App\Repositories\Ecommerce\Product\ProductRepository;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
|
||||
class CategoryController extends Controller
|
||||
{
|
||||
/**
|
||||
* Categories (index)
|
||||
*/
|
||||
public function index(CategoryIndexRequest $request): JsonResponse
|
||||
{
|
||||
return response()->rest(
|
||||
CategoryResource::collection(
|
||||
CategoryRepository::make($request)
|
||||
->applyBasicQueries()
|
||||
->applyFilters()
|
||||
->get()
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Categories (show)
|
||||
*/
|
||||
public function show(Category $category): JsonResponse
|
||||
{
|
||||
$category->load(['media', 'children']);
|
||||
|
||||
return response()->rest(new CategoryResource($category));
|
||||
}
|
||||
|
||||
/**
|
||||
* Categories (children)
|
||||
*/
|
||||
public function children(Category $category): JsonResponse
|
||||
{
|
||||
return response()->rest(
|
||||
CategoryResource::collection(
|
||||
$category->children()
|
||||
->enabled()
|
||||
->ordered()
|
||||
->get()
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Brands (products)
|
||||
*/
|
||||
public function products(BasicProductIndexRequest $request, Category $category): JsonResponse
|
||||
{
|
||||
return response()->rest_paginate(
|
||||
ProductResource::collection(
|
||||
ProductRepository::make($request)
|
||||
->queryAsFromResource($category)
|
||||
->applyBasicQueries()
|
||||
->applyFilters()
|
||||
->applySorting()
|
||||
->simplePaginate()
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api\V1\Category\Resources;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
|
||||
class CategoryMediaResource extends JsonResource
|
||||
{
|
||||
/**
|
||||
* Transform the resource into an array.
|
||||
*
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function toArray(Request $request): array
|
||||
{
|
||||
return [
|
||||
'thumbnail' => $this->getUrl(),
|
||||
'images_400x400' => $this->getUrl('thumb400x400'),
|
||||
'images_720x720' => $this->getUrl('thumb720x720'),
|
||||
'images_800x800' => $this->getUrl('thumb800x800'),
|
||||
'images_1200x1200' => $this->getUrl('thumb1200x1200'),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api\V1\Category\Resources;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
|
||||
class CategoryResource extends JsonResource
|
||||
{
|
||||
/**
|
||||
* Transform the resource into an array.
|
||||
*
|
||||
* @return array|\Illuminate\Contracts\Support\Arrayable|\JsonSerializable
|
||||
*/
|
||||
public function toArray(Request $request): array
|
||||
{
|
||||
return [
|
||||
'id' => $this->id,
|
||||
'parent_id' => $this->parent_id,
|
||||
'name' => $this->name,
|
||||
'slug' => $this->slug,
|
||||
'group' => $this->type,
|
||||
'description' => $this->description,
|
||||
'color' => $this->color,
|
||||
'media' => CategoryMediaResource::collection($this->whenLoaded('media')),
|
||||
'children' => static::collection($this->whenLoaded('children')),
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user