| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566 |
- <?php
- namespace Controllers;
- use Libs\ResponseLib;
- use Models\OrderbookSearchModel;
- use Psr\Http\Message\ServerRequestInterface;
- use Respect\Validation\Exceptions\ValidationException;
- use Respect\Validation\Validator as val;
- class OrderbookFilterController
- {
- private OrderbookSearchModel $searchModel;
- public function __construct()
- {
- $this->searchModel = new OrderbookSearchModel();
- }
- public function __invoke(ServerRequestInterface $request)
- {
- $body = json_decode((string)$request->getBody(), true) ?? [];
- try {
- val::key('state', val::stringType()->notEmpty())
- ->key('commodity_type', val::stringType()->notEmpty())
- ->assert($body);
- } catch (ValidationException $e) {
- return ResponseLib::sendFail(
- 'Validation failed: ' . $e->getFullMessage(),
- [],
- 'E_VALIDATE'
- )->withStatus(400);
- }
- $state = strtoupper(trim((string)$body['state']));
- $commodityType = trim((string)$body['commodity_type']);
- try {
- $orders = $this->searchModel->searchOpenOrders($state, $commodityType);
- } catch (\Throwable $e) {
- return ResponseLib::sendFail(
- 'Falha ao consultar orderbook: ' . $e->getMessage(),
- [],
- 'E_DATABASE'
- )->withStatus(500);
- }
- if (!$orders) {
- return ResponseLib::sendOk(
- [
- 'state' => $state,
- 'commodity_type' => $commodityType,
- 'orders' => [],
- ],
- 'S_ORDERBOOK_EMPTY'
- );
- }
- return ResponseLib::sendOk([
- 'state' => $state,
- 'commodity_type' => $commodityType,
- 'orders' => $orders,
- ], 'S_ORDERBOOK_FILTER');
- }
- }
|