Skip to content

Commit 15afed7

Browse files
committed
Split menu request
1 parent 9a2ba5d commit 15afed7

File tree

5 files changed

+141
-66
lines changed

5 files changed

+141
-66
lines changed

app/Http/Controllers/MenuController.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@
1515
use Kami\Cocktail\Rules\ResourceBelongsToBar;
1616
use Kami\Cocktail\Http\Resources\MenuResource;
1717
use Illuminate\Http\Resources\Json\JsonResource;
18-
use Kami\Cocktail\Models\Enums\MenuItemTypeEnum;
18+
use Kami\Cocktail\OpenAPI\Schemas\MenuItemRequest;
1919
use Kami\Cocktail\Http\Resources\MenuPublicResource;
20+
use Kami\Cocktail\OpenAPI\Schemas\MenuRequest as SchemasMenuRequest;
2021

2122
class MenuController extends Controller
2223
{
@@ -98,27 +99,26 @@ public function update(MenuRequest $request): MenuResource
9899
abort(403);
99100
}
100101

101-
/** @var array<mixed> */
102-
$items = $request->input('items', []);
102+
$schemaRequest = SchemasMenuRequest::fromIlluminateRequest($request);
103103

104-
$ingredients = collect($items)->where('type', MenuItemTypeEnum::Ingredient->value)->values()->toArray();
105-
$cocktails = collect($items)->where('type', MenuItemTypeEnum::Cocktail->value)->values()->toArray();
104+
$ingredients = $schemaRequest->getIngredients();
105+
$cocktails = $schemaRequest->getCocktails();
106106

