diff --git a/public/index.php b/public/index.php index 291addd..ab2914a 100644 --- a/public/index.php +++ b/public/index.php @@ -11,7 +11,9 @@ $loader->register(); $security = new \Silex\Controller\SecurityController(); $user = new \Silex\Controller\UserController(); $router = new Router($_SERVER['REQUEST_URI']); +$router->setBasePath("~cofrizot/silex/index.php"); $router->get('/^$/', [$user, 'index']); $router->get('/^news\/(?[\w-]+)$/', [$user, 'viewPost']); +$router->get('/^comments\/(?[\w-]+)$/', [$user, 'viewPostComments']); $router->match('/^login$/', [$security, 'login']); $router->run(new \Silex\DI\DI($router))->render(__DIR__ . '/../' . VIEW_PATH); diff --git a/src/Silex/Controller/UserController.php b/src/Silex/Controller/UserController.php index 5e915b0..cddb4c6 100644 --- a/src/Silex/Controller/UserController.php +++ b/src/Silex/Controller/UserController.php @@ -19,6 +19,13 @@ class UserController { $newsId = intval($params['id']); $news = $di->getNewsGateway()->getById($newsId); - return new HttpResponse(200, 'home', ['news' => $news]); + return new HttpResponse(200, 'newsView', ['news' => $news]); + } + + public function viewPostComments(DI $di, array $params): HttpResponse + { + $newsId = intval($params['id']); + $comments = $di->getCommentGateway()->getByNewsId($newsId); + return new HttpResponse(200, 'comment', ['comments' => $comments]); } } diff --git a/src/Silex/DI/DI.php b/src/Silex/DI/DI.php index 2de5880..abb1a9b 100644 --- a/src/Silex/DI/DI.php +++ b/src/Silex/DI/DI.php @@ -5,6 +5,7 @@ declare(strict_types=1); namespace Silex\DI; use Silex\Gateway\NewsGateway; +use Silex\Gateway\CommentGateway; use PDO; use Silex\Gateway\UserGateway; use Silex\Router\Router; @@ -15,6 +16,7 @@ class DI private Router $router; private ?PDO $pdo = null; private ?NewsGateway $newsGateway = null; + private ?CommentGateway $commentGateway = null; private ?UserGateway $userGateway = null; private ?Security $security = null; @@ -53,6 +55,14 @@ class DI return $this->security; } + public function getCommentGateway(): CommentGateway + { + if ($this->commentGateway === null) { + $this->commentGateway = new CommentGateway($this->getPDO()); + } + return $this->commentGateway; + } + private function getPDO(): PDO { if ($this->pdo === null) { diff --git a/src/Silex/Gateway/CommentGateway.php b/src/Silex/Gateway/CommentGateway.php new file mode 100644 index 0000000..87ee152 --- /dev/null +++ b/src/Silex/Gateway/CommentGateway.php @@ -0,0 +1,41 @@ +pdo = $pdo; + } + + /** + * @return Comment[] + */ + public function getByNewsId(int $id): array + { + $req = $this->pdo->prepare('SELECT * FROM comment WHERE news_id = :id ORDER BY publication_date DESC'); + $req->bindValue(':id', $id, PDO::PARAM_INT); + if (!$req->execute()) { + return []; + } + $comments = []; + while ($data = $req->fetch()) { + $comments[] = $this->createComment($data); + } + return $comments; + } + + private function createComment(array $data): Comment + { + return new Comment(intval($data['id_comment']),intval($data['news_id']), DateTime::createFromFormat('Y-m-d H:i:s', $data['publication_date']), $data['content'], intval($data['author_id'])); + } +} \ No newline at end of file diff --git a/src/Silex/Gateway/NewsGateway.php b/src/Silex/Gateway/NewsGateway.php index b7329a7..4986614 100644 --- a/src/Silex/Gateway/NewsGateway.php +++ b/src/Silex/Gateway/NewsGateway.php @@ -49,6 +49,7 @@ class NewsGateway return $news; } + private function createNews(array $data): News { return new News($data['title'], $data['content'], DateTime::createFromFormat('Y-m-d H:i:s', $data['publication_date'])); diff --git a/src/Silex/Model/Comment.php b/src/Silex/Model/Comment.php new file mode 100644 index 0000000..588aaf6 --- /dev/null +++ b/src/Silex/Model/Comment.php @@ -0,0 +1,50 @@ +idComment = $idComment; + $this->newsId = $newsId; + $this->publicationDate = $publicationDate; + $this->content = $content; + $this->authorId = $authorId; + } + + public function getCommentId(): int + { + return $this->idComment; + } + + public function getNewsId(): int + { + return $this->newsId; + } + + public function getPublicationDate(): DateTime + { + return $this->publicationDate; + } + + public function getContent(): string + { + return $this->content; + } + + public function getAuthorId(): int + { + return $this->authorId; + } +} \ No newline at end of file diff --git a/views/comment.php b/views/comment.php new file mode 100644 index 0000000..5c7ff7a --- /dev/null +++ b/views/comment.php @@ -0,0 +1,19 @@ + +

Comments

+ +
+
+

+ getCommentId() ?> +

+
+
+
+ getAuthorId() . " published on " . $comment->getPublicationDate()->format('Y-m-d H:i:s') ?> +
+
+ getContent() ?> +
+
+
+ \ No newline at end of file diff --git a/views/newsView.php b/views/newsView.php new file mode 100644 index 0000000..3716e3b --- /dev/null +++ b/views/newsView.php @@ -0,0 +1,14 @@ + +

News

+
+
+

+ getTitle() ?> +

+
+
+
+ getContent() ?>... +
+
+
\ No newline at end of file