. * */ namespace OCA\News\External; use \OCA\AppFramework\Core\API; use \OCA\AppFramework\Controller\Controller; use \OCA\AppFramework\Http\Request; use \OCA\News\BusinessLayer\FeedBusinessLayer; use \OCA\News\BusinessLayer\FolderBusinessLayer; use \OCA\News\BusinessLayer\ItemBusinessLayer; use \OCA\News\BusinessLayer\BusinessLayerException; use \OCA\News\BusinessLayer\BusinessLayerExistsException; class FeedAPI extends Controller { private $itemBusinessLayer; private $feedBusinessLayer; private $folderBusinessLayer; public function __construct(API $api, Request $request, FolderBusinessLayer $folderBusinessLayer, FeedBusinessLayer $feedBusinessLayer, ItemBusinessLayer $itemBusinessLayer){ parent::__construct($api, $request); $this->folderBusinessLayer = $folderBusinessLayer; $this->feedBusinessLayer = $feedBusinessLayer; $this->itemBusinessLayer = $itemBusinessLayer; } public function getAll() { $userId = $this->api->getUserId(); $result = array( 'feeds' => array(), 'starredCount' => $this->itemBusinessLayer->starredCount($userId) ); foreach ($this->feedBusinessLayer->findAll($userId) as $feed) { array_push($result['feeds'], $feed->toAPI()); } // check case when there are no items try { $result['newestItemId'] = $this->itemBusinessLayer->getNewestItemId($userId); } catch(BusinessLayerException $ex) {} return new NewsAPIResult($result); } public function create() { $userId = $this->api->getUserId(); $feedUrl = $this->params('url'); $folderId = (int) $this->params('folderId', 0); try { $this->feedBusinessLayer->purgeDeleted($userId, false); $feed = $this->feedBusinessLayer->create($feedUrl, $folderId, $userId); $result = array( 'feeds' => array($feed->toAPI()) ); try { $result['newestItemId'] = $this->itemBusinessLayer->getNewestItemId($userId); } catch(BusinessLayerException $ex) {} return new NewsAPIResult($result); } catch(BusinessLayerExistsException $ex) { return new NewsAPIResult(null, NewsAPIResult::EXISTS_ERROR, $ex->getMessage()); } catch(BusinessLayerException $ex) { return new NewsAPIResult(null, NewsAPIResult::NOT_FOUND_ERROR, $ex->getMessage()); } } public function delete() { $userId = $this->api->getUserId(); $feedId = (int) $this->params('feedId'); try { $this->feedBusinessLayer->delete($feedId, $userId); return new NewsAPIResult(); } catch(BusinessLayerException $ex) { return new NewsAPIResult(null, NewsAPIResult::NOT_FOUND_ERROR, $ex->getMessage()); } } public function read() { $userId = $this->api->getUserId(); $feedId = (int) $this->params('feedId'); $newestItemId = (int) $this->params('newestItemId'); $this->itemBusinessLayer->readFeed($feedId, $newestItemId, $userId); return new NewsAPIResult(); } public function move() { $userId = $this->api->getUserId(); $feedId = (int) $this->params('feedId'); $folderId = (int) $this->params('folderId'); try { $this->feedBusinessLayer->move($feedId, $folderId, $userId); return new NewsAPIResult(); } catch(BusinessLayerException $ex) { return new NewsAPIResult(null, NewsAPIResult::NOT_FOUND_ERROR, $ex->getMessage()); } } }