From 643fa4624dd7ba2db1349f16131bf330aeee3387 Mon Sep 17 00:00:00 2001 From: Bernhard Posselt Date: Wed, 9 Apr 2014 16:10:48 +0200 Subject: port to internal controller, some routes are still broken --- controller/itemapicontroller.php | 276 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 276 insertions(+) create mode 100644 controller/itemapicontroller.php (limited to 'controller/itemapicontroller.php') diff --git a/controller/itemapicontroller.php b/controller/itemapicontroller.php new file mode 100644 index 000000000..ce3d915d7 --- /dev/null +++ b/controller/itemapicontroller.php @@ -0,0 +1,276 @@ +. +* +*/ + +namespace OCA\News\Controller; + +use \OCP\IRequest; +use \OCP\AppFramework\Controller; +use \OCP\AppFramework\Http; +use \OCP\AppFramework\Http\JSONResponse; + +use \OCA\News\BusinessLayer\ItemBusinessLayer; +use \OCA\News\BusinessLayer\BusinessLayerException; +use \OCA\News\Core\API; + +class ItemApiController extends Controller { + + private $itemBusinessLayer; + private $api; + + public function __construct(API $api, + IRequest $request, + ItemBusinessLayer $itemBusinessLayer){ + parent::__construct($api->getAppName(), $request); + $this->itemBusinessLayer = $itemBusinessLayer; + $this->api = $api; + } + + + /** + * @NoAdminRequired + * @NoCSRFRequired + * @API + */ + public function index() { + $result = array( + 'items' => array() + ); + + $userId = $this->api->getUserId(); + $batchSize = (int) $this->params('batchSize', 20); + $offset = (int) $this->params('offset', 0); + $type = (int) $this->params('type'); + $id = (int) $this->params('id'); + $showAll = $this->params('getRead'); + + if($showAll === 'true' || $showAll === true) { + $showAll = true; + } else { + $showAll = false; + } + + $items = $this->itemBusinessLayer->findAll( + $id, + $type, + $batchSize, + $offset, + $showAll, + $userId + ); + + foreach ($items as $item) { + array_push($result['items'], $item->toAPI()); + } + + return new JSONResponse($result); + } + + + /** + * @NoAdminRequired + * @NoCSRFRequired + * @API + */ + public function getUpdated() { + $result = array( + 'items' => array() + ); + + $userId = $this->api->getUserId(); + $lastModified = (int) $this->params('lastModified', 0); + $type = (int) $this->params('type'); + $id = (int) $this->params('id'); + + $items = $this->itemBusinessLayer->findAllNew( + $id, + $type, + $lastModified, + true, + $userId + ); + + foreach ($items as $item) { + array_push($result['items'], $item->toAPI()); + } + + return new JSONResponse($result); + } + + + private function setRead($isRead) { + $userId = $this->api->getUserId(); + $itemId = (int) $this->params('itemId'); + try { + $this->itemBusinessLayer->read($itemId, $isRead, $userId); + return new JSONResponse(); + } catch(BusinessLayerException $ex){ + return new JSONResponse(array('message' => $ex->getMessage()), + Http::STATUS_NOT_FOUND); + } + } + + + private function setStarred($isStarred) { + $userId = $this->api->getUserId(); + $feedId = (int) $this->params('feedId'); + $guidHash = $this->params('guidHash'); + try { + $this->itemBusinessLayer->star($feedId, $guidHash, $isStarred, $userId); + return new JSONResponse(); + } catch(BusinessLayerException $ex){ + return new JSONResponse(array('message' => $ex->getMessage()), + Http::STATUS_NOT_FOUND); + } + } + + + /** + * @NoAdminRequired + * @NoCSRFRequired + * @API + */ + public function read() { + return $this->setRead(true); + } + + + /** + * @NoAdminRequired + * @NoCSRFRequired + * @API + */ + public function unread() { + return $this->setRead(false); + } + + + /** + * @NoAdminRequired + * @NoCSRFRequired + * @API + */ + public function star() { + return $this->setStarred(true); + } + + + /** + * @NoAdminRequired + * @NoCSRFRequired + * @API + */ + public function unstar() { + return $this->setStarred(false); + } + + + /** + * @NoAdminRequired + * @NoCSRFRequired + * @API + */ + public function readAll() { + $userId = $this->api->getUserId(); + $newestItemId = (int) $this->params('newestItemId'); + + $this->itemBusinessLayer->readAll($newestItemId, $userId); + return new JSONResponse(); + } + + + private function setMultipleRead($isRead) { + $userId = $this->api->getUserId(); + $items = $this->params('items'); + + foreach($items as $id) { + try { + $this->itemBusinessLayer->read($id, $isRead, $userId); + } catch(BusinessLayerException $ex) { + continue; + } + } + + return new JSONResponse(); + } + + + /** + * @NoAdminRequired + * @NoCSRFRequired + * @API + */ + public function readMultiple() { + return $this->setMultipleRead(true); + } + + + /** + * @NoAdminRequired + * @NoCSRFRequired + * @API + */ + public function unreadMultiple() { + return $this->setMultipleRead(false); + } + + + private function setMultipleStarred($isStarred) { + $userId = $this->api->getUserId(); + $items = $this->params('items'); + + foreach($items as $item) { + try { + $this->itemBusinessLayer->star($item['feedId'], + $item['guidHash'], $isStarred, $userId); + } catch(BusinessLayerException $ex) { + continue; + } + } + + return new JSONResponse(); + } + + + /** + * @NoAdminRequired + * @NoCSRFRequired + * @API + */ + public function starMultiple() { + return $this->setMultipleStarred(true); + } + + + /** + * @NoAdminRequired + * @NoCSRFRequired + * @API + */ + public function unstarMultiple() { + return $this->setMultipleStarred(false); + } + + +} -- cgit v1.2.3