Skip to content

Commit

Permalink
Adds remaining methods for the post repository
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobemerick committed Dec 1, 2015
1 parent d7fe621 commit 34318de
Show file tree
Hide file tree
Showing 2 changed files with 162 additions and 2 deletions.
157 changes: 155 additions & 2 deletions src/Domain/Blog/MysqlPostRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,168 @@ public function findPostByPath($category, $path)
FROM `jpemeric_blog`.`post`
WHERE `path` = :path AND `category` = :category AND `display` = :is_active
LIMIT 1";
$bindings = array(
$bindings = [
'path' => $path,
'category' => $category,
'is_active' => 1,
);
];

return $this
->connections
->getRead()
->fetchOne($query, $bindings);
}

public function getActivePosts($limit = null, $offset = 0)
{
$query = "
SELECT `id`, `title`, `path`, `date`, `body`, `category`
FROM `jpemeric_blog`.`post`
WHERE `display` = :is_active
ORDER BY `date` DESC";
if ($limit != null) {
$query .= "
LIMIT {$offset}, {$limit}";
}

$bindings = [
'is_active' => 1,
];

return $this
->connections
->getRead()
->fetchAll($query, $bindings);
}

public function getActivePostsCount()
{
$query = "
SELECT COUNT(1) AS `count`
FROM `jpemeric_blog`.`post`
WHERE `display` = :is_active";
$bindings = [
'is_active' => 1,
];

return $this
->connections
->getRead()
->fetchValue($query, $bindings);
}

public function getActivePostsByTag($tag, $limit = null, $offset = 0)
{
$query = "
SELECT `id`, `title`, `path`, `date`, `body`, `category`
FROM `jpemeric_blog`.`post`
INNER JOIN `jpemeric_blog`.`ptlink` ON `ptlink`.`post_id` = `post`.`id` AND
`ptlink`.`tag_id` = :tag_id
WHERE `display` = :is_active";
if ($limit != null) {
$query .= "
LIMIT {$offset}, {$limit}";
}

$bindings = [
'tag_id' => $tag,
'is_active' => 1,
];

return $this
->connections
->getRead()
->fetchAll($query, $bindings);
}

public function getActivePostsCountByTag($tag)
{
$query = "
SELECT COUNT(1) AS `count`
FROM `jpemeric_blog`.`post`
INNER JOIN `jpemeric_blog`.`ptlink` ON `ptlink`.`post_id` = `post`.`id` AND
`ptlink`.`tag_id` = :tag_id
WHERE `display` = :is_active";
$bindings = [
'tag_id' => $tag,
'is_active' => 1,
];

return $this
->connections
->getRead()
->fetchValue($query, $bindings);
}

public function getActivePostsByCategory($category, $limit = null, $offset = 0)
{
$query = "
SELECT `id`, `title`, `path`, `date`, `body`, `category`
FROM `jpemeric_blog`.`post`
WHERE `category` = :category AND `display` = :is_active";
if ($limit != null) {
$query .= "
LIMIT {$offset}, {$limit}";
}

$bindings = [
'category' => $category,
'is_active' => 1,
];

return $this
->connections
->getRead()
->fetchAll($query, $bindings);
}

public function getActivePostsCountByCategory($category)
{
$query = "
SELECT COUNT(1) AS `count`
FROM `jpemeric_blog`.`post`
WHERE `category` = :category AND `display` = :is_active";
$bindings = [
'category' => $category,
'is_active' => 1,
];

return $this
->connections
->getRead()
->fetchValue($query, $bindings);
}

public function getActivePostsByRelatedTags($post, $limit = 4)
{
$query = "
SELECT `id`, `title`, `path`, `date`, `body`, `category`, COUNT(1) AS `count`
FROM `jpemeric_blog`.`post`
INNER JOIN `jpemeric_blog`.`ptlink` ON `ptlink`.`post_id` = `post`.`id` AND
`ptlink`.`tag_id` IN (
SELECT `id`
FROM `jpemeric_blog`.`tag`
INNER JOIN `jpemeric_blog`.`ptlink` ON `ptlink`.`tag_id` = `tag`.`id` AND
`ptlink`.`post_id` = :post)
WHERE `id` <> :post AND `id` NOT IN (
SELECT `post`
FROM `jpemeric_blog`.`series_post`
WHERE `id` = (
SELECT `series`
FROM `jpemeric_blog`.`series_post`
WHERE `post` = :post
)) AND `display` = :is_active
GROUP BY `id`
ORDER BY `count` DESC
LIMIT {$limit}";
$bindings = [
'post' => $post,
'is_active' => 1,
];

return $this
->connections
->getRead()
->fetchAll($query, $bindings);
}
}
7 changes: 7 additions & 0 deletions src/Domain/Blog/PostRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,11 @@
interface PostRepository
{
public function findPostByPath($category, $path);
public function getActivePosts($limit = null, $offset= 0);
public function getActivePostsCount();
public function getActivePostsByTag($tag, $limit = null, $offset = 0);
public function getActivePostsCountByTag($tag);
public function getActivePostsByCategory($category, $limit = null, $offset = 0);
public function getActivePostsCountByCategory($category);
public function getActivePostsByRelatedTags($post, $limit = 4);
}

0 comments on commit 34318de

Please sign in to comment.