summaryrefslogtreecommitdiffstats
path: root/controller/itemapicontroller.php
diff options
context:
space:
mode:
authorBernhard Posselt <dev@bernhard-posselt.com>2014-04-09 16:10:48 +0200
committerBernhard Posselt <dev@bernhard-posselt.com>2014-04-09 22:52:27 +0200
commit643fa4624dd7ba2db1349f16131bf330aeee3387 (patch)
tree73bc787a3eb1ed5f53ab932187546c980ac50bb5 /controller/itemapicontroller.php
parentefe9db1e064c9736fe35c27040ee60fa28ca8d4d (diff)
port to internal controller, some routes are still broken
Diffstat (limited to 'controller/itemapicontroller.php')
-rw-r--r--controller/itemapicontroller.php276
1 files changed, 276 insertions, 0 deletions
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 @@
+<?php
+
+/**
+* ownCloud - News
+*
+* @author Alessandro Cosentino
+* @author Bernhard Posselt
+* @copyright 2012 Alessandro Cosentino cosenal@gmail.com
+* @copyright 2012 Bernhard Posselt dev@bernhard-posselt.com
+*
+* This library is free software; you can redistribute it and/or
+* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
+* License as published by the Free Software Foundation; either
+* version 3 of the License, or any later version.
+*
+* This library is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
+*
+* You should have received a copy of the GNU Affero General Public
+* License along with this library. If not, see <http://www.gnu.org/licenses/>.
+*
+*/
+
+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);
+ }
+
+
+}