|
2 | 2 |
|
3 | 3 | namespace PH7\ApiSimpleMenu\Dal; |
4 | 4 |
|
5 | | - |
6 | | -use Ramsey\Uuid\Uuid; |
| 5 | +use PH7\ApiSimpleMenu\Entity\Item as ItemEntity; |
7 | 6 | use RedBeanPHP\R; |
| 7 | + |
8 | 8 | class FoodItemDal |
9 | 9 | { |
10 | 10 | public const TABLE_NAME = 'fooditems'; // Cannot have underscore. Use one word |
11 | 11 |
|
12 | | - public static function get(string $itemUuid): ?array |
| 12 | + public static function get(string $itemUuid): ItemEntity |
13 | 13 | { |
14 | 14 | $bindings = ['itemUuid' => $itemUuid]; |
15 | 15 | $itemBean = R::findOne(self::TABLE_NAME, 'item_uuid = :itemUuid', $bindings); |
16 | 16 |
|
17 | | - return $itemBean?->export(); |
| 17 | + return (new ItemEntity())->unserialize($itemBean?->export()); |
18 | 18 | } |
19 | 19 |
|
20 | 20 | public static function getAll(): array |
21 | 21 | { |
22 | | - return R::findAll(self::TABLE_NAME); |
| 22 | + $itemsBean = R::findAll(self::TABLE_NAME); |
| 23 | + |
| 24 | + $areAnyItems = $itemsBean && count($itemsBean); |
| 25 | + |
| 26 | + if (!$areAnyItems) { |
| 27 | + // if no items found, return empty array |
| 28 | + return []; |
| 29 | + } |
| 30 | + |
| 31 | + return array_map( |
| 32 | + function (object $itemBean): array { |
| 33 | + $itemEntity = (new ItemEntity())->unserialize($itemBean?->export()); |
| 34 | + |
| 35 | + // Select the fields we want to export and give back to the client |
| 36 | + return [ |
| 37 | + 'foodUuid' => $itemEntity->getItemUuid(), |
| 38 | + 'name' => $itemEntity->getName(), |
| 39 | + 'price' => $itemEntity->getPrice(), |
| 40 | + 'available' => $itemEntity->getAvailable() |
| 41 | + ]; |
| 42 | + }, $itemsBean); |
23 | 43 | } |
24 | 44 |
|
25 | | - public static function createDefaultItem(): int|string |
| 45 | + public static function createDefaultItem(ItemEntity $itemEntity): int|string |
26 | 46 | { |
27 | 47 | $itemBan = R::dispense(self::TABLE_NAME); |
28 | 48 |
|
29 | | - $itemBan->item_uuid = Uuid::uuid4()->toString(); |
30 | | - $itemBan->name = 'Burrito Chips'; |
31 | | - $itemBan->price = 19.55; |
32 | | - $itemBan->available = true; |
| 49 | + // TODO Move this into Food Entity to build the item |
| 50 | + |
| 51 | + $itemBan->item_uuid = $itemEntity->getItemUuid(); |
| 52 | + $itemBan->name = $itemEntity->getName(); |
| 53 | + $itemBan->price = $itemEntity->getPrice(); |
| 54 | + $itemBan->available = $itemEntity->getAvailable(); |
33 | 55 |
|
34 | 56 | // return the increment entry ID |
35 | 57 | return R::store($itemBan); |
|
0 commit comments