Permet de poster des commentaires
This commit is contained in:
@@ -4,8 +4,10 @@ declare(strict_types=1);
|
||||
|
||||
namespace Silex\Controller;
|
||||
|
||||
use DateTime;
|
||||
use Silex\DI\DI;
|
||||
use Silex\Http\HttpResponse;
|
||||
use Silex\Model\Comment;
|
||||
use Silex\Util\Pagination;
|
||||
|
||||
class UserController
|
||||
@@ -38,13 +40,15 @@ class UserController
|
||||
if($news->getSlug() !== $params['slug']){
|
||||
HttpResponse::redirect($di->getRouter()->url($news->getSlugRedirect()));
|
||||
}
|
||||
return new HttpResponse(200, 'newsView', ['news' => $news]);
|
||||
$comments = $di->getCommentGateway()->getByNewsId($newsId);
|
||||
return new HttpResponse(200, 'newsView', ['news' => $news, 'comments' => $comments]);
|
||||
}
|
||||
|
||||
public function viewPostComments(DI $di, array $params): HttpResponse
|
||||
public function comment(DI $di, array $params): void
|
||||
{
|
||||
$newsId = intval($params['id']);
|
||||
$comments = $di->getCommentGateway()->getByNewsId($newsId);
|
||||
return new HttpResponse(200, 'comment', ['comments' => $comments]);
|
||||
$news = $di->getNewsGateway()->getById($newsId);
|
||||
$di->getCommentGateway()->insert(new Comment(-1, $newsId, new DateTime(), $_POST['content'], $di->getSecurity()->getCurrentUserId()));
|
||||
HttpResponse::redirect($di->getRouter()->url($news->getSlugRedirect()));
|
||||
}
|
||||
}
|
||||
|
@@ -17,6 +17,27 @@ class CommentGateway
|
||||
$this->pdo = $pdo;
|
||||
}
|
||||
|
||||
public function insert(Comment $comment): bool
|
||||
{
|
||||
$req = $this->pdo->prepare('INSERT INTO comment (news_id, content, author_id) VALUES (:news_id, :content, :author_id);');
|
||||
$req->execute(['news_id' => $comment->getNewsId(), 'content' => $comment->getContent(), 'author_id' => $comment->getAuthorId()]);
|
||||
$comment->setId(intval($this->pdo->lastInsertId()));
|
||||
return true;
|
||||
}
|
||||
|
||||
public function update(Comment $comment): bool
|
||||
{
|
||||
$req = $this->pdo->prepare('UPDATE comment SET news_id = :news_id, content = :content, author_id = :author_id WHERE id_comment = :id_comment;');
|
||||
$req->execute(['news_id' => $comment->getNewsId(), 'content' => $comment->getContent(), 'author_id' => $comment->getAuthorId(), 'id_comment' => $comment->getId()]);
|
||||
return true;
|
||||
}
|
||||
|
||||
public function delete(Comment $comment): void
|
||||
{
|
||||
$req = $this->pdo->prepare('DELETE FROM comment WHERE id_comment = :id_comment;');
|
||||
$req->execute(['id_comment' => $comment->getId()]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Comment[]
|
||||
*/
|
||||
|
@@ -34,7 +34,7 @@ class NewsGateway
|
||||
|
||||
public function delete(News $news): void
|
||||
{
|
||||
$req = $this->pdo->prepare('DELETE FROM news WHERE id = :id_news;');
|
||||
$req = $this->pdo->prepare('DELETE FROM news WHERE id_news = :id_news;');
|
||||
$req->execute(['id_news' => $news->getId()]);
|
||||
}
|
||||
|
||||
|
@@ -23,7 +23,7 @@ class Comment
|
||||
$this->authorId = $authorId;
|
||||
}
|
||||
|
||||
public function getCommentId(): int
|
||||
public function getId(): int
|
||||
{
|
||||
return $this->idComment;
|
||||
}
|
||||
@@ -47,4 +47,9 @@ class Comment
|
||||
{
|
||||
return $this->authorId;
|
||||
}
|
||||
|
||||
public function setId(int $id): void
|
||||
{
|
||||
$this->idComment = $id;
|
||||
}
|
||||
}
|
@@ -38,7 +38,7 @@ class Router
|
||||
|
||||
public function post(string $path, callable $callable): self
|
||||
{
|
||||
return $this->addRoute(['GET'], $path, $callable);
|
||||
return $this->addRoute(['POST'], $path, $callable);
|
||||
}
|
||||
|
||||
public function match(string $path, callable $callable): self
|
||||
|
Reference in New Issue
Block a user