107-
Validator::make($ingredients, [
107+
Validator::make(collect($ingredients)->map(fn (MenuItemRequest $mi) => ['id' => $mi->id])->toArray(), [
108108
'*.id' => [new ResourceBelongsToBar(bar()->id, 'ingredients')],
109109
])->validate();
110110

111-
Validator::make($cocktails, [
111+
Validator::make(collect($cocktails)->map(fn (MenuItemRequest $mi) => ['id' => $mi->id])->toArray(), [
112112
'*.id' => [new ResourceBelongsToBar(bar()->id, 'cocktails')],
113113
])->validate();
114114

115115
$menu = Menu::firstOrCreate(['bar_id' => bar()->id]);
116-
$menu->is_enabled = $request->boolean('is_enabled');
116+
$menu->is_enabled = $schemaRequest->isEnabled;
117117
if (!$menu->created_at) {
118118
$menu->created_at = now();
119119
}
120120
$menu->updated_at = now();
121-
$menu->syncItems($items);
121+
$menu->syncItems($schemaRequest->items);
122122
$menu->save();
123123

124124
return new MenuResource($menu);

app/Models/Menu.php

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Illuminate\Database\Eloquent\Model;
1111
use Kami\Cocktail\Models\ValueObjects\MenuItem;
1212
use Kami\Cocktail\Models\Enums\MenuItemTypeEnum;
13+
use Kami\Cocktail\OpenAPI\Schemas\MenuItemRequest;
1314
use Illuminate\Database\Eloquent\Relations\HasMany;
1415
use Illuminate\Database\Eloquent\Relations\BelongsTo;
1516
use Illuminate\Database\Eloquent\Factories\HasFactory;
@@ -61,7 +62,7 @@ public function getMenuItems(): Collection
6162
}
6263

6364
/**
64-
* @param array<int, array<string, mixed>> $menuItems
65+
* @param array<MenuItemRequest> $menuItems
6566
*/
6667
public function syncItems(array $menuItems): void
6768
{
@@ -70,33 +71,33 @@ public function syncItems(array $menuItems): void
7071

7172
foreach ($menuItems as $menuItem) {
7273
$price = 0;
73-
if ($menuItem['price'] ?? false) {
74+
if ($menuItem->price ?? false) {
7475
$price = Money::of(
75-
$menuItem['price'],
76-
$menuItem['currency'],
76+
$menuItem->price,
77+
$menuItem->currency,
7778
roundingMode: RoundingMode::UP
7879
)->getMinorAmount()->toInt();
7980
}
8081

81-
if (MenuItemTypeEnum::from($menuItem['type']) === MenuItemTypeEnum::Ingredient) {
82-
$currentIngredientMenuItems[] = $menuItem['id'];
82+
if ($menuItem->type === MenuItemTypeEnum::Ingredient) {
83+
$currentIngredientMenuItems[] = $menuItem->id;
8384
$this->menuIngredients()->updateOrCreate([
84-
'ingredient_id' => $menuItem['id']
85+
'ingredient_id' => $menuItem->id
8586
], [
86-
'category_name' => $menuItem['category_name'],
87-
'sort' => $menuItem['sort'] ?? 0,
87+
'category_name' => $menuItem->categoryName,
88+
'sort' => $menuItem->sort ?? 0,
8889
'price' => $price,
89-
'currency' => $menuItem['currency'] ?? null,
90+
'currency' => $menuItem->currency ?? null,
9091
]);
9192
} else {
92-
$currentCocktailMenuItems[] = $menuItem['id'];
93+
$currentCocktailMenuItems[] = $menuItem->id;
9394
$this->menuCocktails()->updateOrCreate([
94-
'cocktail_id' => $menuItem['id']
95+
'cocktail_id' => $menuItem->id
9596
], [
96-
'category_name' => $menuItem['category_name'],
97-
'sort' => $menuItem['sort'] ?? 0,
97+
'category_name' => $menuItem->categoryName,
98+
'sort' => $menuItem->sort ?? 0,
9899
'price' => $price,
99-
'currency' => $menuItem['currency'] ?? null,
100+
'currency' => $menuItem->currency ?? null,
100101
]);
101102
}
102103
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Kami\Cocktail\OpenAPI\Schemas;
6+
7+
use OpenApi\Attributes as OAT;
8+
use Kami\Cocktail\Models\Enums\MenuItemTypeEnum;
9+
10+
#[OAT\Schema(required: ['id', 'type', 'category_name', 'sort', 'price', 'currency'])]
11+
class MenuItemRequest
12+
{
13+
public function __construct(
14+
#[OAT\Property(example: 1)]
15+
public int $id,
16+
#[OAT\Property()]
17+
public MenuItemTypeEnum $type,
18+
#[OAT\Property(property: 'category_name', example: 'Category name')]
19+
public string $categoryName,
20+
#[OAT\Property(example: 1)]
21+
public int $sort,
22+
#[OAT\Property(example: 22.52)]
23+
public float $price,
24+
#[OAT\Property(example: 'EUR', format: 'ISO 4217')]
25+
public string $currency,
26+
) {
27+
}
28+
29+
/**
30+
* @param array<mixed> $input
31+
*/
32+
public static function fromArray(array $input): self
33+
{
34+
return new self(
35+
id: (int) $input['id'],
36+
type: MenuItemTypeEnum::from($input['type']),
37+
categoryName: $input['category_name'],
38+
sort: (int) $input['sort'],
39+
price: (float) $input['price'],
40+
currency: $input['currency'] ?? 'EUR',
41+
);
42+
}
43+
}

app/OpenAPI/Schemas/MenuRequest.php

Lines changed: 43 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,52 @@
44

55
namespace Kami\Cocktail\OpenAPI\Schemas;
66

7+
use Illuminate\Http\Request;
78
use OpenApi\Attributes as OAT;
8-
use Kami\Cocktail\Models\Enums\MenuItemTypeEnum;
99

1010
#[OAT\Schema(required: ['is_enabled', 'items'])]
1111
class MenuRequest
1212
{
13-
#[OAT\Property(property: 'is_enabled')]
14-
public bool $isEnabled = false;
15-
/** @var array<mixed> */
16-
#[OAT\Property(type: 'array', items: new OAT\Items(type: 'object', properties: [
17-
new OAT\Property(type: 'integer', property: 'id', example: 1),
18-
new OAT\Property(type: 'schema', property: 'type', ref: MenuItemTypeEnum::class),
19-
new OAT\Property(type: 'string', property: 'category_name', example: 'Category name'),
20-
new OAT\Property(type: 'integer', property: 'sort', example: 1),
21-
new OAT\Property(type: 'integer', property: 'price', example: 2252, format: 'minor'),
22-
new OAT\Property(type: 'string', property: 'currency', example: 'EUR', format: 'ISO 4217'),
23-
], required: ['id', 'type', 'category_name', 'sort', 'price', 'currency'])),
24-
]
25-
public array $items = [];
13+
/**
14+
* @param array<MenuItemRequest> $items
15+
*/
16+
public function __construct(
17+
#[OAT\Property(property: 'is_enabled')]
18+
public bool $isEnabled = false,
19+
#[OAT\Property(items: new OAT\Items(type: MenuItemRequest::class))]
20+
public array $items = [],
21+
) {
22+
}
23+
24+
public static function fromIlluminateRequest(Request $request): self
25+
{
26+
/** @var array<mixed> */
27+
$formItems = $request->post('items', []);
28+
29+
$items = [];
30+
foreach ($formItems as $formItem) {
31+
$items[] = MenuItemRequest::fromArray($formItem);
32+
}
33+
34+
return new self(
35+
isEnabled: $request->boolean('is_enabled', false),
36+
items: $items,
37+
);
38+
}
39+
40+
/**
41+
* @return array<MenuItemRequest>
42+
*/
43+
public function getIngredients(): array
44+
{
45+
return array_filter($this->items, fn (MenuItemRequest $item) => $item->type === \Kami\Cocktail\Models\Enums\MenuItemTypeEnum::Ingredient);
46+
}
47+
48+
/**
49+
* @return array<MenuItemRequest>
50+
*/
51+
public function getCocktails(): array
52+
{
53+
return array_filter($this->items, fn (MenuItemRequest $item) => $item->type === \Kami\Cocktail\Models\Enums\MenuItemTypeEnum::Cocktail);
54+
}
2655
}

docs/openapi-generated.yaml

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -11179,6 +11179,35 @@ components:
1117911179
- 'null'
1118011180
example: 'My device'
1118111181
type: object
11182+
MenuItemRequest:
11183+
required:
11184+
- id
11185+
- type
11186+
- category_name
11187+
- sort
11188+
- price
11189+
- currency
11190+
properties:
11191+
id:
11192+
type: integer
11193+
example: 1
11194+
type:
11195+
$ref: '#/components/schemas/MenuItemTypeEnum'
11196+
category_name:
11197+
type: string
11198+
example: 'Category name'
11199+
sort:
11200+
type: integer
11201+
example: 1
11202+
price:
11203+
type: number
11204+
format: float
11205+
example: 22.52
11206+
currency:
11207+
type: string
11208+
format: 'ISO 4217'
11209+
example: EUR
11210+
type: object
1118211211
MenuRequest:
1118311212
required:
1118411213
- is_enabled
@@ -11189,34 +11218,7 @@ components:
1118911218
items:
1119011219
type: array
1119111220
items:
11192-
required:
11193-
- id
11194-
- type
11195-
- category_name
11196-
- sort
11197-
- price
11198-
- currency
11199-
properties:
11200-
id:
11201-
type: integer
11202-
example: 1
11203-
type:
11204-
$ref: '#/components/schemas/MenuItemTypeEnum'
11205-
category_name:
11206-
type: string
11207-
example: 'Category name'
11208-
sort:
11209-
type: integer
11210-
example: 1
11211-
price:
11212-
type: integer
11213-
format: minor
11214-
example: 2252
11215-
currency:
11216-
type: string
11217-
format: 'ISO 4217'
11218-
example: EUR
11219-
type: object
11221+
$ref: '#/components/schemas/MenuItemRequest'
1122011222
type: object
1122111223
NoteRequest:
1122211224
required:

0 commit comments

Comments
 (0)