<?php
namespace App\Controller;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\JsonResponse;
use App\Entity\PostLike;
use App\Repository\PostLikeRepository;
use App\Entity\Annonces;
use App\Repository\AnnoncesRepository;
use App\Entity\User;
use Doctrine\Persistence\ManagerRegistry;
use App\Controller\Fonctions\Fonctions;
/**
* Class PostController
* @package App\Controller
*/
class PostController extends AbstractController
{
/**
* @Route(path="/post/{id}/like", name="post_like", methods={"GET"})
* @param Annonces $annonce
* @param PostLikeRepository $repository
* @return \Symfony\Component\HttpFoundation\JsonResponse
*/
public function show(Request $request, ManagerRegistry $doctrine, AnnoncesRepository $annoncesRepository, $id): Response
{
// fonction pour envoyer l'id du journal ou de l'utilisateur
$user = $this->getUser();
$fonction = new Fonctions();
$idposte = 0;
$message = '';
$utilisateur = '';
if ($user == true) {
$utilisateur = $user->getId();
// mettre l'id de l'annonce
$entityManager = $doctrine->getManager();
$userRepo = $entityManager->getRepository(Annonces::class);
$annonce = $userRepo->find($id);
$userRepo = $entityManager->getRepository(User::class);
$userid = $userRepo->find($user);
// mettre l'id de l'annonce
$userRepo = $entityManager->getRepository(PostLike::class);
$idposte = $userRepo->findOneBy(["annonce" => $id, "user" => $utilisateur]);
$teste = 1;
$date = 0;
$date_terminee = 0;
if ($idposte == true) {
$date = $idposte->getCreatedAt("d/m/Y H:i:s");
$date = $date->format('Y-m-d H:i:s') ;
$date_terminee = new \DateTime();
$date_terminee = $date_terminee->format('Y-m-d H:i:s') ;
$date=date('Y-m-d H:i:s', strtotime('+1 minute', strtotime($date)));
if ($date > $date_terminee) {
$code = 201;
$message = 'Like trop top pour supprimer';
} else {
$teste = 2;
$idposte = $idposte->getId();
$user = $userRepo->find($idposte);
$entityManager->remove($user);
$entityManager->flush($user);
$code = 201;
$message = 'Like supprimer';
}
} else {
$like = new PostLike();
$like->setAnnonce($annonce);
$like->setUser($userid);
$entityManager->persist($like);
$entityManager->flush();
$code = 200;
$message = 'Like bien ajouté';
}
}
return new JsonResponse([
'code' => $code,
'message' => $message,
'teste' => $teste,
'date1' => $date,
'date2' => $date_terminee,
'id' => $utilisateur,
'idpost' => $idposte,
]);
}
}