Create CommentRepository

This commit is contained in:
Olivier PEREZ 2015-04-02 23:32:24 +02:00
parent 7d84ec1e73
commit 612e47ffa3
5 changed files with 92 additions and 41 deletions

View File

@ -79,13 +79,6 @@ class FramaDB {
return $this->pdo->query($sql);
}
function allCommentsByPollId($poll_id) {
$prepared = $this->prepare('SELECT * FROM `' . Utils::table('comment') . '` WHERE poll_id = ? ORDER BY id');
$prepared->execute(array($poll_id));
return $prepared->fetchAll();
}
function allUserVotesByPollId($poll_id) {
$prepared = $this->prepare('SELECT * FROM `' . Utils::table('vote') . '` WHERE poll_id = ? ORDER BY id');
$prepared->execute(array($poll_id));
@ -205,36 +198,12 @@ class FramaDB {
return $prepared->execute([$poll_id]);
}
/**
* Delete all comments of a given poll.
*
* @param $poll_id int The ID of the given poll.
* @return bool|null true if action succeeded.
*/
function deleteCommentsByPollId($poll_id) {
$prepared = $this->prepare('DELETE FROM `' . Utils::table('comment') . '` WHERE poll_id = ?');
return $prepared->execute([$poll_id]);
}
function updateVote($poll_id, $vote_id, $name, $choices) {
$prepared = $this->prepare('UPDATE `' . Utils::table('vote') . '` SET choices = ?, name = ? WHERE poll_id = ? AND id = ?');
return $prepared->execute([$choices, $name, $poll_id, $vote_id]);
}
function insertComment($poll_id, $name, $comment) {
$prepared = $this->prepare('INSERT INTO `' . Utils::table('comment') . '` (poll_id, name, comment) VALUES (?,?,?)');
return $prepared->execute([$poll_id, $name, $comment]);
}
function deleteComment($poll_id, $comment_id) {
$prepared = $this->prepare('DELETE FROM `' . Utils::table('comment') . '` WHERE poll_id = ? AND id = ?');
return $prepared->execute([$poll_id, $comment_id]);
}
function deletePollById($poll_id) {
$prepared = $this->prepare('DELETE FROM `' . Utils::table('poll') . '` WHERE id = ?');

View File

@ -0,0 +1,59 @@
<?php
namespace Framadate\Repositories;
use Framadate\FramaDB;
use Framadate\Utils;
class CommentRepository extends AbstractRepository {
function __construct(FramaDB $connect) {
parent::__construct($connect);
}
function allCommentsByPollId($poll_id) {
$prepared = $this->prepare('SELECT * FROM `' . Utils::table('comment') . '` WHERE poll_id = ? ORDER BY id');
$prepared->execute(array($poll_id));
return $prepared->fetchAll();
}
/**
* Insert a new comment.
*
* @param $poll_id
* @param $name
* @param $comment
* @return bool
*/
function insert($poll_id, $name, $comment) {
$prepared = $this->prepare('INSERT INTO `' . Utils::table('comment') . '` (poll_id, name, comment) VALUES (?,?,?)');
return $prepared->execute([$poll_id, $name, $comment]);
}
function delete($poll_id, $comment_id) {
$prepared = $this->prepare('DELETE FROM `' . Utils::table('comment') . '` WHERE poll_id = ? AND id = ?');
return $prepared->execute([$poll_id, $comment_id]);
}
/**
* Delete all comments of a given poll.
*
* @param $poll_id int The ID of the given poll.
* @return bool|null true if action succeeded.
*/
function deleteByPollId($poll_id) {
$prepared = $this->prepare('DELETE FROM `' . Utils::table('comment') . '` WHERE poll_id = ?');
return $prepared->execute([$poll_id]);
}
public function exists($poll_id, $name, $comment) {
$prepared = $this->prepare('SELECT 1 FROM `' . Utils::table('comment') . '` WHERE poll_id = ? QND name = ? AND comment = ?');
$prepared->execute(array($poll_id, $name, $comment));
return $prepared->rowCount() > 0;
}
}

View File

@ -26,6 +26,7 @@ class RepositoryFactory {
private static $pollRepository;
private static $slotRepository;
private static $commentRepository;
/**
* @param FramaDB $connect
@ -56,4 +57,15 @@ class RepositoryFactory {
return self::$slotRepository;
}
/**
* @return CommentRepository The singleton of CommentRepository
*/
static function commentRepository() {
if (self::$commentRepository == null) {
self::$commentRepository = new CommentRepository(self::$connect);
}
return self::$commentRepository;
}
}

View File

@ -2,7 +2,7 @@
namespace Framadate\Services;
use Framadate\FramaDB;
use Framadate\Utils;
use Framadate\Repositories\RepositoryFactory;
/**
* Class AdminPollService
@ -14,17 +14,23 @@ class AdminPollService {
private $connect;
private $pollService;
private $logService;
private $pollRepository;
private $slotRepository;
private $commentRepository;
function __construct(FramaDB $connect, PollService $pollService, LogService $logService) {
$this->connect = $connect;
$this->pollService = $pollService;
$this->logService = $logService;
$this->pollRepository = RepositoryFactory::pollRepository();
$this->slotRepository = RepositoryFactory::slotRepository();
$this->commentRepository = RepositoryFactory::commentRepository();
}
function updatePoll($poll) {
global $config;
if ($poll->end_date > $poll->creation_date && $poll->end_date <= strtotime($poll->creation_date) + (86400 * $config['default_poll_duration'])) {
return $this->connect->updatePoll($poll);
return $this->pollRepository->update($poll);
} else {
return false;
}
@ -38,7 +44,7 @@ class AdminPollService {
* @return mixed true is action succeeded
*/
function deleteComment($poll_id, $comment_id) {
return $this->connect->deleteComment($poll_id, $comment_id);
return $this->commentRepository->delete($poll_id, $comment_id);
}
/**
@ -49,7 +55,7 @@ class AdminPollService {
*/
function cleanComments($poll_id) {
$this->logService->log("CLEAN_COMMENTS", "id:$poll_id");
return $this->connect->deleteCommentsByPollId($poll_id);
return $this->commentRepository->deleteByPollId($poll_id);
}
/**
@ -81,12 +87,12 @@ class AdminPollService {
* @return bool true is action succeeded
*/
function deleteEntirePoll($poll_id) {
$poll = $this->connect->findPollById($poll_id);
$poll = $this->pollRepository->findById($poll_id);
$this->logService->log('DELETE_POLL', "id:$poll->id, format:$poll->format, admin:$poll->admin_name, mail:$poll->admin_mail");
// Delete the entire poll
$this->connect->deleteVotesByPollId($poll_id);
$this->connect->deleteCommentsByPollId($poll_id);
$this->commentRepository->deleteByPollId($poll_id);
$this->connect->deleteSlotsByPollId($poll_id);
$this->connect->deletePollById($poll_id);
@ -179,7 +185,7 @@ class AdminPollService {
* @return bool true if added
*/
public function addSlot($poll_id, $datetime, $new_moment) {
$slots = $this->connect->allSlotsByPollId($poll_id);
$slots = $this->slotRepository->listByPollId($poll_id);
$result = $this->findInsertPosition($slots, $datetime, $new_moment);
// Begin transaction

View File

@ -28,12 +28,14 @@ class PollService {
private $logService;
private $pollRepository;
private $slotRepository;
private $commentRepository;
function __construct(FramaDB $connect, LogService $logService) {
$this->connect = $connect;
$this->logService = $logService;
$this->pollRepository = RepositoryFactory::pollRepository();
$this->slotRepository = RepositoryFactory::slotRepository();
$this->commentRepository = RepositoryFactory::commentRepository();
}
/**
@ -51,7 +53,7 @@ class PollService {
}
function allCommentsByPollId($poll_id) {
return $this->connect->allCommentsByPollId($poll_id);
return $this->commentRepository->allCommentsByPollId($poll_id);
}
function allVotesByPollId($poll_id) {
@ -75,8 +77,11 @@ class PollService {
}
function addComment($poll_id, $name, $comment) {
// TODO Check if there is no duplicate before to add a new comment
return $this->connect->insertComment($poll_id, $name, $comment);
if ($this->commentRepository->exists($poll_id, $name, $comment)) {
return true;
} else {
return $this->commentRepository->insert($poll_id, $name, $comment);
}
}
public function countVotesByPollId($poll_id) {