From ed2430afa2d1fd9b63ae4c782853e8aab6299a13 Mon Sep 17 00:00:00 2001 From: Kishiko Date: Wed, 7 Mar 2018 09:17:49 +0200 Subject: [PATCH 1/5] Add comments feature --- Controller/Blog.php | 1 + Controller/Comment.php | 52 ++++++++++++++++++++++++++++++++++++++++++ Model/Blog.php | 7 ++++++ Model/Comment.php | 33 +++++++++++++++++++++++++++ View/post.php | 34 +++++++++++++++++++++++++++ db.sql | 11 +++++++++ 6 files changed, 138 insertions(+) create mode 100644 Controller/Comment.php create mode 100644 Model/Comment.php diff --git a/Controller/Blog.php b/Controller/Blog.php index 84715dd..0d0ca24 100644 --- a/Controller/Blog.php +++ b/Controller/Blog.php @@ -44,6 +44,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->oModel->getPostComments($this->_iId); $this->oUtil->getView('post'); } diff --git a/Controller/Comment.php b/Controller/Comment.php new file mode 100644 index 0000000..139cd04 --- /dev/null +++ b/Controller/Comment.php @@ -0,0 +1,52 @@ + + * @copyright (c) 2015-2017, Pierre-Henry Soria. All Rights Reserved. + * @license Lesser General Public License + * @link http://hizup.uk + */ + +namespace TestProject\Controller; + +class Comment +{ + protected $oUtil, $oModel; + + public function __construct() + { + // Enable PHP Session + if (empty($_SESSION)) + @session_start(); + + $this->oUtil = new \TestProject\Engine\Util; + + /** Get the Model class in all the controller class **/ + $this->oUtil->getModel('Comment'); + $this->oModel = 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->oModel->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/Blog.php b/Model/Blog.php index cca329d..2a107c3 100644 --- a/Model/Blog.php +++ b/Model/Blog.php @@ -11,10 +11,12 @@ class Blog { protected $oDb; + private $oCommentModel; public function __construct() { $this->oDb = new \TestProject\Engine\Db; + $this->oCommentModel = new \TestProject\Model\Comment; } public function get($iOffset, $iLimit) @@ -32,6 +34,11 @@ public function getAll() return $oStmt->fetchAll(\PDO::FETCH_OBJ); } + public function getPostComments($iPostId) + { + return $this->oCommentModel->getPostComments($iPostId); + } + public function add(array $aData) { $oStmt = $this->oDb->prepare('INSERT INTO Posts (title, body, createdDate) VALUES(:title, :body, :created_date)'); diff --git a/Model/Comment.php b/Model/Comment.php new file mode 100644 index 0000000..274c112 --- /dev/null +++ b/Model/Comment.php @@ -0,0 +1,33 @@ + + * @copyright (c) 2015-2017, Pierre-Henry Soria. All Rights Reserved. + * @license Lesser General Public License + * @link http://hizup.uk + */ + +namespace TestProject\Model; + +class Comment +{ + protected $oDb; + + public function __construct() + { + $this->oDb = new \TestProject\Engine\Db; + } + + 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/View/post.php b/View/post.php index ff5b6c3..1c5fa91 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, From 5beeb54d18d69ed5334a1e8d7c8fb20a58967415 Mon Sep 17 00:00:00 2001 From: Kishiko Date: Wed, 7 Mar 2018 09:26:28 +0200 Subject: [PATCH 2/5] Add comment feature description --- README.md | 2 ++ 1 file changed, 2 insertions(+) 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. From f46d8359ccc040fa254530d18c90654a3ac11c18 Mon Sep 17 00:00:00 2001 From: Serge Kishiko Date: Thu, 22 Mar 2018 12:57:45 +0200 Subject: [PATCH 3/5] Enhancement after code review from the pull request --- Controller/Blog.php | 5 +++-- Controller/Comment.php | 16 ++++++---------- Engine/Config.php | 4 ++-- Model/Blog.php | 7 ------- Model/Comment.php | 9 +-------- View/post.php | 2 +- 6 files changed, 13 insertions(+), 30 deletions(-) diff --git a/Controller/Blog.php b/Controller/Blog.php index 0d0ca24..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,7 +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->oModel->getPostComments($this->_iId); + $this->oUtil->oComments = $this->oCommentModel->getPostComments($this->_iId); $this->oUtil->getView('post'); } diff --git a/Controller/Comment.php b/Controller/Comment.php index 139cd04..88dab12 100644 --- a/Controller/Comment.php +++ b/Controller/Comment.php @@ -8,21 +8,17 @@ namespace TestProject\Controller; -class Comment +class Comment extends Blog { - protected $oUtil, $oModel; + private $oCommentModel; public function __construct() { - // Enable PHP Session - if (empty($_SESSION)) - @session_start(); - - $this->oUtil = new \TestProject\Engine\Util; - + parent::__construct(); + /** Get the Model class in all the controller class **/ $this->oUtil->getModel('Comment'); - $this->oModel = new \TestProject\Model\Comment; + $this->oCommentModel = new \TestProject\Model\Comment; } public function add() @@ -38,7 +34,7 @@ public function add() 'created_date' => date('Y-m-d H:i:s') ); - if ($this->oModel->add($aData)) + 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.'; diff --git a/Engine/Config.php b/Engine/Config.php index 54d6bb7..2750d48 100644 --- a/Engine/Config.php +++ b/Engine/Config.php @@ -12,10 +12,10 @@ final class Config { // Database info (if you want to test the script, please edit the below constants with yours) const - DB_HOST = 'mymvcblog', + DB_HOST = 'localhost', DB_NAME = 'mymvcblog', DB_USR = 'root', - DB_PWD = 'pwd123', + DB_PWD = '', // Title of the site SITE_NAME = 'My Simple Blog!'; diff --git a/Model/Blog.php b/Model/Blog.php index 2a107c3..cca329d 100644 --- a/Model/Blog.php +++ b/Model/Blog.php @@ -11,12 +11,10 @@ class Blog { protected $oDb; - private $oCommentModel; public function __construct() { $this->oDb = new \TestProject\Engine\Db; - $this->oCommentModel = new \TestProject\Model\Comment; } public function get($iOffset, $iLimit) @@ -34,11 +32,6 @@ public function getAll() return $oStmt->fetchAll(\PDO::FETCH_OBJ); } - public function getPostComments($iPostId) - { - return $this->oCommentModel->getPostComments($iPostId); - } - public function add(array $aData) { $oStmt = $this->oDb->prepare('INSERT INTO Posts (title, body, createdDate) VALUES(:title, :body, :created_date)'); diff --git a/Model/Comment.php b/Model/Comment.php index 274c112..9a5e0e7 100644 --- a/Model/Comment.php +++ b/Model/Comment.php @@ -8,15 +8,8 @@ namespace TestProject\Model; -class Comment +class Comment extends Blog { - protected $oDb; - - public function __construct() - { - $this->oDb = new \TestProject\Engine\Db; - } - public function add(array $aData) { $oStmt = $this->oDb->prepare('INSERT INTO Comments (idPost, content, authorName, createdDate) VALUES(:id_post, :content, :author_name, :created_date)'); diff --git a/View/post.php b/View/post.php index 1c5fa91..0ff6b8b 100644 --- a/View/post.php +++ b/View/post.php @@ -43,7 +43,7 @@

