Ajoute le News Controller
This commit is contained in:
@@ -14,4 +14,11 @@ class UserController
|
||||
$news = $di->getNewsGateway()->getPaginatedRecentNews();
|
||||
return new HttpResponse(200, 'home', ['news' => $news]);
|
||||
}
|
||||
|
||||
public function viewPost(DI $di, array $params): HttpResponse
|
||||
{
|
||||
$newsId = intval($params['id']);
|
||||
$news = $di->getNewsGateway()->getById($newsId);
|
||||
return new HttpResponse(200, 'home', ['news' => $news]);
|
||||
}
|
||||
}
|
||||
|
@@ -30,8 +30,25 @@ class NewsGateway
|
||||
}
|
||||
$news = [];
|
||||
while ($data = $req->fetch()) {
|
||||
$news[] = new News($data['title'], $data['content'], DateTime::createFromFormat('Y-m-d H:i:s', $data['publication_date']));
|
||||
$news[] = $this->createNews($data);
|
||||
}
|
||||
return $news;
|
||||
}
|
||||
|
||||
public function getById(int $id): News
|
||||
{
|
||||
$req = $this->pdo->prepare('SELECT * FROM news WHERE id_news=:id;');
|
||||
$req->bindValue(':id', $id, PDO::PARAM_INT);
|
||||
if (!$req->execute()) {
|
||||
return null;
|
||||
}
|
||||
$data = $req->fetch();
|
||||
$news = $this->createNews($data);
|
||||
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']));
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user