Skip to content

Commit 32f405c

Browse files
committed
Add Food Item service & DAL
1 parent ef26426 commit 32f405c

File tree

3 files changed

+131
-0
lines changed

3 files changed

+131
-0
lines changed

src/Dal/FoodItemDal.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace PH7\ApiSimpleMenu\Dal;
4+
5+
6+
use Ramsey\Uuid\Uuid;
7+
use RedBeanPHP\R;
8+
class FoodItemDal
9+
{
10+
public const TABLE_NAME = 'fooditems'; // Cannot have underscore. Use one word
11+
12+
public static function get(string $itemUuid): ?array
13+
{
14+
$bindings = ['itemUuid' => $itemUuid];
15+
$itemBean = R::findOne(self::TABLE_NAME, 'item_uuid = :itemUuid', $bindings);
16+
17+
return $itemBean?->export();
18+
}
19+
20+
public static function getAll(): array
21+
{
22+
return R::findAll(self::TABLE_NAME);
23+
}
24+
25+
public static function create(): void
26+
{
27+
$itemBan = R::dispense(self::TABLE_NAME);
28+
29+
$itemBan->item_uuid = Uuid::uuid4()->toString();
30+
$itemBan->name = 'Burrito Chips';
31+
$itemBan->price = 19.55;
32+
$itemBan->available = true;
33+
34+
R::store($itemBan);
35+
}
36+
}

src/Service/FoodItem.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
namespace PH7\ApiSimpleMenu\Service;
4+
5+
use PH7\ApiSimpleMenu\Dal\FoodItemDal;
6+
use PH7\ApiSimpleMenu\Validation\Exception\InvalidValidationException;
7+
use Respect\Validation\Validator as v;
8+
9+
class FoodItem
10+
{
11+
public function __construct()
12+
{
13+
// Generate an item for us
14+
FoodItemDal::create();
15+
}
16+
17+
public function retrieve(string $itemUuid): array
18+
{
19+
if (v::uuid()->validate($itemUuid)) {
20+
if ($item = FoodItemDal::get($itemUuid)) {
21+
// Removing fields we don't want to expose
22+
unset($item['id']);
23+
24+
return $item;
25+
}
26+
27+
return [];
28+
}
29+
30+
throw new InvalidValidationException("Invalid user UUID");
31+
}
32+
33+
public function retrieveAll(): array
34+
{
35+
$items = FoodItemDal::getAll();
36+
37+
return array_map(function (object $item): object {
38+
// Remove unnecessary "id" field
39+
unset($item['id']);
40+
return $item;
41+
}, $items);
42+
}
43+
}

src/routes/food-item.routes.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
namespace PH7\ApiSimpleMenu;
3+
4+
use PH7\ApiSimpleMenu\Service\FoodItem;
5+
use PH7\ApiSimpleMenu\Validation\Exception\InvalidValidationException;
6+
7+
use PH7\JustHttp\StatusCode;
8+
use PH7\PhpHttpResponseHeader\Http;
9+
10+
enum FoodItemAction: string
11+
{
12+
case RETRIEVE_ALL = 'retrieveall';
13+
case RETRIEVE = 'retrieve';
14+
15+
public function getResponse(): string
16+
{
17+
$postBody = file_get_contents('php://input');
18+
$postBody = json_decode($postBody);
19+
20+
// Ternary conditional operator operator
21+
$itemId = $_REQUEST['id'] ?? ''; // using the null coalescing operator
22+
23+
$item = new FoodItem();
24+
try {
25+
$response = match ($this) {
26+
self::RETRIEVE_ALL => $item->retrieveAll(),
27+
self::RETRIEVE => $item->retrieve($itemId),
28+
};
29+
} catch (InvalidValidationException $e) {
30+
// Send 400 http status code
31+
Http::setHeadersByCode(StatusCode::BAD_REQUEST);
32+
33+
$response = [
34+
'errors' => [
35+
'message' => $e->getMessage(),
36+
'code' => $e->getCode()
37+
]
38+
];
39+
}
40+
41+
return json_encode($response);
42+
}
43+
}
44+
45+
$action = $_REQUEST['action'] ?? null;
46+
47+
$itemAction = FoodItemAction::tryFrom($action);
48+
if ($itemAction) {
49+
echo $itemAction->getResponse();
50+
} else {
51+
require_once 'not-found.routes.php';
52+
}

0 commit comments

Comments
 (0)