feat(food): implement FoodFormController and FoodFormService; add FoodFormResource for managing food options and routes

This commit is contained in:
2026-08-10 10:05:35 -03:00
parent ca605127d0
commit 1cd60f7021
9 changed files with 202 additions and 5 deletions

View File

@@ -12,6 +12,14 @@ class AccommodationResource extends JsonResource
/** @return array<string, mixed> */
public function toArray(Request $request): array
{
if ($this->resource === null) {
return [
'id' => null,
'name' => 'Alojamiento',
'variants' => [],
];
}
$typeAttribute = $this->itemAttributes
->first(fn ($itemAttribute) => $itemAttribute->attribute?->codigo === 'tipo_alojamiento');
$options = $typeAttribute?->attribute?->options?->keyBy('value') ?? collect();

View File

@@ -12,6 +12,14 @@ class FoodResource extends JsonResource
/** @return array<string, mixed> */
public function toArray(Request $request): array
{
if ($this->resource === null) {
return [
'id' => null,
'name' => 'Comida',
'variants' => [],
];
}
return [
'id' => $this->id,
'name' => $this->nombre,

View File

@@ -20,7 +20,7 @@ class AccommodationService
{
private const ATTRIBUTE_CODE = 'tipo_alojamiento';
public function current(Tenant $tenant): CatalogItem
public function current(Tenant $tenant): ?CatalogItem
{
return CatalogItem::query()
->where('tenant_code', $tenant->codigo)
@@ -31,7 +31,7 @@ class AccommodationService
'variants.inventory',
'variants.definitions',
])
->firstOrFail();
->first();
}
/**

View File

@@ -20,7 +20,7 @@ class FoodService
{
private const ATTRIBUTE_CODES = ['event_date', 'horario', 'servicio'];
public function current(Tenant $tenant): CatalogItem
public function current(Tenant $tenant): ?CatalogItem
{
return CatalogItem::query()
->where('tenant_code', $tenant->codigo)
@@ -32,7 +32,7 @@ class FoodService
'variants.eventDates',
'variants.definitions.itemAttribute.attribute',
])
->firstOrFail();
->first();
}
/**
@@ -282,7 +282,7 @@ class FoodService
/** @param Collection<string, ItemAttribute> $itemAttributes */
private function syncDefinitions(Variant $variant, Collection $itemAttributes, array $data): void
{
$definitionAttributes = $itemAttributes->only(['horario', 'servicio']);
$definitionAttributes = $itemAttributes->toBase()->only(['horario', 'servicio']);
$variant->definitions()->whereIn('item_attribute_id', $definitionAttributes->pluck('id'))->delete();
$variant->definitions()->createMany([
[

View File

@@ -0,0 +1,22 @@
<?php
namespace App\Domains\Forms\Controllers\AdminApp;
use App\Domains\Forms\Resources\FoodFormResource;
use App\Domains\Forms\Services\FoodFormService;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class FoodFormController extends Controller
{
public function __construct(protected FoodFormService $foodFormService) {}
public function __invoke(Request $request): FoodFormResource
{
return FoodFormResource::make(
$this->foodFormService->get(
$request->user('sanctum')->tenant()->firstOrFail()
)
);
}
}

View File

@@ -0,0 +1,39 @@
<?php
namespace App\Domains\Forms\Resources;
use App\Domains\Catalog\Models\AttributeOption;
use App\Domains\Event\Models\EventDate;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Support\Collection;
class FoodFormResource extends JsonResource
{
/** @return array<string, mixed> */
public function toArray(Request $request): array
{
return [
'event_dates' => $this->resource['event_dates']->map(
fn (EventDate $eventDate): array => [
'id' => $eventDate->id,
'date' => $eventDate->date->format('Y-m-d'),
]
)->values(),
'schedules' => $this->options($this->resource['schedules']),
'services' => $this->options($this->resource['services']),
];
}
/**
* @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();
}
}

View File

@@ -0,0 +1,35 @@
<?php
namespace App\Domains\Forms\Services;
use App\Domains\Catalog\Models\Attribute;
use App\Domains\Catalog\Models\AttributeOption;
use App\Domains\Event\Models\EventDate;
use App\Domains\Tenant\Models\Tenant;
use Illuminate\Database\Eloquent\Collection;
class FoodFormService
{
/**
* @return array{
* event_dates: Collection<int, EventDate>,
* schedules: Collection<int, AttributeOption>,
* services: Collection<int, AttributeOption>
* }
*/
public function get(Tenant $tenant): array
{
$attributes = Attribute::query()
->where('tenant_codigo', $tenant->codigo)
->whereIn('codigo', ['horario', 'servicio'])
->with('options')
->get()
->keyBy('codigo');
return [
'event_dates' => $tenant->eventDates()->get(),
'schedules' => $attributes->get('horario')?->options ?? new Collection,
'services' => $attributes->get('servicio')?->options ?? new Collection,
];
}
}

View File

@@ -1,6 +1,7 @@
<?php
use App\Domains\Forms\Controllers\AdminApp\EventFormController;
use App\Domains\Forms\Controllers\AdminApp\FoodFormController;
use App\Domains\Forms\Controllers\AdminApp\MerchandiseFormController;
use App\Domains\Forms\Controllers\AdminApp\SaleFormController;
use App\Domains\Forms\Controllers\AdminApp\StaffFormController;
@@ -16,4 +17,8 @@ Route::prefix('v1/adminapp/forms')
'fiesta-futbol-infantil/merchandise',
MerchandiseFormController::class
);
Route::get(
'fiesta-futbol-infantil/food',
FoodFormController::class
);
});