<?php
namespace App\Controller\ApiController;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use JMS\Serializer\SerializerBuilder;
use JMS\Serializer\SerializationContext;
use App\Entity\User;
use App\Service\HistoriquePageService;
/**
* @Route("/api/historique", name="api_historique")
*/
class HistoriquePageController extends AbstractController
{
/**
* @Route("/user/{id}/refreshHistorique", name="refresh_historique_for_user", methods={"POST"}, requirements={"id"="\d+"})
*/
public function refreshHistoriqueForUser(Request $request, User $user, HistoriquePageService $historiqueService)
{
$serializer = SerializerBuilder::create()->build();
// Params
$pathInfo = $request->request->get('pathInfo', "");
$requestParams = json_decode($request->request->get('request', ""));
$queryParams = json_decode($request->request->get('query', ""));
if ($user) {
// Update des historiques
$historiqueService->sqlUpdateCurrentPage($user, $pathInfo);
// Users connectés
$currentUsers = $historiqueService->getCurrentPageConnectedUsers($user, $pathInfo);
$data = $serializer->serialize($currentUsers, 'json', SerializationContext::create()->setSerializeNull(true));
$response = new Response($data, 201);
$response->headers->set('Content-Type', 'application/json');
} else {
$notFound =
[
"error" => "USER_NOT_FOUND",
"error_description" => "User cannot be found in database"
]
;
$data = $serializer->serialize($notFound, 'json', SerializationContext::create()->setSerializeNull(true));
$response = new Response($data, 404);
$response->headers->set('Content-Type', 'application/json');
}
return $response;
}
}