<?php
namespace App\Controller\API;
use App\Controller\BaseController;
use App\Entity\Log\LogInteraction;
use App\Services\API\BuscadorGeneralService;
use App\Services\API\NoticiasService;
use App\Utils\APIResponse;
use App\Utils\CommonFunctions;
use App\Utils\Constants;
use Exception;
use OpenApi\Annotations as OA;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
/**
* @Route("/noticias")
*/
class NoticiasController extends BaseController
{
/**
* @Route("/principales", name="get_noticias_principales", methods={"GET"})
* @param Request $request
* @param NoticiasService $noticiasService
* @return Response
*
* @OA\Get(
* path="/api/v1/noticias/principales",
* summary="Obtener las noticias principales",
* @OA\Response(
* response=200,
* description="Response",
* @OA\MediaType(
* mediaType="application/json",
* @OA\Schema(
* @OA\Property(property="status", type="integer"),
* @OA\Property(property="errors", type="array", @OA\Items(type="string")),
* @OA\Property(property="content", type="object")
* )
* )
* )
* )
*/
public function noticiasPrincipalesAction(Request $request, NoticiasService $noticiasService)
{
$statusCode = APIResponse::$SUCCESS;
$status = APIResponse::$SUCCESS;
$errors = array();
$result = null;
try {
$result = $noticiasService->getNoticiasPrincipales();
} catch (Exception $e) {
$statusCode = APIResponse::$INTERNAL_ERROR;
$status = APIResponse::$INTERNAL_ERROR;
$errors = array($e->getMessage());
$errorInfo = CommonFunctions::getErrorException($e);
$this->logInteractionService->addErrorLog(
LogInteraction::$LIST,
CommonFunctions::getClassMethod(__METHOD__),
$e->getMessage(),
$errorInfo
);
}
return $this->generateJsonResponse($result, $statusCode, $status, $errors);
}
/**
* @Route("/filtros", name="get_noticias_filtros", methods={"GET"})
* @param Request $request
* @param BuscadorGeneralService $buscadorGeneralService
* @return Response
*
* @OA\Get(
* path="/api/v1/noticias/filtros",
* summary="Obtener los filtros para noticias",
* @OA\Response(
* response=200,
* description="Response",
* @OA\MediaType(
* mediaType="application/json",
* @OA\Schema(
* @OA\Property(property="status", type="integer"),
* @OA\Property(property="errors", type="array", @OA\Items(type="string")),
* @OA\Property(property="content", type="object")
* )
* )
* )
* )
*/
public function noticiasFiltrosAction(Request $request, BuscadorGeneralService $buscadorGeneralService)
{
$statusCode = APIResponse::$SUCCESS;
$status = APIResponse::$SUCCESS;
$errors = array();
$result = null;
try {
$result = array(
'areas' => $buscadorGeneralService->getAreas(),
'fechas' => array_map(function ($key, $item) {
return array('texto' => $item['texto'], 'valor' => $key);
}, array_keys(Constants::FILTRO_FECHAS_NOTICIAS), Constants::FILTRO_FECHAS_NOTICIAS)
);
} catch (Exception $e) {
$statusCode = APIResponse::$INTERNAL_ERROR;
$status = APIResponse::$INTERNAL_ERROR;
$errors = array($e->getMessage());
$errorInfo = CommonFunctions::getErrorException($e);
$this->logInteractionService->addErrorLog(
LogInteraction::$LIST,
CommonFunctions::getClassMethod(__METHOD__),
$e->getMessage(),
$errorInfo
);
}
return $this->generateJsonResponse($result, $statusCode, $status, $errors);
}
/**
* @Route("/{page}", name="get_noticias", methods={"GET"})
* @param Request $request
* @param NoticiasService $noticiasService
* @param $page
* @return Response
*
* @OA\Get(
* path="/api/v1/noticias/{page}",
* summary="Obtener todas las noticias",
* @OA\Parameter(
* name="areas",
* in="query",
* required=false,
* @OA\Schema(type="string")
* ),
* @OA\Parameter(
* name="tags",
* in="query",
* required=false,
* @OA\Schema(type="string")
* ),
* @OA\Parameter(
* name="fecha",
* in="query",
* required=false,
* @OA\Schema(type="string")
* ),
* @OA\Response(
* response=200,
* description="Response",
* @OA\MediaType(
* mediaType="application/json",
* @OA\Schema(
* @OA\Property(property="status", type="integer"),
* @OA\Property(property="errors", type="array", @OA\Items(type="string")),
* @OA\Property(property="content", type="object")
* )
* )
* )
* )
*/
public function noticiasAction(Request $request, NoticiasService $noticiasService, $page)
{
$statusCode = APIResponse::$SUCCESS;
$status = APIResponse::$SUCCESS;
$errors = array();
$result = null;
try {
$filtros = CommonFunctions::parseQueryString($request->getQueryString());
$result = $noticiasService->getAllNoticias($page, $filtros);
} catch (Exception $e) {
$statusCode = APIResponse::$INTERNAL_ERROR;
$status = APIResponse::$INTERNAL_ERROR;
$errors = array($e->getMessage());
$errorInfo = CommonFunctions::getErrorException($e);
$this->logInteractionService->addErrorLog(
LogInteraction::$LIST,
CommonFunctions::getClassMethod(__METHOD__),
$e->getMessage(),
$errorInfo
);
}
return $this->generateJsonResponse($result, $statusCode, $status, $errors);
}
}