feat(forms): implement MerchandiseFormController, MerchandiseFormService, and MerchandiseFormResource for managing merchandise options
This commit is contained in:
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domains\Forms\Controllers\AdminApp;
|
||||
|
||||
use App\Domains\Forms\Resources\MerchandiseFormResource;
|
||||
use App\Domains\Forms\Services\MerchandiseFormService;
|
||||
use App\Http\Controllers\Controller;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class MerchandiseFormController extends Controller
|
||||
{
|
||||
public function __construct(protected MerchandiseFormService $merchandiseFormService) {}
|
||||
|
||||
public function __invoke(Request $request): MerchandiseFormResource
|
||||
{
|
||||
return MerchandiseFormResource::make(
|
||||
$this->merchandiseFormService->get(
|
||||
$request->user('sanctum')->tenant()->firstOrFail()
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
32
app/Domains/Forms/Resources/MerchandiseFormResource.php
Normal file
32
app/Domains/Forms/Resources/MerchandiseFormResource.php
Normal file
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domains\Forms\Resources;
|
||||
|
||||
use App\Domains\Catalog\Models\AttributeOption;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
use Illuminate\Support\Collection;
|
||||
|
||||
class MerchandiseFormResource extends JsonResource
|
||||
{
|
||||
/** @return array<string, mixed> */
|
||||
public function toArray(Request $request): array
|
||||
{
|
||||
return [
|
||||
'colors' => $this->options($this->resource['colors']),
|
||||
'sizes' => $this->options($this->resource['sizes']),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Collection<int, AttributeOption> $options
|
||||
* @return Collection<int, array{value: string, label: string}>
|
||||
*/
|
||||
private function options(Collection $options): Collection
|
||||
{
|
||||
return $options->map(fn (AttributeOption $option): array => [
|
||||
'value' => $option->value,
|
||||
'label' => $option->label,
|
||||
])->values();
|
||||
}
|
||||
}
|
||||
32
app/Domains/Forms/Services/MerchandiseFormService.php
Normal file
32
app/Domains/Forms/Services/MerchandiseFormService.php
Normal file
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domains\Forms\Services;
|
||||
|
||||
use App\Domains\Catalog\Models\Attribute;
|
||||
use App\Domains\Catalog\Models\AttributeOption;
|
||||
use App\Domains\Tenant\Models\Tenant;
|
||||
use Illuminate\Database\Eloquent\Collection;
|
||||
|
||||
class MerchandiseFormService
|
||||
{
|
||||
/**
|
||||
* @return array{
|
||||
* colors: Collection<int, AttributeOption>,
|
||||
* sizes: Collection<int, AttributeOption>
|
||||
* }
|
||||
*/
|
||||
public function get(Tenant $tenant): array
|
||||
{
|
||||
$attributes = Attribute::query()
|
||||
->where('tenant_codigo', $tenant->codigo)
|
||||
->whereIn('codigo', ['color', 'talle'])
|
||||
->with('options')
|
||||
->get()
|
||||
->keyBy('codigo');
|
||||
|
||||
return [
|
||||
'colors' => $attributes->get('color')?->options ?? new Collection,
|
||||
'sizes' => $attributes->get('talle')?->options ?? new Collection,
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -19,6 +19,7 @@ Bajo `/v1/adminapp/forms`, con `auth:sanctum` y `adminapp.tenant`:
|
||||
- `GET /event`.
|
||||
- `GET /sale`.
|
||||
- `GET /staff`.
|
||||
- `GET /fiesta-futbol-infantil/merchandise`: opciones de color y talle del tenant para merchandising.
|
||||
|
||||
## Dependencias
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
<?php
|
||||
|
||||
use App\Domains\Forms\Controllers\AdminApp\EventFormController;
|
||||
use App\Domains\Forms\Controllers\AdminApp\MerchandiseFormController;
|
||||
use App\Domains\Forms\Controllers\AdminApp\SaleFormController;
|
||||
use App\Domains\Forms\Controllers\AdminApp\StaffFormController;
|
||||
use Illuminate\Support\Facades\Route;
|
||||
@@ -11,4 +12,8 @@ Route::prefix('v1/adminapp/forms')
|
||||
Route::get('event', EventFormController::class);
|
||||
Route::get('sale', SaleFormController::class);
|
||||
Route::get('staff', StaffFormController::class);
|
||||
Route::get(
|
||||
'fiesta-futbol-infantil/merchandise',
|
||||
MerchandiseFormController::class
|
||||
);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user