diff --git a/Controller/Blog.php b/Controller/Blog.php index 84715dd..eb0070c 100644 --- a/Controller/Blog.php +++ b/Controller/Blog.php @@ -12,7 +12,7 @@ class Blog { const MAX_POSTS = 5; - protected $oUtil, $oModel; + protected $oUtil, $oModel, $oCommentModel; private $_iId; public function __construct() @@ -26,6 +26,7 @@ public function __construct() /** Get the Model class in all the controller class **/ $this->oUtil->getModel('Blog'); $this->oModel = new \TestProject\Model\Blog; + $this->oCommentModel = new \TestProject\Model\Comment; /** Get the Post ID in the constructor in order to avoid the duplication of the same code **/ $this->_iId = (int) (!empty($_GET['id']) ? $_GET['id'] : 0); @@ -44,6 +45,7 @@ public function index() public function post() { $this->oUtil->oPost = $this->oModel->getById($this->_iId); // Get the data of the post + $this->oUtil->oComments = $this->oCommentModel->getPostComments($this->_iId); $this->oUtil->getView('post'); } diff --git a/Controller/Comment.php b/Controller/Comment.php new file mode 100644 index 0000000..ab1a86b --- /dev/null +++ b/Controller/Comment.php @@ -0,0 +1,46 @@ + + * @copyright (c) 2015-2017, Pierre-Henry Soria. All Rights Reserved. + * @license Lesser General Public License + * @link http://hizup.uk + */ + +namespace TestProject\Controller; + +class Comment extends Blog +{ + public function __construct() + { + $this->oUtil = new \TestProject\Engine\Util; + + /** Get the Model class in all the controller class **/ + $this->oUtil->getModel('Comment'); + $this->oCommentModel = new \TestProject\Model\Comment; + } + + public function add() + { + if (!empty($_POST['add_comment'])) + { + if (isset($_POST['id_post'], $_POST['content'], $_POST['author_name'])) + { + $aData = array( + 'id_post' => $_POST['id_post'], + 'content' => $_POST['content'], + 'author_name' => $_POST['author_name'], + 'created_date' => date('Y-m-d H:i:s') + ); + + if ($this->oCommentModel->add($aData)) + header('Location:' . ROOT_URL . '?p=blog&a=post&id=' . $_POST['id_post']); + else + $this->oUtil->sErrMsg = 'Whoops! An error has occurred! Please try again later.'; + } + else + { + $this->oUtil->sErrMsg = 'All fields are required and the title cannot exceed 50 characters.'; + } + } + } +} diff --git a/Model/Comment.php b/Model/Comment.php new file mode 100644 index 0000000..9a5e0e7 --- /dev/null +++ b/Model/Comment.php @@ -0,0 +1,26 @@ + + * @copyright (c) 2015-2017, Pierre-Henry Soria. All Rights Reserved. + * @license Lesser General Public License + * @link http://hizup.uk + */ + +namespace TestProject\Model; + +class Comment extends Blog +{ + public function add(array $aData) + { + $oStmt = $this->oDb->prepare('INSERT INTO Comments (idPost, content, authorName, createdDate) VALUES(:id_post, :content, :author_name, :created_date)'); + return $oStmt->execute($aData); + } + + public function getPostComments($iPostId) + { + $oStmt = $this->oDb->prepare('SELECT * FROM Comments WHERE idPost = :postId'); + $oStmt->bindParam(':postId', $iPostId, \PDO::PARAM_INT); + $oStmt->execute(); + return $oStmt->fetchAll(\PDO::FETCH_OBJ); + } +} diff --git a/README.md b/README.md index a2cba49..b32e52d 100644 --- a/README.md +++ b/README.md @@ -32,6 +32,7 @@ However, if you need to build a blog system, again this project can be used as a * List of blog articles showing the **title**, **body** truncated to 100 characters, and **date**. It should show only the latest 5 articles * Single blog article showing the **title**, **full body**, and the **date** +* List of article comments showing the **date**, **author name** and **content** #### The Backend @@ -39,6 +40,7 @@ However, if you need to build a blog system, again this project can be used as a * Possibility to add a new blog article with a title and body. The title should allow a maximum of 50 characters * Possibility to edit an existing blog article * Possibility to delete an article +* Possibility to comment a blog article by specifying its **author name** and its **content** * Logout feature for the admin user HTML and CSS code should be kept to the minimum needed to make the website functional – This project is purely to assess how you approach the problem and not how good it looks. diff --git a/View/post.php b/View/post.php index ff5b6c3..0ff6b8b 100644 --- a/View/post.php +++ b/View/post.php @@ -25,6 +25,40 @@ ?> +
+ + diff --git a/db.sql b/db.sql index 12fee1b..fbf268a 100644 --- a/db.sql +++ b/db.sql @@ -29,6 +29,17 @@ INSERT INTO Posts (title, body, createdDate) VALUES (@sPostTitle, @sPostBody, @sPostDate); +CREATE TABLE IF NOT EXISTS Comments ( + id int(10) unsigned NOT NULL AUTO_INCREMENT, + content longtext NOT NULL, + authorName varchar(60) NOT NULL, + idPost int(10) unsigned, + createdDate datetime NOT NULL DEFAULT '0000-00-00 00:00:00', + PRIMARY KEY (id), + FOREIGN KEY (idPost) REFERENCES Posts(id) +) DEFAULT CHARSET=utf8; + + CREATE TABLE IF NOT EXISTS Admins ( id int(10) unsigned NOT NULL AUTO_INCREMENT, email varchar(120) NOT NULL,