wip
This commit is contained in:
61
app/Http/Controllers/Api/V1/Brand/BrandController.php
Normal file
61
app/Http/Controllers/Api/V1/Brand/BrandController.php
Normal file
@@ -0,0 +1,61 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api\V1\Brand;
|
||||
|
||||
use App\Http\Controllers\Api\V1\Brand\Resources\BrandResource;
|
||||
use App\Http\Controllers\Api\V1\Product\Resources\ProductResource;
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Requests\Api\V1\Brand\BrandIndexRequest;
|
||||
use App\Http\Requests\Api\V1\Brand\BrandProductsRequest;
|
||||
use App\Models\Ecommerce\Product\Brand\Brand;
|
||||
use App\Repositories\Ecommerce\Product\ProductRepository;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
|
||||
class BrandController extends Controller
|
||||
{
|
||||
/**
|
||||
* Brands (index)
|
||||
*/
|
||||
public function index(BrandIndexRequest $request): JsonResponse
|
||||
{
|
||||
return response()->rest(
|
||||
BrandResource::collection(
|
||||
Brand::query()
|
||||
->with('media')
|
||||
->enabled()
|
||||
->ordered()
|
||||
->when($request->input('type'), fn ($query, $type) => $query->where('type', $type))
|
||||
->get()
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Brands (show)
|
||||
*
|
||||
* @param App\Models\Ecommerce\Product\Brand\Brand $brand
|
||||
*/
|
||||
public function show(Brand $brand): JsonResponse
|
||||
{
|
||||
$brand->load('media');
|
||||
|
||||
return response()->rest(new BrandResource($brand));
|
||||
}
|
||||
|
||||
/**
|
||||
* Brands (products)
|
||||
*/
|
||||
public function products(BrandProductsRequest $request, Brand $brand): JsonResponse
|
||||
{
|
||||
return response()->rest_paginate(
|
||||
ProductResource::collection(
|
||||
ProductRepository::make($request)
|
||||
->queryAsFromResource($brand)
|
||||
->applyBasicQueries()
|
||||
->applyFilters()
|
||||
->applySorting()
|
||||
->simplePaginate()
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api\V1\Brand\Resources;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
|
||||
class BrandMediaResource extends JsonResource
|
||||
{
|
||||
/**
|
||||
* Transform the resource into an array.
|
||||
*
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function toArray(Request $request): array
|
||||
{
|
||||
return [
|
||||
'thumbnail' => $this->getUrl('thumb200x200'),
|
||||
'images_400x400' => $this->getUrl('thumb400x400'),
|
||||
'images_720x720' => $this->getUrl('thumb720x720'),
|
||||
'images_800x800' => $this->getUrl('thumb800x800'),
|
||||
'images_1200x1200' => $this->getUrl('thumb1200x1200'),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api\V1\Brand\Resources;
|
||||
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
|
||||
class BrandResource extends JsonResource
|
||||
{
|
||||
/**
|
||||
* Transform the resource into an array.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @return array|\Illuminate\Contracts\Support\Arrayable|\JsonSerializable
|
||||
*/
|
||||
public function toArray($request): array
|
||||
{
|
||||
return [
|
||||
'id' => $this->id,
|
||||
'name' => $this->name,
|
||||
'slug' => $this->slug,
|
||||
'type' => $this->type,
|
||||
'description' => $this->description,
|
||||
'media' => BrandMediaResource::collection($this->whenLoaded('media')),
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user