diff options
Diffstat (limited to 'controller/feedcontroller.php')
-rw-r--r-- | controller/feedcontroller.php | 325 |
1 files changed, 162 insertions, 163 deletions
diff --git a/controller/feedcontroller.php b/controller/feedcontroller.php index 56c7d5af8..c09349b15 100644 --- a/controller/feedcontroller.php +++ b/controller/feedcontroller.php @@ -17,35 +17,36 @@ use \OCP\IRequest; use \OCP\IConfig; use \OCP\AppFramework\Controller; use \OCP\AppFramework\Http; -use \OCP\AppFramework\Http\JSONResponse; -use \OCA\News\BusinessLayer\ItemBusinessLayer; -use \OCA\News\BusinessLayer\FeedBusinessLayer; -use \OCA\News\BusinessLayer\FolderBusinessLayer; -use \OCA\News\BusinessLayer\BusinessLayerException; -use \OCA\News\BusinessLayer\BusinessLayerConflictException; +use \OCA\News\Service\ItemService; +use \OCA\News\Service\FeedService; +use \OCA\News\Service\FolderService; +use \OCA\News\Service\ServiceNotFoundException; +use \OCA\News\Service\ServiceConflictException; use \OCA\News\Db\FeedType; class FeedController extends Controller { - private $feedBusinessLayer; - private $folderBusinessLayer; - private $itemBusinessLayer; + use JSONHttpError; + + private $feedService; + private $folderService; + private $itemService; private $userId; private $settings; - public function __construct($appName, - IRequest $request, - FolderBusinessLayer $folderBusinessLayer, - FeedBusinessLayer $feedBusinessLayer, - ItemBusinessLayer $itemBusinessLayer, + public function __construct($appName, + IRequest $request, + FolderService $folderService, + FeedService $feedService, + ItemService $itemService, IConfig $settings, $userId){ parent::__construct($appName, $request); - $this->feedBusinessLayer = $feedBusinessLayer; - $this->folderBusinessLayer = $folderBusinessLayer; - $this->itemBusinessLayer = $itemBusinessLayer; + $this->feedService = $feedService; + $this->folderService = $folderService; + $this->itemService = $itemService; $this->userId = $userId; $this->settings = $settings; } @@ -59,20 +60,20 @@ class FeedController extends Controller { // this method is also used to update the interface // because of this we also pass the starred count and the newest // item id which will be used for marking feeds read - $params = array( - 'feeds' => $this->feedBusinessLayer->findAll($this->userId), - 'starred' => $this->itemBusinessLayer->starredCount($this->userId) - ); + $params = [ + 'feeds' => $this->feedService->findAll($this->userId), + 'starred' => $this->itemService->starredCount($this->userId) + ]; try { - $params['newestItemId'] = - $this->itemBusinessLayer->getNewestItemId($this->userId); - + $params['newestItemId'] = + $this->itemService->getNewestItemId($this->userId); + // An exception occurs if there is a newest item. If there is none, // simply ignore it and do not add the newestItemId - } catch (BusinessLayerException $ex) {} + } catch (ServiceNotFoundException $ex) {} - return new JSONResponse($params); + return $params; } @@ -80,11 +81,11 @@ class FeedController extends Controller { * @NoAdminRequired */ public function active(){ - $feedId = (int) $this->settings->getUserValue($this->userId, + $feedId = (int) $this->settings->getUserValue($this->userId, $this->appName,'lastViewedFeedId'); $feedType = $this->settings->getUserValue($this->userId, $this->appName, 'lastViewedFeedType'); - + // cast from null to int is 0 if($feedType !== null){ $feedType = (int) $feedType; @@ -93,205 +94,203 @@ class FeedController extends Controller { // check if feed or folder exists try { if($feedType === FeedType::FOLDER){ - $this->folderBusinessLayer->find($feedId, $this->userId); - + $this->folderService->find($feedId, $this->userId); + } elseif ($feedType === FeedType::FEED){ - $this->feedBusinessLayer->find($feedId, $this->userId); - + $this->feedService->find($feedId, $this->userId); + // if its the first launch, those values will be null } elseif($feedType === null){ - throw new BusinessLayerException(''); + throw new ServiceNotFoundException(''); } - - } catch (BusinessLayerException $ex){ + + } catch (ServiceNotFoundException $ex){ $feedId = 0; $feedType = FeedType::SUBSCRIPTIONS; } - $params = array( - 'activeFeed' => array( + return [ + 'activeFeed' => [ 'id' => $feedId, 'type' => $feedType - ) - ); - - return new JSONResponse($params); + ] + ]; } - /** - * @NoAdminRequired - */ - public function create(){ - $url = $this->params('url'); - $parentFolderId = (int) $this->params('parentFolderId'); - + /** + * @NoAdminRequired + * + * @param string $url + * @param int $parentFolderId + * @param string $title + * @return array|\OCP\AppFramework\Http\JSONResponse + */ + public function create($url, $parentFolderId, $title){ try { - // we need to purge deleted feeds if a feed is created to + // we need to purge deleted feeds if a feed is created to // prevent already exists exceptions - $this->feedBusinessLayer->purgeDeleted($this->userId, false); + $this->feedService->purgeDeleted($this->userId, false); - $feed = $this->feedBusinessLayer->create($url, $parentFolderId, $this->userId); - $params = array( - 'feeds' => array($feed) - ); + $feed = $this->feedService->create($url, $parentFolderId, + $this->userId, $title); + $params = ['feeds' => [$feed]]; try { - $params['newestItemId'] = - $this->itemBusinessLayer->getNewestItemId($this->userId); + $params['newestItemId'] = + $this->itemService->getNewestItemId($this->userId); // An exception occurs if there is a newest item. If there is none, // simply ignore it and do not add the newestItemId - } catch (BusinessLayerException $ex) {} - - return new JSONResponse($params); - - } catch(BusinessLayerConflictException $ex) { - return new JSONResponse(array( - 'msg' => $ex->getMessage() - ), Http::STATUS_CONFLICT); - - } catch(BusinessLayerException $ex) { - return new JSONResponse(array( - 'msg' => $ex->getMessage() - ), Http::STATUS_UNPROCESSABLE_ENTITY); + } catch (ServiceNotFoundException $ex) {} + + return $params; + + } catch(ServiceConflictException $ex) { + return $this->error($ex, Http::STATUS_CONFLICT); + } catch(ServiceNotFoundException $ex) { + return $this->error($ex, Http::STATUS_UNPROCESSABLE_ENTITY); } - } + } - /** - * @NoAdminRequired - */ - public function delete(){ - $feedId = (int) $this->params('feedId'); + /** + * @NoAdminRequired + * + * @param int $feedId + * @return \OCP\AppFramework\Http\JSONResponse + */ + public function delete($feedId){ try { - $this->feedBusinessLayer->markDeleted($feedId, $this->userId); - return new JSONResponse(); - } catch(BusinessLayerException $ex) { - return new JSONResponse(array( - 'msg' => $ex->getMessage() - ), Http::STATUS_NOT_FOUND); + $this->feedService->markDeleted($feedId, $this->userId); + } catch(ServiceNotFoundException $ex) { + return $this->error($ex, Http::STATUS_NOT_FOUND); } + + return []; } - /** - * @NoAdminRequired - */ - public function update(){ + /** + * @NoAdminRequired + * + * @param int $feedId + * @return array|\OCP\AppFramework\Http\JSONResponse + */ + public function update($feedId){ try { - $feedId = (int) $this->params('feedId'); - - $feed = $this->feedBusinessLayer->update($feedId, $this->userId); + $feed = $this->feedService->update($feedId, $this->userId); - $params = array( - 'feeds' => array( - // only pass unreadcount to not accidentally readd + return [ + 'feeds' => [ + // only pass unread count to not accidentally readd // the feed again - array( + [ 'id' => $feed->getId(), 'unreadCount' => $feed->getUnreadCount() - ) - ) - ); - - return new JSONResponse($params); + ] + ] + ]; - } catch(BusinessLayerException $ex) { - return new JSONResponse(array( - 'msg' => $ex->getMessage() - ), Http::STATUS_NOT_FOUND); + } catch(ServiceNotFoundException $ex) { + return $this->error($ex, Http::STATUS_NOT_FOUND); } - } + } - /** - * @NoAdminRequired - */ - public function move(){ - $feedId = (int) $this->params('feedId'); - $parentFolderId = (int) $this->params('parentFolderId'); + /** + * @NoAdminRequired + * + * @param int $feedId + * @param int $parentFolderId + * @return \OCP\AppFramework\Http\JSONResponse + */ + public function move($feedId, $parentFolderId){ try { - $this->feedBusinessLayer->move($feedId, $parentFolderId, $this->userId); - return new JSONResponse(); - } catch(BusinessLayerException $ex) { - return new JSONResponse(array( - 'msg' => $ex->getMessage() - ), Http::STATUS_NOT_FOUND); + $this->feedService->move($feedId, $parentFolderId, $this->userId); + } catch(ServiceNotFoundException $ex) { + return $this->error($ex, Http::STATUS_NOT_FOUND); } - } - /** - * @NoAdminRequired - */ - public function rename() { - $feedId = (int) $this->params('feedId'); - $feedTitle = $this->params('feedTitle'); + return []; + } + /** + * @NoAdminRequired + * + * @param int $feedId + * @param string $feedTitle + * @return \OCP\AppFramework\Http\JSONResponse + */ + public function rename($feedId, $feedTitle) { try { - $this->feedBusinessLayer->rename($feedId, $feedTitle, $this->userId); - return new JSONResponse(); - } catch(BusinessLayerException $ex) { - return new JSONResponse(array( - 'msg' => $ex->getMessage() - ), Http::STATUS_NOT_FOUND); + $this->feedService->rename($feedId, $feedTitle, $this->userId); + } catch(ServiceNotFoundException $ex) { + return $this->error($ex, Http::STATUS_NOT_FOUND); } + + return []; } - /** - * @NoAdminRequired - */ - public function import() { - $json = $this->params('json'); - $feed = $this->feedBusinessLayer->importArticles($json, $this->userId); + /** + * @NoAdminRequired + * + * @param array $json + * @return array + */ + public function import($json) { + $feed = $this->feedService->importArticles($json, $this->userId); + + $params = [ + 'starred' => $this->itemService->starredCount($this->userId) + ]; - $params = array(); if($feed) { - $params['feeds'] = array($feed); + $params['feeds'] = [$feed]; } - return new JSONResponse($params); + return $params; } - /** - * @NoAdminRequired - */ - public function read(){ - $feedId = (int) $this->params('feedId'); - $highestItemId = (int) $this->params('highestItemId'); - - $this->itemBusinessLayer->readFeed($feedId, $highestItemId, $this->userId); + /** + * @NoAdminRequired + * + * @param int $feedId + * @param int $highestItemId + * @return array + */ + public function read($feedId, $highestItemId){ + $this->itemService->readFeed($feedId, $highestItemId, $this->userId); - $params = array( - 'feeds' => array( - array( + return [ + 'feeds' => [ + [ 'id' => $feedId, 'unreadCount' => 0 - ) - ) - ); - return new JSONResponse($params); + ] + ] + ]; } - /** - * @NoAdminRequired - */ - public function restore(){ - $feedId = (int) $this->params('feedId'); - + /** + * @NoAdminRequired + * + * @param int $feedId + * @return \OCP\AppFramework\Http\JSONResponse + */ + public function restore($feedId){ try { - $this->feedBusinessLayer->unmarkDeleted($feedId, $this->userId); - return new JSONResponse(); - } catch(BusinessLayerException $ex) { - return new JSONResponse(array( - 'msg' => $ex->getMessage() - ), Http::STATUS_NOT_FOUND); + $this->feedService->unmarkDeleted($feedId, $this->userId); + } catch(ServiceNotFoundException $ex) { + return $this->error($ex, Http::STATUS_NOT_FOUND); } + + return []; } |