70 lines
2.3 KiB
PHP
70 lines
2.3 KiB
PHP
<?php
|
|
|
|
namespace App\api;
|
|
|
|
require_once '../app/models/ReportsModel.php';
|
|
|
|
use App\models\ReportsModel;
|
|
|
|
|
|
use Nyholm\Psr7\Response;
|
|
use PDOException;
|
|
use Psr\Http\Message\ServerRequestInterface;
|
|
use Psr\Http\Message\ResponseInterface;
|
|
|
|
|
|
class ReportController
|
|
{
|
|
|
|
public function __construct(public ReportsModel $reportsModel) {}
|
|
public function show(ServerRequestInterface $request, $queryParams)
|
|
{
|
|
$connection = $this->reportsModel->dbConnect();
|
|
|
|
if (!$connection instanceof \PDO) {
|
|
return new Response(500, ['Content-Type' => 'application/json'], json_encode(['error' => 'Erro interno. Contate a equipe de desenvolvimento.']));
|
|
}
|
|
|
|
if (!isset($queryParams['startData']) && !isset($queryParams['endData'])) {
|
|
return new Response(403, ['Content-Type' => 'application/json'], json_encode(['error' => 'O parâmetro não foi enviado corretamente, ou não existe. Tente novamente'], JSON_UNESCAPED_UNICODE));
|
|
}
|
|
|
|
try {
|
|
if (isset($queryParams['endData'])) {
|
|
$sql = 'SELECT * FROM queue_stats_mv WHERE datetime BETWEEN :startData AND :endData';
|
|
|
|
$startData = $queryParams['startData'];
|
|
$endData = $queryParams['endData'] . ' 23:59:59';
|
|
|
|
$stmt = $connection->prepare($sql);
|
|
|
|
$stmt->bindParam(':startData', $startData, \PDO::PARAM_STR);
|
|
$stmt->bindParam(':endData', $endData, \PDO::PARAM_STR);
|
|
} else {
|
|
$sql = 'SELECT * FROM queue_stats_mv WHERE datetime LIKE :startData';
|
|
|
|
$startData = $queryParams['startData'] . '%';
|
|
|
|
$stmt = $connection->prepare($sql);
|
|
|
|
$stmt->bindParam(':startData', $startData, \PDO::PARAM_STR);
|
|
}
|
|
|
|
$stmt->execute();
|
|
$result = $stmt->fetchAll(\PDO::FETCH_ASSOC);
|
|
|
|
return new Response(
|
|
200,
|
|
['Content-Type' => 'application/json'],
|
|
json_encode(['success' => true, 'data' => $result], JSON_UNESCAPED_UNICODE)
|
|
);
|
|
} catch (\PDOException $e) {
|
|
return new Response(
|
|
500,
|
|
['Content-Type' => 'application/json'],
|
|
json_encode(['error' => 'Erro ao executar a consulta: ' . $e->getMessage()], JSON_UNESCAPED_UNICODE)
|
|
);
|
|
}
|
|
}
|
|
}
|