<?php
namespace App\Controller;
use App\Entity\TrCoeur;
use App\Form\TrCoeurType;
use App\Repository\TrCoeurRepository;
use App\Entity\TrProduits;
use App\Form\TrProduitsType;
use App\Repository\TrProduitsRepository;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\JsonResponse;
use App\Service\Cart\CartService;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\Request;
use symfony\component\Form\FormBuilderInterface;
use Symfony\Component\String\Slugger\SluggerInterface;
use Symfony\Component\DependencyInjection\Loader\Configurator\form;
use Symfony\Component\HttpFoundation\File\Exception\FileException;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
class TrCoeurController extends AbstractController
{
/**
* @Route("/tr/coeur/initial", name="app_tr_coeur")
*/
public function index(): Response
{
return $this->render('tr_coeur/index.html.twig', [
'controller_name' => 'TrCoeurController',
]);
}
/**
* @Route("/tr/coeur/{id}/coeur", name="trcoeur_add")
*/
public function form($id, request $request, EntityManagerInterface $manager)
{
/*if (!$Coeur) {
$Coeur = new TrCoeur();
}
$form = $this->createForm(TrCoeurType::class, $Coeur);
$form->handleRequest($request);*/
$Coeur = new TrCoeur();
$repository = $manager->getRepository(TrProduits::class);
$Produits = $repository->findBy(['id' => $id]);
$DATEDUJOUR = new \DateTime();
$Coeur->setPOINTAGE(true);
$Coeur->setDATEPOINTAGE($DATEDUJOUR);
$Coeur->setPRODUIT($Produits[0]);
$manager->persist($Coeur);
$manager->flush();
return $this->redirectToRoute('Bussness');
}
/**
* @Route("/api/alali/v1/like", name="api_like_product", methods={"POST"})
*/
public function like(Request $request, EntityManagerInterface $em): JsonResponse {
$data = json_decode($request->getContent(), true);
if (!isset($data['productId'])) {
return new JsonResponse([
'error' => 'productId manquant',
], 400);
}
$productId = (int) $data['productId'];
/** @var TrProduits|null $product */
$product = $em->getRepository(TrProduits::class)->find($productId);
if (!$product) {
return new JsonResponse([
'error' => 'Produit introuvable',
], 404);
}
$heart = new TrCoeur();
$heart->setPOINTAGE(true);
$heart->setPRODUIT($product);
// ICI on met la date directement côté backend
// adapte le nom du setter à ton entité (setDATEPOINTAGE / setDATE_POINTAGE / etc.)
$heart->setDATEPOINTAGE(new \DateTimeImmutable());
$em->persist($heart);
$em->flush();
return new JsonResponse([
'id' => $heart->getId(),
'POINTAGE' => $heart->isPOINTAGE(),
'DATE_POINTAGE' => $heart->getDATEPOINTAGE() ? $heart->getDATEPOINTAGE()->format('Y-m-d H:i:s') : null,
'PRODUIT' => $productId,
], 201);
}
/**
* @Route("/api/alali/v1/coeurs_stats_mobile", name="api_coeurs_stats_mobile", methods={"GET"})
*/
public function coeursStatsMobile(EntityManagerInterface $em): JsonResponse
{
// ⚠️ adapte App\Entity\TrCoeur au vrai nom de ton entité
$total = (int) $em->createQuery(
'SELECT COUNT(c.id) FROM App\Entity\TrCoeur c'
)->getSingleScalarResult();
return new JsonResponse([
'success' => true,
'total' => $total,
]);
}
}