Ajoute les 2 compteurs de messages
This commit is contained in:
@@ -15,12 +15,20 @@ class UserController
|
||||
public function index(DI $di, array $params): HttpResponse
|
||||
{
|
||||
$gw = $di->getNewsGateway();
|
||||
$gwc = $di->getCommentGateway();
|
||||
$user = $di->getSecurity()->getCurrentUser();
|
||||
|
||||
$page = intval($params['page'] ?? 1);
|
||||
$total = $gw->getCount();
|
||||
$nbPages = Pagination::getNbPages($total, 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
|
||||
|
@@ -34,6 +34,25 @@ class CommentGateway
|
||||
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
|
||||
{
|
||||
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'; ?>
|
||||
<div>
|
||||
<p>
|
||||
<?= "Nombre de messages total : " . $params['nbComments'] ?> <br>
|
||||
<?= "Nombre de messages de l'utilisateur : " . $params['nbCommentsByUser'] ?>
|
||||
</p>
|
||||
</div>
|
||||
<h1>Hello world!</h1>
|
||||
<?php foreach ($params['news'] as $news) : ?>
|
||||
<div class="card">
|
||||
|
Reference in New Issue
Block a user