wip
This commit is contained in:
61
app/Http/Controllers/Api/V1/Channel/ChannelController.php
Normal file
61
app/Http/Controllers/Api/V1/Channel/ChannelController.php
Normal file
@@ -0,0 +1,61 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api\V1\Channel;
|
||||
|
||||
use App\Http\Controllers\Api\V1\Channel\Requests\ChannelIndexRequest;
|
||||
use App\Http\Controllers\Api\V1\Product\Resources\ProductResource;
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Resources\Api\V1\Channel\ChannelResource;
|
||||
use App\Models\Ecommerce\Channel\Channel;
|
||||
use App\Repositories\Ecommerce\Product\ProductRepository;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class ChannelController extends Controller
|
||||
{
|
||||
/**
|
||||
* Channels (index)
|
||||
*/
|
||||
public function index(ChannelIndexRequest $request): JsonResponse
|
||||
{
|
||||
return response()->rest_paginate(
|
||||
ChannelResource::collection(
|
||||
Channel::query()
|
||||
->with('media')
|
||||
->where('is_visible', true)
|
||||
->ordered()
|
||||
->simplePaginate(
|
||||
perPage: $request->perPage,
|
||||
columns: $request->fields,
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Channels (show)
|
||||
*/
|
||||
public function show(Channel $channel): JsonResponse
|
||||
{
|
||||
$channel->load('media');
|
||||
|
||||
return response()->rest(new ChannelResource($channel));
|
||||
}
|
||||
|
||||
/**
|
||||
* Channels (products)
|
||||
*/
|
||||
public function products(Request $request, Channel $channel): JsonResponse
|
||||
{
|
||||
return response()->rest_paginate(
|
||||
ProductResource::collection(
|
||||
ProductRepository::make($request)
|
||||
->queryAsFromResource($channel)
|
||||
->applyBasicQueries()
|
||||
->applyFilters()
|
||||
->applySorting()
|
||||
->simplePaginate()
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api\V1\Channel\Requests;
|
||||
|
||||
use App\Models\Ecommerce\Channel\Channel;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
|
||||
class ChannelIndexRequest extends FormRequest
|
||||
{
|
||||
/**
|
||||
* Get the validation rules that apply to the request.
|
||||
*
|
||||
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array|string>
|
||||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'perPage' => ['nullable', 'integer'],
|
||||
'fields' => ['nullable', 'string'],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle a passed validation attempt.
|
||||
*/
|
||||
protected function passedValidation(): void
|
||||
{
|
||||
$fields = [];
|
||||
if ($this->fields) {
|
||||
$sanitezedFields = validateCommaSeperated($this->fields, Channel::class);
|
||||
|
||||
$fields['fields'] = ! empty($sanitezedFields) ? $sanitezedFields : ['*'];
|
||||
} else {
|
||||
$fields['fields'] = ['*'];
|
||||
}
|
||||
|
||||
$this->merge([
|
||||
'perPage' => $this->perPage ?: 6,
|
||||
...$fields,
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user