Add a new comment

-
+ From 4ad2bf3ca5a2130330cd61a9e34fffbb97b376ea Mon Sep 17 00:00:00 2001 From: Serge Kishiko Date: Thu, 22 Mar 2018 13:06:14 +0200 Subject: [PATCH 4/5] Reset database Config values --- Engine/Config.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Engine/Config.php b/Engine/Config.php index 2750d48..54d6bb7 100644 --- a/Engine/Config.php +++ b/Engine/Config.php @@ -12,10 +12,10 @@ final class Config { // Database info (if you want to test the script, please edit the below constants with yours) const - DB_HOST = 'localhost', + DB_HOST = 'mymvcblog', DB_NAME = 'mymvcblog', DB_USR = 'root', - DB_PWD = '', + DB_PWD = 'pwd123', // Title of the site SITE_NAME = 'My Simple Blog!'; From 1e4972ea0b1127d2f539a43a8b2ffce78ce2004e Mon Sep 17 00:00:00 2001 From: Serge Kishiko Date: Thu, 22 Mar 2018 22:55:37 +0200 Subject: [PATCH 5/5] Fix 'Cannot redeclare \TestProject\Model\Comment class' error --- Controller/Comment.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/Controller/Comment.php b/Controller/Comment.php index 88dab12..ab1a86b 100644 --- a/Controller/Comment.php +++ b/Controller/Comment.php @@ -10,11 +10,9 @@ class Comment extends Blog { - private $oCommentModel; - public function __construct() { - parent::__construct(); + $this->oUtil = new \TestProject\Engine\Util; /** Get the Model class in all the controller class **/ $this->oUtil->getModel('Comment');