Comments administration
This commit is contained in:
parent
71ffcefab9
commit
67a4f95882
5 changed files with 56 additions and 4 deletions
|
@ -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', [
|
||||
|
|
|
@ -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();
|
||||
|
|
|
@ -43,6 +43,11 @@ class Comment
|
|||
*/
|
||||
private $post;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->valid = false;
|
||||
}
|
||||
|
||||
public function getId(): ?int
|
||||
{
|
||||
return $this->id;
|
||||
|
|
|
@ -1,7 +1,21 @@
|
|||
{% extends 'admin/base-admin.html.twig' %}
|
||||
|
||||
{% block title %}Hello AdminController!{% endblock %}
|
||||
{% block title %}Administration - Comments{% endblock %}
|
||||
|
||||
{% block body %}
|
||||
|
||||
<div class="container-fluid">
|
||||
{% for comment in comments %}
|
||||
<div class="card text-center">
|
||||
<div class="card-body">
|
||||
<h5 class="card-title">{{ comment.Post.title }}: {{ comment.username }}</h5>
|
||||
<p class="card-text">{{ comment.content }}</p>
|
||||
<a href="{{ path('comment-remove', {id: comment.id}) }}" class="btn btn-danger">Delete</a>
|
||||
{% if comment.valid == false %}
|
||||
<a href="{{ path('comment-valid', {id: comment.id}) }}" class="btn btn-primary">Validate</a>
|
||||
{% endif %}
|
||||
</div>
|
||||
<div class="card-footer text-muted">{{ comment.createdAt.format('Y-m-d H:i:s') }}</div>
|
||||
</div>
|
||||
{% endfor %}
|
||||
</div>
|
||||
{% endblock %}
|
||||
|
|
|
@ -13,7 +13,7 @@
|
|||
</div>
|
||||
<div class="container-fluid">
|
||||
<h3>Comments</h3>
|
||||
{% for comment in post.comments %}
|
||||
{% for comment in post.comments|filter(c => c.valid) %}
|
||||
<div class="card">
|
||||
<div class="card-body">
|
||||
<h5 class="card-title">{{ comment.username }}</h5>
|
||||
|
|
Reference in a new issue