src/Controller/Api/RelojApiController.php line 81

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Controller\Api;
  4. use App\Entity\AccionAnuncio;
  5. use App\Entity\AccionEstado;
  6. use App\Entity\Canal;
  7. use App\Entity\Reloj;
  8. use App\Enum\EstadoRelojEnum;
  9. use Doctrine\Common\Collections\Criteria;
  10. use Doctrine\ORM\EntityManagerInterface;
  11. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  12. use Symfony\Component\HttpClient\Exception\InvalidArgumentException;
  13. use Symfony\Component\HttpFoundation\Response;
  14. use Symfony\Component\Routing\Annotation\Route;
  15. use Symfony\Component\HttpFoundation\Request;
  16. use Vich\UploaderBundle\Storage\StorageInterface;
  17. final class RelojApiController extends AbstractController
  18. {
  19.     public function __construct(private EntityManagerInterface $em, private StorageInterface $storage)
  20.     {
  21.     }
  22.     /**
  23.      * @Route("/api/reloj", methods={"GET"})
  24.      */
  25.     public function getAction(Request $request)
  26.     {
  27.         try
  28.         {
  29.             if(!$request->query->has('id')) throw new InvalidArgumentException('RELOJ_NOT_EXISTS');
  30.             $reloj $this->em->getRepository(Reloj::class)->find($request->query->get('id'));
  31.             if(!$reloj) throw new InvalidArgumentException('RELOJ_NOT_EXISTS');
  32.             return new Response(
  33.                 json_encode(['data' => [
  34.                     'IDPerseo' => $reloj->getIDPerseo(),
  35.                     'foto' => $this->storage->resolveUri($reloj'fotoFile') ?? '/assets/img/not_available.png',
  36.                     'fecha' => $reloj->getFecha(),
  37.                     'marca' => $reloj->getMarca()?->getId(),
  38.                     'modelo1' => $reloj->getModelo1(),
  39.                     'ref1' => $reloj->getRef1(),
  40.                     'serie' => $reloj->getSerie(),
  41.                     'modelos' => $reloj->getModelos(),
  42.                     'refs' => $reloj->getRefs(),
  43.                     'aspecto' => $reloj->getAspecto()?->getId(),
  44.                     'fecha' => $reloj->getFecha()?->format('d-m-Y'),
  45.                     'caja' => $reloj->getCaja(),
  46.                     'papeles' => $reloj->getPapeles(),
  47.                     'stock' => $reloj->getStock(),
  48.                     'chrono24' => $reloj->isChrono24(),
  49.                     'comentario' => $reloj->getComentario(),
  50.                     'precioVentaTotal' => $reloj->getPrecioVentaTotal(),
  51.                     'precioCosteTotal' => $reloj->getPrecioCosteTotal(),
  52.                     'precioPromocion' => $reloj->getPrecioPromocion(),
  53.                     'garantia' => $reloj->getGarantia(),
  54.                     'recompra' => $reloj->getRecompra(),
  55.                 ]]),
  56.                 Response::HTTP_OK,
  57.                 ['content-type' => 'text/json']
  58.             );
  59.         }
  60.         catch(InvalidArgumentException $e)
  61.         {
  62.             return new Response(
  63.                 json_encode(['error' => $e->getMessage()]),
  64.                 Response::HTTP_BAD_REQUEST,
  65.                 ['content-type' => 'text/json']
  66.             );
  67.         }
  68.     }
  69.     /**
  70.      * @Route("/api/reloj/precio-promocion", methods={"GET"})
  71.      */
  72.     public function getPrecioPromocionAction(Request $request)
  73.     {
  74.         try
  75.         {
  76.             if(!$request->query->has('id')) throw new InvalidArgumentException('RELOJ_NOT_EXISTS');
  77.             $reloj $this->em->getRepository(Reloj::class)->find($request->query->get('id'));
  78.             if(!$reloj) throw new InvalidArgumentException('RELOJ_NOT_EXISTS');
  79.             $criteria Criteria::create()
  80.                 ->orderBy([
  81.                     'fecha' => Criteria::DESC,
  82.                     'created_at' => Criteria::DESC,
  83.                 ]);
  84.             //if(!$request->query->has('plataforma')) throw new InvalidArgumentException('CANAL_NOT_EXISTS');
  85.             if($request->query->has('plataforma') && $request->query->get('plataforma')) {
  86.                 $plataforma $this->em->getRepository(Canal::class)->find($request->query->get('plataforma'));
  87.                 if (!$plataforma) {
  88.                     throw new InvalidArgumentException('CANAL_NOT_EXISTS');
  89.                 }
  90.                 $anuncio $reloj->getPromociones()->getAcciones()
  91.                     ->filter(fn($accion) => $accion instanceof AccionAnuncio && $accion->getCanal() === $plataforma)
  92.                     ->matching($criteria)
  93.                     ->first();
  94.             }
  95.             else
  96.             {
  97.                 $anuncio $reloj->getPromociones()->getAcciones()
  98.                     ->filter(fn($accion) => $accion instanceof AccionEstado && $accion?->getEstado()?->getKey() === EstadoRelojEnum::ESTADO_ANUNCIO)
  99.                     ->matching($criteria)
  100.                     ->first();
  101.             }
  102.             return new Response(
  103.                 json_encode(['precioAnuncio' => $anuncio $anuncio->getPrecio() : 0.00]),
  104.                 Response::HTTP_OK,
  105.                 ['content-type' => 'text/json']
  106.             );
  107.         }
  108.         catch(InvalidArgumentException $e)
  109.         {
  110.             return new Response(
  111.                 json_encode(['error' => $e->getMessage()]),
  112.                 Response::HTTP_BAD_REQUEST,
  113.                 ['content-type' => 'text/json']
  114.             );
  115.         }
  116.     }
  117. }