| 1234567891011121314151617181920212223242526272829303132333435363738394041424344 |
- <?php
- namespace Controllers;
- use Libs\ResponseLib;
- use Models\OrderItemModel;
- use Psr\Http\Message\ServerRequestInterface;
- use Respect\Validation\Validator as v;
- use Respect\Validation\Exceptions\ValidationException;
- class OrderItemCreateController
- {
- private OrderItemModel $model;
- public function __construct()
- {
- $this->model = new OrderItemModel();
- }
- public function __invoke(ServerRequestInterface $request)
- {
- $body = json_decode((string)$request->getBody(), true) ?? [];
- try {
- // company_id, order_id e product_id são obrigatórios para criar um item de pedido
- v::key('company_id', v::intType()->positive()) // company_id agora é obrigatório
- ->key('order_id', v::intType()->positive())
- ->key('product_id', v::intType()->positive())
- ->assert($body);
- } catch (ValidationException $e) {
- return ResponseLib::sendFail("Validation failed: " . $e->getFullMessage(), [], "E_VALIDATE")->withStatus(400);
- }
- $companyId = (int) $body['company_id'];
- $orderId = (int) $body['order_id'];
- $productId = (int) $body['product_id'];
- $created = $this->model->createOrderItem($orderId, $productId, $companyId);
- return $created
- ? ResponseLib::sendOk(['created' => true, 'order_item_id' => $created])
- : ResponseLib::sendFail("Failed to create order item", [], "E_DATABASE")->withStatus(500);
- }
- }
|