|
|
|
@ -16,9 +16,16 @@ 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;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* CRUD on posts and comments.
|
|
|
|
|
*/
|
|
|
|
|
class PostController extends AbstractController
|
|
|
|
|
{
|
|
|
|
|
/**
|
|
|
|
|
* The number of item on a page for the pagination.
|
|
|
|
|
*/
|
|
|
|
|
private const POSTS_PER_PAGE = 10;
|
|
|
|
|
|
|
|
|
|
#[Route('/', name: 'app_posts')]
|
|
|
|
@ -35,7 +42,7 @@ class PostController extends AbstractController
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[Route('/posts', name: 'app_post_index', methods: ['GET'])]
|
|
|
|
|
public function table(PostRepository $repository): Response
|
|
|
|
|
public function table(): Response
|
|
|
|
|
{
|
|
|
|
|
return $this->redirectToRoute('app_posts', [], Response::HTTP_SEE_OTHER);
|
|
|
|
|
}
|
|
|
|
@ -170,4 +177,29 @@ class PostController extends AbstractController
|
|
|
|
|
}
|
|
|
|
|
return $this->redirectToRoute('app_post_show', ['id' => $comment->getRelatedPost()->getId()]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[Route('/post/{id}/like', name: 'app_posts_like', methods: ['POST'])]
|
|
|
|
|
#[IsGranted('ROLE_USER')]
|
|
|
|
|
public function addLike(#[CurrentUser] User $user, Post $post, EntityManagerInterface $entityManager): JsonResponse
|
|
|
|
|
{
|
|
|
|
|
$user->addLikedPost($post);
|
|
|
|
|
$entityManager->flush();
|
|
|
|
|
|
|
|
|
|
$likesCount = $post->getLikes()->count();
|
|
|
|
|
|
|
|
|
|
return new JsonResponse(['success' => true, 'likesCount' => $likesCount]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[Route('/post/{id}/unlike', name: 'app_posts_unlike', methods: ['POST'])]
|
|
|
|
|
#[IsGranted('ROLE_USER')]
|
|
|
|
|
public function deleteLike(#[CurrentUser] User $user, Post $post, EntityManagerInterface $entityManager): JsonResponse
|
|
|
|
|
{
|
|
|
|
|
$user->removeLikedPost($post);
|
|
|
|
|
$entityManager->flush();
|
|
|
|
|
|
|
|
|
|
$likesCount = $post->getLikes()->count();
|
|
|
|
|
|
|
|
|
|
return new JsonResponse(['success' => true, 'likesCount' => $likesCount]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|