Ajoute les 2 compteurs de messages

main
Colin FRIZOT 2 years ago
parent 2c8e49e560
commit f8959acae7

@ -15,12 +15,20 @@ class UserController
public function index(DI $di, array $params): HttpResponse public function index(DI $di, array $params): HttpResponse
{ {
$gw = $di->getNewsGateway(); $gw = $di->getNewsGateway();
$gwc = $di->getCommentGateway();
$user = $di->getSecurity()->getCurrentUser();
$page = intval($params['page'] ?? 1); $page = intval($params['page'] ?? 1);
$total = $gw->getCount(); $total = $gw->getCount();
$nbPages = Pagination::getNbPages($total, self::PER_PAGE); $nbPages = Pagination::getNbPages($total, self::PER_PAGE);
$news = $gw->getPaginatedRecentNews($page , self::PER_PAGE); $news = $gw->getPaginatedRecentNews($page , self::PER_PAGE);
return new HttpResponse(200, 'home', ['news' => $news, 'page' => $page, 'nbPages' => $nbPages, 'router' => $di->getRouter()]); $nbComments = $gwc->getCommentNumber();
if($user !== null){
$nbCommentsByUser = $gwc->getCommentNumberFromUser($user->getId());
} else {
$nbCommentsByUser = 0;
}
return new HttpResponse(200, 'home', ['news' => $news, 'page' => $page, 'nbPages' => $nbPages, 'router' => $di->getRouter(), 'nbComments' => $nbComments, 'nbCommentsByUser' => $nbCommentsByUser]);
} }
public function viewPost(DI $di, array $params): HttpResponse public function viewPost(DI $di, array $params): HttpResponse

@ -34,6 +34,25 @@ class CommentGateway
return $comments; return $comments;
} }
public function getCommentNumber(): int
{
$req = $this->pdo->prepare('SELECT COUNT(*) FROM comment');
$req->execute();
$data = $req->fetch();
$nbComment = intval($data[0]);
return $nbComment;
}
public function getCommentNumberFromUser(int $id): int
{
$req = $this->pdo->prepare('SELECT COUNT(*) FROM comment WHERE author_id = :id');
$req->bindValue(':id', $id, PDO::PARAM_INT);
$req->execute();
$data = $req->fetch();
$nbComment = intval($data[0]);
return $nbComment;
}
private function createComment(array $data): Comment 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'])); 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']));

@ -1,4 +1,10 @@
<?php $params['title'] = 'Home'; ?> <?php $params['title'] = 'Home'; ?>
<div>
<p>
<?= "Nombre de messages total : " . $params['nbComments'] ?> <br>
<?= "Nombre de messages de l'utilisateur : " . $params['nbCommentsByUser'] ?>
</p>
</div>
<h1>Hello world!</h1> <h1>Hello world!</h1>
<?php foreach ($params['news'] as $news) : ?> <?php foreach ($params['news'] as $news) : ?>
<div class="card"> <div class="card">

Loading…
Cancel
Save