| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 |
- <?php
- namespace Controllers;
- use Libs\ResponseLib;
- use Models\OrderItemModel;
- use Models\ProductModel;
- use Psr\Http\Message\ServerRequestInterface;
- use Respect\Validation\Validator as v;
- use Respect\Validation\Exceptions\ValidationException;
- class OrderItemGetController
- {
- private OrderItemModel $model;
- public function __construct()
- {
- $this->model = new OrderItemModel();
- }
- public function __invoke(ServerRequestInterface $request)
- {
- $body = json_decode((string)$request->getBody(), true) ?? [];
- try {
- v::key('company_id', v::intType()->positive())->assert($body);
- if (isset($body['order_id'])) {
- v::key('order_id', v::intType()->positive())->assert($body);
- } else {
- if (isset($body['page'])) {
- v::key('page', v::intType()->positive())->assert($body);
- }
- }
- } catch (ValidationException $e) {
- return ResponseLib::sendFail("Validation failed: " . $e->getFullMessage(), [], "E_VALIDATE")->withStatus(401);
- }
- $companyId = (int) $body['company_id'];
- $orderId = isset($body['order_id']) ? (int) $body['order_id'] : null;
- $page = isset($body['page']) ? (int) $body['page'] : 1;
- if ($page < 1) {
- $page = 1;
- }
- $orderItems = $this->model->getOrderItemsByOrderId($orderId, $companyId, $page);
- $productModel = new ProductModel();
- foreach ($orderItems as &$item) {
- $product = $productModel->getProductById($item['product_id'], $companyId);
- $item['product_details'] = $product;
- }
- if (!empty($orderItems)) {
- return ResponseLib::sendOk($orderItems);
- }
- return ResponseLib::sendFail("No Order Items Found for the Given Data", [], "E_DATABASE")->withStatus(204);
- }
- }
|