fonctionnalité like and dislike of a post

This commit is contained in:
Matis MAZINGUE
2024-06-13 11:54:28 +02:00
parent 8d1f0b9a6f
commit c8617388c7
5 changed files with 94 additions and 7 deletions

View File

@@ -16,6 +16,7 @@ use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\Security\Http\Attribute\CurrentUser;
use Symfony\Component\Security\Http\Attribute\IsGranted;
use Symfony\UX\Turbo\TurboBundle;
use Symfony\Component\HttpFoundation\JsonResponse;
class PostController extends AbstractController
{
@@ -171,23 +172,45 @@ class PostController extends AbstractController
return $this->redirectToRoute('app_post_show', ['id' => $comment->getRelatedPost()->getId()]);
}
#[Route('/post/{id}/like', name: 'app_posts', methods: ['POST'])]
// #[Route('/post/{id}/like', name: 'app_posts_like', methods: ['POST'])]
// #[IsGranted('ROLE_USER')]
// public function addLike(Post $post, EntityManagerInterface $entityManager, #[CurrentUser] User $user): Response
// {
// $user->addLikedPost($post);
// $entityManager->flush();
// return $this->redirectToRoute('app_post_show', ['id' => $post->getId()], Response::HTTP_SEE_OTHER);
// }
// #[Route('/post/{id}/unlike', name: 'app_posts_unlike', methods: ['POST'])]
// #[IsGranted('ROLE_USER')]
// public function deleteLike(Post $post, EntityManagerInterface $entityManager, #[CurrentUser] User $user): Response
// {
// $user->removeLikedPost($post);
// $entityManager->flush();
// return $this->redirectToRoute('app_post_show', ['id' => $post->getId()], Response::HTTP_SEE_OTHER);
// }
#[Route('/post/{id}/like', name: 'app_posts_like', methods: ['POST'])]
#[IsGranted('ROLE_USER')]
public function addLike(Request $request, Post $post, EntityManagerInterface $entityManager, #[CurrentUser] User $user): Response
public function addLike(#[CurrentUser] User $user, Post $post, EntityManagerInterface $entityManager): JsonResponse
{
$user->addLikedPost($post);
$entityManager->flush();
return $this->redirectToRoute('app_post_show', ['id' => $post->getId()], Response::HTTP_SEE_OTHER);
$likesCount = $post->getLikes()->count();
return new JsonResponse(['success' => true, 'likesCount' => $likesCount]);
}
#[Route('/post/{id}/unlike', name: 'app_posts', methods: ['POST'])]
#[Route('/post/{id}/unlike', name: 'app_posts_unlike', methods: ['POST'])]
#[IsGranted('ROLE_USER')]
public function deleteLike(Request $request, Post $post, EntityManagerInterface $entityManager, #[CurrentUser] User $user): Response
public function deleteLike(#[CurrentUser] User $user, Post $post, EntityManagerInterface $entityManager): JsonResponse
{
$user->removeLikedPost($post);
$entityManager->flush();
return $this->redirectToRoute('app_post_show', ['id' => $post->getId()], Response::HTTP_SEE_OTHER);
$likesCount = $post->getLikes()->count();
return new JsonResponse(['success' => true, 'likesCount' => $likesCount]);
}
}