Permet de poster des commentaires

main
Clément FRÉVILLE 3 years ago
parent cd6beb5628
commit e241b4d75d

@ -15,7 +15,7 @@ $router = new Router($_SERVER['REQUEST_URI']);
$router->get('/^$/', [$user, 'index']);
$router->get('/^recent\/(?<page>\d+)$/', [$user, 'index']);
$router->get('/^news\/(?<slug>[A-Za-z0-9-]+)-(?<id>\d+)$/', [$user, 'viewPost']);
$router->get('/^comments\/(?<id>[\w-]+)$/', [$user, 'viewPostComments']);
$router->post('/^comment\/(?<id>\d+)$/', [$user, 'comment']);
$router->match('/^login$/', [$security, 'login']);
$router->match('/^register$/', [$security, 'register']);
$router->match('/^logout$/', [$security, 'logout']);

@ -4,8 +4,10 @@ declare(strict_types=1);
namespace Silex\Controller;
use DateTime;
use Silex\DI\DI;
use Silex\Http\HttpResponse;
use Silex\Model\Comment;
use Silex\Util\Pagination;
class UserController
@ -38,13 +40,15 @@ class UserController
if($news->getSlug() !== $params['slug']){
HttpResponse::redirect($di->getRouter()->url($news->getSlugRedirect()));
}
return new HttpResponse(200, 'newsView', ['news' => $news]);
$comments = $di->getCommentGateway()->getByNewsId($newsId);
return new HttpResponse(200, 'newsView', ['news' => $news, 'comments' => $comments]);
}
public function viewPostComments(DI $di, array $params): HttpResponse
public function comment(DI $di, array $params): void
{
$newsId = intval($params['id']);
$comments = $di->getCommentGateway()->getByNewsId($newsId);
return new HttpResponse(200, 'comment', ['comments' => $comments]);
$news = $di->getNewsGateway()->getById($newsId);
$di->getCommentGateway()->insert(new Comment(-1, $newsId, new DateTime(), $_POST['content'], $di->getSecurity()->getCurrentUserId()));
HttpResponse::redirect($di->getRouter()->url($news->getSlugRedirect()));
}
}

@ -17,6 +17,27 @@ class CommentGateway
$this->pdo = $pdo;
}
public function insert(Comment $comment): bool
{
$req = $this->pdo->prepare('INSERT INTO comment (news_id, content, author_id) VALUES (:news_id, :content, :author_id);');
$req->execute(['news_id' => $comment->getNewsId(), 'content' => $comment->getContent(), 'author_id' => $comment->getAuthorId()]);
$comment->setId(intval($this->pdo->lastInsertId()));
return true;
}
public function update(Comment $comment): bool
{
$req = $this->pdo->prepare('UPDATE comment SET news_id = :news_id, content = :content, author_id = :author_id WHERE id_comment = :id_comment;');
$req->execute(['news_id' => $comment->getNewsId(), 'content' => $comment->getContent(), 'author_id' => $comment->getAuthorId(), 'id_comment' => $comment->getId()]);
return true;
}
public function delete(Comment $comment): void
{
$req = $this->pdo->prepare('DELETE FROM comment WHERE id_comment = :id_comment;');
$req->execute(['id_comment' => $comment->getId()]);
}
/**
* @return Comment[]
*/

@ -34,7 +34,7 @@ class NewsGateway
public function delete(News $news): void
{
$req = $this->pdo->prepare('DELETE FROM news WHERE id = :id_news;');
$req = $this->pdo->prepare('DELETE FROM news WHERE id_news = :id_news;');
$req->execute(['id_news' => $news->getId()]);
}

@ -23,7 +23,7 @@ class Comment
$this->authorId = $authorId;
}
public function getCommentId(): int
public function getId(): int
{
return $this->idComment;
}
@ -47,4 +47,9 @@ class Comment
{
return $this->authorId;
}
public function setId(int $id): void
{
$this->idComment = $id;
}
}

@ -38,7 +38,7 @@ class Router
public function post(string $path, callable $callable): self
{
return $this->addRoute(['GET'], $path, $callable);
return $this->addRoute(['POST'], $path, $callable);
}
public function match(string $path, callable $callable): self

@ -11,4 +11,33 @@
<?= $params['news']->getContent() ?>
</div>
</div>
</div>
</div>
<section class="section comments">
<?php foreach ($params['comments'] as $comment) : ?>
<article class="message">
<header class="message-header">
From <?= $comment->getAuthorId() ?> published on <?= $comment->getPublicationDate()->format('Y-m-d H:i:s') ?>
</header>
<div class="message-body">
<?= $comment->getContent() ?>
</div>
</article>
<?php endforeach; ?>
<?php if ($security->getCurrentUserId() !== null) : ?>
<form action="<?= $router->url('comment/' . $params['news']->getId()) ?>" method="post">
<div class="field">
<label class="label" for="content">Comment</label>
<div class="control">
<textarea class="textarea" id="content" name="content"></textarea>
</div>
</div>
<div class="field">
<div class="control">
<button class="button is-link">Submit</button>
</div>
</div>
</form>
<?php endif; ?>
</section>

Loading…
Cancel
Save