src/Controller/TrCoeurController.php line 53

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\TrCoeur;
  4. use App\Form\TrCoeurType;
  5. use App\Repository\TrCoeurRepository;
  6. use App\Entity\TrProduits;
  7. use App\Form\TrProduitsType;
  8. use App\Repository\TrProduitsRepository;
  9. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  10. use Symfony\Component\HttpFoundation\Response;
  11. use Symfony\Component\Routing\Annotation\Route;
  12. use Symfony\Component\HttpFoundation\JsonResponse;
  13. use App\Service\Cart\CartService;
  14. use Doctrine\ORM\EntityManagerInterface;
  15. use Symfony\Component\HttpFoundation\Request;
  16. use symfony\component\Form\FormBuilderInterface;
  17. use Symfony\Component\String\Slugger\SluggerInterface;
  18. use Symfony\Component\DependencyInjection\Loader\Configurator\form;
  19. use Symfony\Component\HttpFoundation\File\Exception\FileException;
  20. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  21. class TrCoeurController extends AbstractController
  22. {
  23.     /**
  24.      * @Route("/tr/coeur/initial", name="app_tr_coeur")
  25.      */
  26.     public function index(): Response
  27.     {
  28.         return $this->render('tr_coeur/index.html.twig', [
  29.             'controller_name' => 'TrCoeurController',
  30.         ]);
  31.     }
  32.     /**
  33.      * @Route("/tr/coeur/{id}/coeur", name="trcoeur_add")
  34.      */
  35.     public function form($id, request $request, EntityManagerInterface $manager)
  36.     {
  37.         
  38.         /*if (!$Coeur) {
  39.             $Coeur = new TrCoeur();
  40.         }
  41.         
  42.         $form = $this->createForm(TrCoeurType::class, $Coeur);
  43.         
  44.         $form->handleRequest($request);*/
  45.         $Coeur = new TrCoeur();
  46.         $repository = $manager->getRepository(TrProduits::class);
  47.         $Produits = $repository->findBy(['id' => $id]);
  48.         $DATEDUJOUR = new \DateTime();
  49.         $Coeur->setPOINTAGE(true);
  50.         $Coeur->setDATEPOINTAGE($DATEDUJOUR);
  51.         $Coeur->setPRODUIT($Produits[0]);
  52.         $manager->persist($Coeur);
  53.         $manager->flush();
  54.         return $this->redirectToRoute('Bussness');
  55.         
  56.     }
  57.     /**
  58.      * @Route("/api/alali/v1/like", name="api_like_product", methods={"POST"})
  59.      */
  60.     public function like(Request $request, EntityManagerInterface $em): JsonResponse {
  61.         $data = json_decode($request->getContent(), true);
  62.         if (!isset($data['productId'])) {
  63.             return new JsonResponse([
  64.                 'error' => 'productId manquant',
  65.             ], 400);
  66.         }
  67.         $productId = (int) $data['productId'];
  68.         /** @var TrProduits|null $product */
  69.         $product = $em->getRepository(TrProduits::class)->find($productId);
  70.         if (!$product) {
  71.             return new JsonResponse([
  72.                 'error' => 'Produit introuvable',
  73.             ], 404);
  74.         }
  75.         $heart = new TrCoeur();
  76.         $heart->setPOINTAGE(true);
  77.         $heart->setPRODUIT($product);
  78.         // ICI on met la date directement côté backend
  79.         // adapte le nom du setter à ton entité (setDATEPOINTAGE / setDATE_POINTAGE / etc.)
  80.         $heart->setDATEPOINTAGE(new \DateTimeImmutable());
  81.         $em->persist($heart);
  82.         $em->flush();
  83.         return new JsonResponse([
  84.             'id'            => $heart->getId(),
  85.             'POINTAGE'      => $heart->isPOINTAGE(),
  86.             'DATE_POINTAGE' => $heart->getDATEPOINTAGE() ? $heart->getDATEPOINTAGE()->format('Y-m-d H:i:s') : null,
  87.             'PRODUIT'       => $productId,
  88.         ], 201);
  89.     }
  90.     /**
  91.      * @Route("/api/alali/v1/coeurs_stats_mobile", name="api_coeurs_stats_mobile", methods={"GET"})
  92.      */
  93.     public function coeursStatsMobile(EntityManagerInterface $em): JsonResponse
  94.     {
  95.         // ⚠️ adapte App\Entity\TrCoeur au vrai nom de ton entité
  96.         $total = (int) $em->createQuery(
  97.             'SELECT COUNT(c.id) FROM App\Entity\TrCoeur c'
  98.         )->getSingleScalarResult();
  99.         return new JsonResponse([
  100.             'success' => true,
  101.             'total'   => $total,
  102.         ]);
  103.     }
  104. }