From e241b4d75df0753ba412cf7b8fcd8c08f5a9b003 Mon Sep 17 00:00:00 2001 From: clfreville2 Date: Tue, 6 Dec 2022 09:38:13 +0100 Subject: [PATCH] Permet de poster des commentaires --- public/index.php | 2 +- src/Silex/Controller/UserController.php | 12 ++++++---- src/Silex/Gateway/CommentGateway.php | 21 +++++++++++++++++ src/Silex/Gateway/NewsGateway.php | 2 +- src/Silex/Model/Comment.php | 7 +++++- src/Silex/Router/Router.php | 2 +- views/newsView.php | 31 ++++++++++++++++++++++++- 7 files changed, 68 insertions(+), 9 deletions(-) diff --git a/public/index.php b/public/index.php index 205d17f..358e189 100644 --- a/public/index.php +++ b/public/index.php @@ -15,7 +15,7 @@ $router = new Router($_SERVER['REQUEST_URI']); $router->get('/^$/', [$user, 'index']); $router->get('/^recent\/(?\d+)$/', [$user, 'index']); $router->get('/^news\/(?[A-Za-z0-9-]+)-(?\d+)$/', [$user, 'viewPost']); -$router->get('/^comments\/(?[\w-]+)$/', [$user, 'viewPostComments']); +$router->post('/^comment\/(?\d+)$/', [$user, 'comment']); $router->match('/^login$/', [$security, 'login']); $router->match('/^register$/', [$security, 'register']); $router->match('/^logout$/', [$security, 'logout']); diff --git a/src/Silex/Controller/UserController.php b/src/Silex/Controller/UserController.php index f86f9bc..75f03ca 100644 --- a/src/Silex/Controller/UserController.php +++ b/src/Silex/Controller/UserController.php @@ -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())); } } diff --git a/src/Silex/Gateway/CommentGateway.php b/src/Silex/Gateway/CommentGateway.php index 2cc4f23..2a2a2de 100644 --- a/src/Silex/Gateway/CommentGateway.php +++ b/src/Silex/Gateway/CommentGateway.php @@ -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[] */ diff --git a/src/Silex/Gateway/NewsGateway.php b/src/Silex/Gateway/NewsGateway.php index a2b3f37..c7d28bf 100644 --- a/src/Silex/Gateway/NewsGateway.php +++ b/src/Silex/Gateway/NewsGateway.php @@ -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()]); } diff --git a/src/Silex/Model/Comment.php b/src/Silex/Model/Comment.php index 588aaf6..f5ef1ff 100644 --- a/src/Silex/Model/Comment.php +++ b/src/Silex/Model/Comment.php @@ -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; + } } \ No newline at end of file diff --git a/src/Silex/Router/Router.php b/src/Silex/Router/Router.php index 23bc022..282bcf6 100644 --- a/src/Silex/Router/Router.php +++ b/src/Silex/Router/Router.php @@ -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 diff --git a/views/newsView.php b/views/newsView.php index 712f8cd..c644527 100644 --- a/views/newsView.php +++ b/views/newsView.php @@ -11,4 +11,33 @@ getContent() ?> - \ No newline at end of file + + +
+ +
+
+ From getAuthorId() ?> published on getPublicationDate()->format('Y-m-d H:i:s') ?> +
+
+ getContent() ?> +
+
+ + getCurrentUserId() !== null) : ?> +
+
+ +
+ +
+
+ +
+
+ +
+
+
+ +