<?php
namespace App\Controller;
use App\Entity\Comment;
use App\Entity\Post;
use App\Services\PostService;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
class BlogController extends AbstractController
{
#[Route('/blog', name: 'app_blog')]
public function showBlog(EntityManagerInterface $entityManager): Response
{
$posts = $entityManager->getRepository(Post::class)->findBy([], ['id' => 'DESC']);
return $this->render('blog/index.html.twig', [
'posts' => $posts,
]);
}
#[Route('/blog/singlePost/{postId}', name: 'app_blog_post_single')]
public function showSinglePost(EntityManagerInterface $entityManager, string $postId)
{
$post = $entityManager->getRepository(Post::class)->find($postId);
$comments = $entityManager->getRepository(Comment::class)->findBy(['post' => $postId]);
return $this->render('blog/single_post.html.twig', [
'post' => $post,
'comments' => $comments
]);
}
#[Route('/blog/addNewPost', name: 'app_blog_post_add')]
public function addNewPost(EntityManagerInterface $entityManager, Request $request)
{
$postService = new PostService();
$postService->addNewPost($entityManager, $request);
return $this->render('blog/add_post.html.twig');
}
#[Route('/blog/addComment', name: 'app_blog_comment_add')]
public function addComment(EntityManagerInterface $entityManager, Request $request)
{
$postService = new PostService();
$postService->addComment($entityManager, $request);
return new Response('', Response::HTTP_OK);
}
}