<?php
declare(strict_types=1);
namespace App\Controller\Api;
use App\Entity\AccionAnuncio;
use App\Entity\AccionEstado;
use App\Entity\Canal;
use App\Entity\Reloj;
use App\Enum\EstadoRelojEnum;
use Doctrine\Common\Collections\Criteria;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpClient\Exception\InvalidArgumentException;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Request;
use Vich\UploaderBundle\Storage\StorageInterface;
final class RelojApiController extends AbstractController
{
public function __construct(private EntityManagerInterface $em, private StorageInterface $storage)
{
}
/**
* @Route("/api/reloj", methods={"GET"})
*/
public function getAction(Request $request)
{
try
{
if(!$request->query->has('id')) throw new InvalidArgumentException('RELOJ_NOT_EXISTS');
$reloj = $this->em->getRepository(Reloj::class)->find($request->query->get('id'));
if(!$reloj) throw new InvalidArgumentException('RELOJ_NOT_EXISTS');
return new Response(
json_encode(['data' => [
'IDPerseo' => $reloj->getIDPerseo(),
'foto' => $this->storage->resolveUri($reloj, 'fotoFile') ?? '/assets/img/not_available.png',
'fecha' => $reloj->getFecha(),
'marca' => $reloj->getMarca()?->getId(),
'modelo1' => $reloj->getModelo1(),
'ref1' => $reloj->getRef1(),
'serie' => $reloj->getSerie(),
'modelos' => $reloj->getModelos(),
'refs' => $reloj->getRefs(),
'aspecto' => $reloj->getAspecto()?->getId(),
'fecha' => $reloj->getFecha()?->format('d-m-Y'),
'caja' => $reloj->getCaja(),
'papeles' => $reloj->getPapeles(),
'stock' => $reloj->getStock(),
'chrono24' => $reloj->isChrono24(),
'comentario' => $reloj->getComentario(),
'precioVentaTotal' => $reloj->getPrecioVentaTotal(),
'precioCosteTotal' => $reloj->getPrecioCosteTotal(),
'precioPromocion' => $reloj->getPrecioPromocion(),
'garantia' => $reloj->getGarantia(),
'recompra' => $reloj->getRecompra(),
]]),
Response::HTTP_OK,
['content-type' => 'text/json']
);
}
catch(InvalidArgumentException $e)
{
return new Response(
json_encode(['error' => $e->getMessage()]),
Response::HTTP_BAD_REQUEST,
['content-type' => 'text/json']
);
}
}
/**
* @Route("/api/reloj/precio-promocion", methods={"GET"})
*/
public function getPrecioPromocionAction(Request $request)
{
try
{
if(!$request->query->has('id')) throw new InvalidArgumentException('RELOJ_NOT_EXISTS');
$reloj = $this->em->getRepository(Reloj::class)->find($request->query->get('id'));
if(!$reloj) throw new InvalidArgumentException('RELOJ_NOT_EXISTS');
$criteria = Criteria::create()
->orderBy([
'fecha' => Criteria::DESC,
'created_at' => Criteria::DESC,
]);
//if(!$request->query->has('plataforma')) throw new InvalidArgumentException('CANAL_NOT_EXISTS');
if($request->query->has('plataforma') && $request->query->get('plataforma')) {
$plataforma = $this->em->getRepository(Canal::class)->find($request->query->get('plataforma'));
if (!$plataforma) {
throw new InvalidArgumentException('CANAL_NOT_EXISTS');
}
$anuncio = $reloj->getPromociones()->getAcciones()
->filter(fn($accion) => $accion instanceof AccionAnuncio && $accion->getCanal() === $plataforma)
->matching($criteria)
->first();
}
else
{
$anuncio = $reloj->getPromociones()->getAcciones()
->filter(fn($accion) => $accion instanceof AccionEstado && $accion?->getEstado()?->getKey() === EstadoRelojEnum::ESTADO_ANUNCIO)
->matching($criteria)
->first();
}
return new Response(
json_encode(['precioAnuncio' => $anuncio ? $anuncio->getPrecio() : 0.00]),
Response::HTTP_OK,
['content-type' => 'text/json']
);
}
catch(InvalidArgumentException $e)
{
return new Response(
json_encode(['error' => $e->getMessage()]),
Response::HTTP_BAD_REQUEST,
['content-type' => 'text/json']
);
}
}
}