diff --git a/src/Controller/CommentController.php b/src/Controller/CommentController.php index cbcb38a..298130e 100644 --- a/src/Controller/CommentController.php +++ b/src/Controller/CommentController.php @@ -20,6 +20,40 @@ class CommentController extends AbstractController ]); } + /** + * @Route("/admin/comments/valid/{id}", name="comment-valid") + * @param int $id + * @return Response + */ + public function valid(int $id): Response + { + $manager = $this->getDoctrine()->getManager(); + $comm = $manager->getRepository(Comment::class)->find($id); + if (!$comm) { + throw $this->createNotFoundException("Comment not found"); + } + $comm->setValid(true); + $manager->flush(); + return $this->redirectToRoute('comments'); + } + + /** + * @Route("/admin/comments/remove/{id}", name="comment-remove") + * @param int $id + * @return Response + */ + public function remove(int $id): Response + { + $manager = $this->getDoctrine()->getManager(); + $comm = $manager->getRepository(Comment::class)->find($id); + if (!$comm) { + throw $this->createNotFoundException("Comment not found"); + } + $manager->remove($comm); + $manager->flush(); + return $this->redirectToRoute('comments'); + } + public function recentComment(): Response { return $this->render('comment/recent_comment.html.twig', [ diff --git a/src/Controller/PostController.php b/src/Controller/PostController.php index 92a8c04..abff63e 100644 --- a/src/Controller/PostController.php +++ b/src/Controller/PostController.php @@ -30,7 +30,6 @@ class PostController extends AbstractController if ($form->isSubmitted() && $form->isValid()) { $comment = $form->getData(); $comment->setCreatedAt(new \DateTime()); - $comment->setValid(true); $manager = $this->getDoctrine()->getManager(); $manager->persist($comment); $manager->flush(); diff --git a/src/Entity/Comment.php b/src/Entity/Comment.php index 1295da9..63eb3cb 100644 --- a/src/Entity/Comment.php +++ b/src/Entity/Comment.php @@ -43,6 +43,11 @@ class Comment */ private $post; + public function __construct() + { + $this->valid = false; + } + public function getId(): ?int { return $this->id; diff --git a/templates/comment/comments.html.twig b/templates/comment/comments.html.twig index 2b9eca2..96aabc6 100644 --- a/templates/comment/comments.html.twig +++ b/templates/comment/comments.html.twig @@ -1,7 +1,21 @@ {% extends 'admin/base-admin.html.twig' %} -{% block title %}Hello AdminController!{% endblock %} +{% block title %}Administration - Comments{% endblock %} {% block body %} - +
+ {% for comment in comments %} +
+
+
{{ comment.Post.title }}: {{ comment.username }}
+

{{ comment.content }}

+ Delete + {% if comment.valid == false %} + Validate + {% endif %} +
+ +
+ {% endfor %} +
{% endblock %} diff --git a/templates/post/index.html.twig b/templates/post/index.html.twig index 2bb62dc..aba3793 100644 --- a/templates/post/index.html.twig +++ b/templates/post/index.html.twig @@ -13,7 +13,7 @@

Comments

- {% for comment in post.comments %} + {% for comment in post.comments|filter(c => c.valid) %}
{{ comment.username }}