<?php
namespace App\Controller\Main;
use App\Services\Main\HistoricalService;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Serializer\Exception\ExceptionInterface;
/**
* Class HistoricalController.
*
* @Route("historical")
*/
class HistoricalController extends AbstractController
{
/**
* @var HistoricalService
*/
private HistoricalService $historicalService;
/**
* HistoricalController constructor.
*
* @param HistoricalService $HistoricalService
*/
public function __construct(HistoricalService $historicalService)
{
$this->historicalService = $historicalService;
}
/**
* @Route("/get",
* name="historical.get",
* methods={"POST"},
* options={"expose" = true}
* )
*
* @ParamConverter("from", class="string")
* @ParamConverter("to", class="string")
* @ParamConverter("listUsers", class="array")
* @ParamConverter("listTypes", class="array")
*
* @param Request $request
*
* @throws ExceptionInterface
*
* @return Response
*/
public function getHistoricals(Request $request): Response
{
$listParameters = $request->request->all();
$listFilters = [
'user' => json_decode($listParameters['listUsers']),
'object' => json_decode($listParameters['listTypes']),
'period' => [
'from' => $listParameters['from'],
'to' => $listParameters['to'],
],
];
if ($request->request->has('objectCode')) {
$listFilters['objectCode'] = $listParameters['objectCode'];
}
if ($request->request->has('start') && $request->request->has('length')) {
$listFilters['start'] = $listParameters['start'];
$listFilters['length'] = $listParameters['length'];
$listFilters['sortBy'] = $listParameters['order'][0]['column'];
$listFilters['orderBy'] = $listParameters['order'][0]['dir'];
}
if ($request->request->has('search')) {
$listFilters['search'] = $listParameters['search']['value'];
}
$listSerializeOptions = $listParameters['listSerializeOptions'] ?? [];
$listHistoricals = $this->historicalService->getHistorical($listFilters, $listSerializeOptions);
return $this->json([
'draw' => $listParameters['draw'],
'recordsTotal' => $listHistoricals['totalRows'],
'recordsFiltered' => $listHistoricals['totalRows'],
'data' => $listHistoricals['historicals'],
]);
}
/**
* @Route(
* "/restore",
* name="restore",
* methods={"POST"}
* )
*
* @param Request $request
*
* @return Response
*/
public function restoreHistoricals(Request $request): Response
{
return $this->json(
$this->historicalService->restore(
$request->request->get('id'),
json_decode($request->request->get('properties') ?? '{}', true)
)
);
}
}