Include groups into API

This commit is contained in:
denfil 2015-12-24 15:07:06 +03:00
parent 31f1561e65
commit 5c5c9e1eef
2 changed files with 45 additions and 2 deletions

View File

@ -342,10 +342,18 @@ function count_failed_feeds()
// Get all feeds
function get_all()
{
return Database::getInstance('db')
$result = Database::getInstance('db')
->table('feeds')
->asc('title')
->findAll();
$groups = Group\get_all_grouped_by_feeds();
foreach ($result as &$feed) {
$feed['groups'] = isset($groups[$feed['id']])
? $groups[$feed['id']]
: array();
}
return $result;
}
// Get all feeds with the number unread/total items in the order failed, working, disabled
@ -397,10 +405,19 @@ function count_items($feed_id)
// Get one feed
function get($feed_id)
{
return Database::getInstance('db')
$result = Database::getInstance('db')
->table('feeds')
->eq('id', $feed_id)
->findOne();
if (!$result) {
return $result;
}
$groups = Group\get_all_grouped_by_feeds();
$result['groups'] = isset($groups[$result['id']])
? $groups[$result['id']]
: array();
return $result;
}
// Update parsing error column

View File

@ -17,6 +17,32 @@ function get_all()
->findAll();
}
/**
* Get all groups grouped by feed_id
*
* @return array
*/
function get_all_grouped_by_feeds()
{
$groups = Database::getInstance('db')
->table('groups')
->join('feeds_groups', 'group_id', 'id')
->columns('feed_id', 'group_id', 'title')
->findAll();
if (!$groups) {
return array();
}
$result = array();
foreach ($groups as $group) {
$result[$group['feed_id']][] = [
'group_id' => $group['group_id'],
'title' => $group['title']
];
}
return $result;
}
/**
* Get assoc array of group ids with assigned feeds ids
*