Ajoute la gateway des commentaires et corrige les vues
This commit is contained in:
@@ -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\/(?<id>[\w-]+)$/', [$user, 'viewPost']);
|
||||
$router->get('/^comments\/(?<id>[\w-]+)$/', [$user, 'viewPostComments']);
|
||||
$router->match('/^login$/', [$security, 'login']);
|
||||
$router->run(new \Silex\DI\DI($router))->render(__DIR__ . '/../' . VIEW_PATH);
|
||||
|
@@ -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]);
|
||||
}
|
||||
}
|
||||
|
@@ -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) {
|
||||
|
41
src/Silex/Gateway/CommentGateway.php
Normal file
41
src/Silex/Gateway/CommentGateway.php
Normal file
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Silex\Gateway;
|
||||
|
||||
use DateTime;
|
||||
use PDO;
|
||||
use Silex\Model\Comment;
|
||||
|
||||
class CommentGateway
|
||||
{
|
||||
private PDO $pdo;
|
||||
|
||||
public function __construct(PDO $pdo)
|
||||
{
|
||||
$this->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']));
|
||||
}
|
||||
}
|
@@ -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']));
|
||||
|
50
src/Silex/Model/Comment.php
Normal file
50
src/Silex/Model/Comment.php
Normal file
@@ -0,0 +1,50 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Silex\Model;
|
||||
|
||||
use DateTime;
|
||||
|
||||
class Comment
|
||||
{
|
||||
private int $idComment;
|
||||
private int $newsId;
|
||||
private DateTime $publicationDate;
|
||||
private string $content;
|
||||
private int $authorId;
|
||||
|
||||
public function __construct(int $idComment, int $newsId, DateTime $publicationDate, string $content, int $authorId)
|
||||
{
|
||||
$this->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;
|
||||
}
|
||||
}
|
19
views/comment.php
Normal file
19
views/comment.php
Normal file
@@ -0,0 +1,19 @@
|
||||
<?php $params['title'] = 'Comment'; ?>
|
||||
<h1>Comments</h1>
|
||||
<?php foreach ($params['comments'] as $comment) : ?>
|
||||
<div class="card">
|
||||
<header class="card-header">
|
||||
<p class="card-header-title">
|
||||
<?= $comment->getCommentId() ?>
|
||||
</p>
|
||||
</header>
|
||||
<div class="card-content">
|
||||
<div class="content">
|
||||
<?= "AuthorId :" . $comment->getAuthorId() . " published on " . $comment->getPublicationDate()->format('Y-m-d H:i:s') ?>
|
||||
</div>
|
||||
<div class="content">
|
||||
<?= $comment->getContent() ?>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<?php endforeach ?>
|
14
views/newsView.php
Normal file
14
views/newsView.php
Normal file
@@ -0,0 +1,14 @@
|
||||
<?php $params['title'] = 'NewsView'; ?>
|
||||
<h1>News</h1>
|
||||
<div class="card">
|
||||
<header class="card-header">
|
||||
<p class="card-header-title">
|
||||
<?= $params['news']->getTitle() ?>
|
||||
</p>
|
||||
</header>
|
||||
<div class="card-content">
|
||||
<div class="content">
|
||||
<?= $params['news']->getContent() ?>...
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
Reference in New Issue
Block a user