Refactor API

This commit is contained in:
denfil 2015-12-24 17:34:55 +03:00
parent 5c5c9e1eef
commit eaf7801778
3 changed files with 42 additions and 47 deletions

View File

@ -20,13 +20,27 @@ $server->register('app.version', function () {
// Get all feeds
$server->register('feed.list', function () {
return Model\Feed\get_all();
$feeds = Model\Feed\get_all();
if (!$feeds) {
return $feeds;
}
$groups = Model\Group\get_feeds_map();
foreach ($feeds as &$feed) {
$feed['feed_group_ids'] = isset($groups[$feed['id']])
? $groups[$feed['id']]
: array();
}
return $feeds;
});
// Get one feed
$server->register('feed.info', function ($feed_id) {
return Model\Feed\get($feed_id);
$result = Model\Feed\get($feed_id);
$result['feed_group_ids'] = Model\Group\get_feed_group_ids($feed_id);
return $result;
});
// Add a new feed
@ -77,6 +91,12 @@ $server->register('feed.update', function($feed_id) {
return Model\Feed\refresh($feed_id);
});
// Get all groups
$server->register('group.list', function () {
return Model\Group\get_all();
});
// Get all items for a specific feed
$server->register('item.feed.list', function ($feed_id, $offset = null, $limit = null) {

View File

@ -342,18 +342,10 @@ function count_failed_feeds()
// Get all feeds
function get_all()
{
$result = Database::getInstance('db')
return 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
@ -405,19 +397,10 @@ function count_items($feed_id)
// Get one feed
function get($feed_id)
{
$result = Database::getInstance('db')
return 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,32 +17,6 @@ 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
*
@ -73,6 +47,24 @@ function get_map()
return $map;
}
/**
* Get assoc array of feeds ids with assigned groups ids
*
* @return array
*/
function get_feeds_map()
{
$result = Database::getInstance('db')
->table('feeds_groups')
->findAll();
$map = array();
foreach ($result as $row) {
$map[$row['feed_id']][] = $row['group_id'];
}
return $map;
}
/**
* Get all groups assigned to feed
*