File tree Expand file tree Collapse file tree 2 files changed +12
-8
lines changed
Expand file tree Collapse file tree 2 files changed +12
-8
lines changed Original file line number Diff line number Diff 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}
Original file line number Diff line number Diff line change 88
99class 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 ' ]);
You can’t perform that action at this time.
0 commit comments