diff --git a/public/css/components/comment.css b/public/css/components/comment.css new file mode 100644 index 0000000..de91daf --- /dev/null +++ b/public/css/components/comment.css @@ -0,0 +1,25 @@ +.comment-wrapper { + background-color: #f2f2f7; + border: 1px solid black; + border-radius: 1rem; + padding: 20px; + + .comment-info { + display: flex; + flex-direction: row; + align-items: center; + gap: 10px; + } + + .comment-text { + p { + white-space: pre-wrap; + font-size: 1em; + } + } + + a { + text-decoration: none; + color: black; + } +} \ No newline at end of file diff --git a/public/css/components/post.css b/public/css/components/post.css index 6f67aa8..18c931e 100644 --- a/public/css/components/post.css +++ b/public/css/components/post.css @@ -5,7 +5,16 @@ border: 3px solid black; border-radius: 1rem; padding: 20px; - margin-top: 25vh; + margin-top: 10vh; + + h1 { + font-size: 3em; + } + + p { + white-space: pre-wrap; + font-size: 1.2em; + } } #post-info { @@ -15,21 +24,33 @@ gap: 10px; } -h1 { - font-size: 3em; -} - -p { - white-space: pre-wrap; - font-size: 1.2em; -} - #comments { h2 { font-size: 30px; } + + #comments-wrapper { + padding: 10px; + } } hr { color: black; +} + +#comment { + display: flex; + flex-direction: rows; + align-items: center; + gap: 2rem; + justify-content: space-between; + + :first-child { + width: 100%; + } + + #comment_text { + display: flex; + flex-grow: 1; + } } \ No newline at end of file diff --git a/src/Controller/PostController.php b/src/Controller/PostController.php index 21b90e9..f16d3fd 100644 --- a/src/Controller/PostController.php +++ b/src/Controller/PostController.php @@ -2,12 +2,14 @@ namespace App\Controller; +use App\Entity\Commentary; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\Routing\Attribute\Route; use Doctrine\ORM\EntityManagerInterface; use App\Entity\Post; use App\Form\Type\PostType; +use App\Form\Type\CommentType; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; class PostController extends AbstractController @@ -20,8 +22,7 @@ class PostController extends AbstractController $this->em = $em; } - # DEBUG: Ne doit pas ĂȘtre laissĂ© en production. - #[Route('/', name: 'all post', methods: ['GET'])] + #[Route('/', name: 'all_posts', methods: ['GET'])] public function getAllPost(): Response { $posts = $this->em->getRepository(Post::class)->findAll(); @@ -35,18 +36,35 @@ class PostController extends AbstractController #[Route( '/post/{id}', name: 'display_post', - methods: ['GET'], + methods: ['GET', 'POST'], requirements: ['id' => '\d+'] )] - public function getPost(int $id): Response + public function getPost(int $id, Request $request): Response { $post = $this->em->getRepository(Post::class)->find($id); if (!$post) { } + + $comment = new Commentary(); + $commentForm = $this->createForm(CommentType::class, $comment); + + $commentForm->handleRequest($request); + if ($commentForm->isSubmitted() && $commentForm->isValid()) { + $user = $this->getUser(); + + $comment->setProfil($user); + $comment->setPost($post); + + $this->em->persist($comment); + $this->em->flush(); + + return $this->redirectToRoute('display_post', ['id' => $id]); + } return $this->render('post/post.html.twig', [ - 'post' => $post + 'post' => $post, + 'commentForm' => $commentForm, ]); } @@ -88,4 +106,33 @@ class PostController extends AbstractController } return new Response(); } + + #[Route('/post/{id}/comment', name: 'post_comment', methods: ['POST'])] + public function addComment(int $id, Request $request): Response + { + $this->denyAccessUnlessGranted('IS_AUTHENTICATED'); + + $comment = new Commentary(); + $form = $this->createForm(CommentType::class, $comment); + + $form->handleRequest($request); + if ($form->isSubmitted() && $form->isValid()) { + $post = $this->em->getRepository(Post::class)->find($id); + + $form = $form->getData(); + $user = $this->getUser(); + + $comment->setProfil($user); + $comment->setPost($post); + + $this->em->persist($comment); + $this->em->flush(); + + return $this->redirectToRoute('display_post', ['id' => $id]); + } + + return $this->render('post/new.html.twig', [ + 'form' => $form, + ]); + } } diff --git a/src/Controller/ProfilController.php b/src/Controller/ProfilController.php index 2d1b473..e918269 100644 --- a/src/Controller/ProfilController.php +++ b/src/Controller/ProfilController.php @@ -15,10 +15,8 @@ use Symfony\Component\HttpFoundation\Request; class ProfilController extends AbstractController { - - public function __construct(private EntityManager $mgr, private PostRepository $postRepository) - { - } + public function __construct(private EntityManager $mgr, private PostRepository $postRepository) {} + #[Route(path: "/profil", name: "profil_perso", methods: ["GET"])] public function baseProfil(): Response { @@ -29,6 +27,7 @@ class ProfilController extends AbstractController } return $this->redirectToRoute('profil_show', ['id' => $this->getUser()->getId()]); } + #[Route('/profil/{id}', name: 'profil_show', requirements: ['page' => '\d+'])] public function profil(int $id): Response { @@ -46,7 +45,6 @@ class ProfilController extends AbstractController ]); } - #[Route('/profil/post/follow', name: 'profil_post_follow')] public function postProfilfollow(): Response { @@ -170,6 +168,4 @@ class ProfilController extends AbstractController return $this->redirectToRoute('app_login'); } - - } diff --git a/src/Entity/Commentary.php b/src/Entity/Commentary.php index f54f60c..c5af0c3 100644 --- a/src/Entity/Commentary.php +++ b/src/Entity/Commentary.php @@ -2,12 +2,10 @@ namespace App\Entity; -use ApiPlatform\Metadata\ApiResource; use App\Repository\CommentaryRepository; use Doctrine\ORM\Mapping as ORM; #[ORM\Entity(repositoryClass: CommentaryRepository::class)] -#[ApiResource] class Commentary { #[ORM\Id] diff --git a/src/Form/CommentType.php b/src/Form/CommentType.php new file mode 100644 index 0000000..44ef234 --- /dev/null +++ b/src/Form/CommentType.php @@ -0,0 +1,23 @@ +add('text', TextType::class, [ + 'label' => false + ]) + ->add('submit', SubmitType::class, [ + 'label' => 'Comment' + ]) + ; + } +} diff --git a/templates/comment/comment.html.twig b/templates/comment/comment.html.twig new file mode 100644 index 0000000..864e5b4 --- /dev/null +++ b/templates/comment/comment.html.twig @@ -0,0 +1,12 @@ +
{{ comment.text }}
+