Skip to content

Commit 62575f6

Browse files
committed
Automatically add default food item if none has been inserted
1 parent 32f405c commit 62575f6

File tree

2 files changed

+12
-8
lines changed

2 files changed

+12
-8
lines changed

src/Dal/FoodItemDal.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public static function getAll(): array
2222
return R::findAll(self::TABLE_NAME);
2323
}
2424

25-
public static function create(): void
25+
public static function createDefaultItem(): int|string
2626
{
2727
$itemBan = R::dispense(self::TABLE_NAME);
2828

@@ -31,6 +31,7 @@ public static function create(): void
3131
$itemBan->price = 19.55;
3232
$itemBan->available = true;
3333

34-
R::store($itemBan);
34+
// return the increment entry ID
35+
return R::store($itemBan);
3536
}
3637
}

src/Service/FoodItem.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,6 @@
88

99
class FoodItem
1010
{
11-
public function __construct()
12-
{
13-
// Generate an item for us
14-
FoodItemDal::create();
15-
}
16-
1711
public function retrieve(string $itemUuid): array
1812
{
1913
if (v::uuid()->validate($itemUuid)) {
@@ -34,6 +28,15 @@ public function retrieveAll(): array
3428
{
3529
$items = FoodItemDal::getAll();
3630

31+
if (count($items) === 0) {
32+
// if no items have been added yet, create the first one
33+
FoodItemDal::createDefaultItem();
34+
35+
// then, get again all items
36+
// to retrieve the new one that just got added
37+
$items = FoodItemDal::getAll();
38+
}
39+
3740
return array_map(function (object $item): object {
3841
// Remove unnecessary "id" field
3942
unset($item['id']);

0 commit comments

Comments
 (0)