diff --git a/src/Controller/PostController.php b/src/Controller/PostController.php index 7f4e77f..0020574 100644 --- a/src/Controller/PostController.php +++ b/src/Controller/PostController.php @@ -170,4 +170,24 @@ class PostController extends AbstractController } return $this->redirectToRoute('app_post_show', ['id' => $comment->getRelatedPost()->getId()]); } + + #[Route('/post/{id}/like', name: 'app_posts', methods: ['POST'])] + #[IsGranted('ROLE_USER')] + public function addLike(Request $request, 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', methods: ['POST'])] + #[IsGranted('ROLE_USER')] + public function deleteLike(Request $request, 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); + + } + } diff --git a/templates/post/index.html.twig b/templates/post/index.html.twig index d14adbc..2b404bd 100644 --- a/templates/post/index.html.twig +++ b/templates/post/index.html.twig @@ -12,7 +12,7 @@

{{ post.commentary }}