From f82b818bdb852f63fdb4a431808f37902f4272ff Mon Sep 17 00:00:00 2001 From: Bernhard Posselt Date: Tue, 13 May 2014 20:14:00 +0200 Subject: break all the things! --- controller/apicontroller.php | 94 ----------------------- controller/feedapicontroller.php | 91 ++++++++++++---------- controller/feedcontroller.php | 4 - controller/folderapicontroller.php | 17 ++--- controller/foldercontroller.php | 65 +++++++--------- controller/itemapicontroller.php | 146 ++++++++++++++++++------------------ controller/itemcontroller.php | 8 +- controller/jsonhttperror.php | 36 +++++++++ controller/pagecontroller.php | 27 +++---- controller/utilityapicontroller.php | 68 +++++++++++++++++ 10 files changed, 275 insertions(+), 281 deletions(-) delete mode 100644 controller/apicontroller.php create mode 100644 controller/jsonhttperror.php create mode 100644 controller/utilityapicontroller.php (limited to 'controller') diff --git a/controller/apicontroller.php b/controller/apicontroller.php deleted file mode 100644 index a23a60318..000000000 --- a/controller/apicontroller.php +++ /dev/null @@ -1,94 +0,0 @@ - - * @author Bernhard Posselt - * @copyright Alessandro Cosentino 2012 - * @copyright Bernhard Posselt 2012, 2014 - */ - -namespace OCA\News\Controller; - -use \OCP\IRequest; -use \OCP\IConfig; -use \OCP\AppFramework\Controller; -use \OCP\AppFramework\Http; -use \OCP\AppFramework\Http\JSONResponse; -use \OCP\AppFramework\Http\Response; - -use \OCA\News\Utility\Updater; - -class ApiController extends Controller { - - private $updater; - private $settings; - - public function __construct($appName, IRequest $request, Updater $updater, - IConfig $settings){ - parent::__construct($appName, $request); - $this->updater = $updater; - $this->settings = $settings; - } - - - /** - * @NoAdminRequired - * @NoCSRFRequired - * @API - */ - public function version() { - $version = $this->settings->getAppValue($this->appName, - 'installed_version'); - $response = new JSONResponse(array('version' => $version)); - return $response; - } - - - /** - * @NoCSRFRequired - */ - public function beforeUpdate() { - $this->updater->beforeUpdate(); - return new JSONResponse(); - } - - - /** - * @NoCSRFRequired - */ - public function afterUpdate() { - $this->updater->afterUpdate(); - return new JSONResponse(); - } - - - /** - * @NoAdminRequired - * @NoCSRFRequired - * @PublicPage - */ - public function cors() { - // needed for webapps access due to cross origin request policy - if(isset($this->request->server['HTTP_ORIGIN'])) { - $origin = $this->request->server['HTTP_ORIGIN']; - } else { - $origin = '*'; - } - - $response = new Response(); - $response->addHeader('Access-Control-Allow-Origin', $origin); - $response->addHeader('Access-Control-Allow-Methods', - 'PUT, POST, GET, DELETE'); - $response->addHeader('Access-Control-Allow-Credentials', 'false'); - $response->addHeader('Access-Control-Max-Age', '1728000'); - $response->addHeader('Access-Control-Allow-Headers', - 'Authorization, Content-Type'); - return $response; - } - - -} diff --git a/controller/feedapicontroller.php b/controller/feedapicontroller.php index 05c50543e..883732c87 100644 --- a/controller/feedapicontroller.php +++ b/controller/feedapicontroller.php @@ -14,11 +14,11 @@ namespace OCA\News\Controller; use \OCP\IRequest; -use \OCP\AppFramework\Controller; +use \OCP\ILogger; +use \OCP\AppFramework\ApiController; use \OCP\AppFramework\Http; use \OCP\AppFramework\Http\JSONResponse; -use \OCA\News\Core\Logger; use \OCA\News\BusinessLayer\FeedBusinessLayer; use \OCA\News\BusinessLayer\FolderBusinessLayer; use \OCA\News\BusinessLayer\ItemBusinessLayer; @@ -26,34 +26,37 @@ use \OCA\News\BusinessLayer\BusinessLayerException; use \OCA\News\BusinessLayer\BusinessLayerConflictException; -class FeedApiController extends Controller { +class FeedApiController extends ApiController { private $itemBusinessLayer; private $feedBusinessLayer; private $folderBusinessLayer; private $userId; private $logger; + private $loggerParams; public function __construct($appName, IRequest $request, FolderBusinessLayer $folderBusinessLayer, FeedBusinessLayer $feedBusinessLayer, ItemBusinessLayer $itemBusinessLayer, - Logger $logger, - $userId){ + ILogger $logger, + $userId, + $loggerParams){ parent::__construct($appName, $request); $this->folderBusinessLayer = $folderBusinessLayer; $this->feedBusinessLayer = $feedBusinessLayer; $this->itemBusinessLayer = $itemBusinessLayer; $this->userId = $userId; $this->logger = $logger; + $this->loggerParams = $loggerParams; } /** * @NoAdminRequired * @NoCSRFRequired - * @API + * @CORS */ public function index() { @@ -62,56 +65,66 @@ class FeedApiController extends Controller { 'starredCount' => $this->itemBusinessLayer->starredCount($this->userId) ); - foreach ($this->feedBusinessLayer->findAll($this->userId) as $feed) { + $feeds = $this->feedBusinessLayer->findAll($this->userId); + foreach ($feeds as $feed) { array_push($result['feeds'], $feed->toAPI()); } // check case when there are no items try { - $result['newestItemId'] = - $this->itemBusinessLayer->getNewestItemId($this->userId); + $result['newestItemId'] = $this->itemBusinessLayer + ->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($result); + return $result; } /** * @NoAdminRequired * @NoCSRFRequired - * @API + * @CORS + * + * @param string $url + * @param int $folderId */ - public function create() { - $feedUrl = $this->params('url'); - $folderId = (int) $this->params('folderId', 0); - + public function create($url, $folderId=0) { try { $this->feedBusinessLayer->purgeDeleted($this->userId, false); - $feed = $this->feedBusinessLayer->create($feedUrl, $folderId, $this->userId); + $feed = $this->feedBusinessLayer->create($url, $folderId, + $this->userId); $result = array( 'feeds' => array($feed->toAPI()) ); try { - $result['newestItemId'] = - $this->itemBusinessLayer->getNewestItemId($this->userId); + $result['newestItemId'] = $this->itemBusinessLayer + ->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($result); + return $result; } catch(BusinessLayerConflictException $ex) { - return new JSONResponse(array('message' => $ex->getMessage()), - Http::STATUS_CONFLICT); + + return new JSONResponse( + array('message' => $ex->getMessage()), + Http::STATUS_CONFLICT + ); + } catch(BusinessLayerException $ex) { - return new JSONResponse(array('message' => $ex->getMessage()), - Http::STATUS_NOT_FOUND); + + return new JSONResponse( + array('message' => $ex->getMessage()), + Http::STATUS_NOT_FOUND + ); + } } @@ -119,17 +132,18 @@ class FeedApiController extends Controller { /** * @NoAdminRequired * @NoCSRFRequired - * @API + * @CORS + * + * @param int $feedId */ - public function delete() { - $feedId = (int) $this->params('feedId'); - + public function delete($feedId) { try { $this->feedBusinessLayer->delete($feedId, $this->userId); - return new JSONResponse(); } catch(BusinessLayerException $ex) { - return new JSONResponse(array('message' => $ex->getMessage()), - Http::STATUS_NOT_FOUND); + return new JSONResponse( + array('message' => $ex->getMessage()), + Http::STATUS_NOT_FOUND + ); } } @@ -137,21 +151,20 @@ class FeedApiController extends Controller { /** * @NoAdminRequired * @NoCSRFRequired - * @API + * @CORS */ public function read() { $feedId = (int) $this->params('feedId'); $newestItemId = (int) $this->params('newestItemId'); $this->itemBusinessLayer->readFeed($feedId, $newestItemId, $this->userId); - return new JSONResponse(); } /** * @NoAdminRequired * @NoCSRFRequired - * @API + * @CORS */ public function move() { $feedId = (int) $this->params('feedId'); @@ -159,7 +172,6 @@ class FeedApiController extends Controller { try { $this->feedBusinessLayer->move($feedId, $folderId, $this->userId); - return new JSONResponse(); } catch(BusinessLayerException $ex) { return new JSONResponse(array('message' => $ex->getMessage()), Http::STATUS_NOT_FOUND); @@ -170,7 +182,7 @@ class FeedApiController extends Controller { /** * @NoAdminRequired * @NoCSRFRequired - * @API + * @CORS */ public function rename() { $feedId = (int) $this->params('feedId'); @@ -178,7 +190,6 @@ class FeedApiController extends Controller { try { $this->feedBusinessLayer->rename($feedId, $feedTitle, $this->userId); - return new JSONResponse(); } catch(BusinessLayerException $ex) { return new JSONResponse(array('message' => $ex->getMessage()), Http::STATUS_NOT_FOUND); @@ -188,7 +199,7 @@ class FeedApiController extends Controller { /** * @NoCSRFRequired - * @API + * @CORS */ public function fromAllUsers() { $feeds = $this->feedBusinessLayer->findAllFromAllUsers(); @@ -216,11 +227,9 @@ class FeedApiController extends Controller { $this->feedBusinessLayer->update($feedId, $userId); // ignore update failure (feed could not be reachable etc, we dont care) } catch(\Exception $ex) { - $this->logger->log('Could not update feed ' . $ex->getMessage(), - 'debug'); + $this->logger->debug('Could not update feed ' . $ex->getMessage(), + $this->loggerParams); } - return new JSONResponse(); - } diff --git a/controller/feedcontroller.php b/controller/feedcontroller.php index 56c7d5af8..02e523dff 100644 --- a/controller/feedcontroller.php +++ b/controller/feedcontroller.php @@ -167,7 +167,6 @@ class FeedController extends Controller { try { $this->feedBusinessLayer->markDeleted($feedId, $this->userId); - return new JSONResponse(); } catch(BusinessLayerException $ex) { return new JSONResponse(array( 'msg' => $ex->getMessage() @@ -215,7 +214,6 @@ class FeedController extends Controller { try { $this->feedBusinessLayer->move($feedId, $parentFolderId, $this->userId); - return new JSONResponse(); } catch(BusinessLayerException $ex) { return new JSONResponse(array( 'msg' => $ex->getMessage() @@ -232,7 +230,6 @@ class FeedController extends Controller { try { $this->feedBusinessLayer->rename($feedId, $feedTitle, $this->userId); - return new JSONResponse(); } catch(BusinessLayerException $ex) { return new JSONResponse(array( 'msg' => $ex->getMessage() @@ -286,7 +283,6 @@ class FeedController extends Controller { try { $this->feedBusinessLayer->unmarkDeleted($feedId, $this->userId); - return new JSONResponse(); } catch(BusinessLayerException $ex) { return new JSONResponse(array( 'msg' => $ex->getMessage() diff --git a/controller/folderapicontroller.php b/controller/folderapicontroller.php index 4e566541c..40ffc1389 100644 --- a/controller/folderapicontroller.php +++ b/controller/folderapicontroller.php @@ -14,7 +14,7 @@ namespace OCA\News\Controller; use \OCP\IRequest; -use \OCP\AppFramework\Controller; +use \OCP\AppFramework\ApiController; use \OCP\AppFramework\Http; use \OCP\AppFramework\Http\JSONResponse; @@ -25,7 +25,7 @@ use \OCA\News\BusinessLayer\BusinessLayerConflictException; use \OCA\News\BusinessLayer\BusinessLayerValidationException; -class FolderApiController extends Controller { +class FolderApiController extends ApiController { private $folderBusinessLayer; private $itemBusinessLayer; @@ -46,7 +46,7 @@ class FolderApiController extends Controller { /** * @NoAdminRequired * @NoCSRFRequired - * @API + * @CORS */ public function index() { $result = array( @@ -64,7 +64,7 @@ class FolderApiController extends Controller { /** * @NoAdminRequired * @NoCSRFRequired - * @API + * @CORS */ public function create() { $folderName = $this->params('name'); @@ -93,14 +93,13 @@ class FolderApiController extends Controller { /** * @NoAdminRequired * @NoCSRFRequired - * @API + * @CORS */ public function delete() { $folderId = (int) $this->params('folderId'); try { $this->folderBusinessLayer->delete($folderId, $this->userId); - return new JSONResponse(); } catch(BusinessLayerException $ex) { return new JSONResponse(array('message' => $ex->getMessage()), Http::STATUS_NOT_FOUND); @@ -111,7 +110,7 @@ class FolderApiController extends Controller { /** * @NoAdminRequired * @NoCSRFRequired - * @API + * @CORS */ public function update() { $folderId = (int) $this->params('folderId'); @@ -119,7 +118,6 @@ class FolderApiController extends Controller { try { $this->folderBusinessLayer->rename($folderId, $folderName, $this->userId); - return new JSONResponse(); } catch(BusinessLayerValidationException $ex) { return new JSONResponse(array('message' => $ex->getMessage()), @@ -139,14 +137,13 @@ class FolderApiController extends Controller { /** * @NoAdminRequired * @NoCSRFRequired - * @API + * @CORS */ public function read() { $folderId = (int) $this->params('folderId'); $newestItemId = (int) $this->params('newestItemId'); $this->itemBusinessLayer->readFolder($folderId, $newestItemId, $this->userId); - return new JSONResponse(); } diff --git a/controller/foldercontroller.php b/controller/foldercontroller.php index caa07f4d2..76476dd7a 100644 --- a/controller/foldercontroller.php +++ b/controller/foldercontroller.php @@ -27,12 +27,15 @@ use \OCA\News\BusinessLayer\BusinessLayerValidationException; class FolderController extends Controller { + use JSONHttpError; + private $folderBusinessLayer; private $feedBusinessLayer; private $itemBusinessLayer; private $userId; - public function __construct($appName, IRequest $request, + public function __construct($appName, + IRequest $request, FolderBusinessLayer $folderBusinessLayer, FeedBusinessLayer $feedBusinessLayer, ItemBusinessLayer $itemBusinessLayer, @@ -53,76 +56,64 @@ class FolderController extends Controller { $result = array( 'folders' => $folders ); - return new JSONResponse($result); + return $result; } - private function setOpened($isOpened){ - $folderId = (int) $this->params('folderId'); - + private function setOpened($isOpened, $folderId){ $this->folderBusinessLayer->open($folderId, $isOpened, $this->userId); } /** * @NoAdminRequired + * + * @param int $folderId */ - public function open(){ + public function open($folderId){ try { - $this->setOpened(true); - return new JSONResponse(); + $this->setOpened(true, $folderId); } catch(BusinessLayerException $ex) { - return new JSONResponse(array( - 'msg' => $ex->getMessage() - ), Http::STATUS_NOT_FOUND); + return $this->error($ex, Http::STATUS_NOT_FOUND); } } /** * @NoAdminRequired + * + * @param int $folderId */ - public function collapse(){ + public function collapse($folderId){ try { - $this->setOpened(false); - return new JSONResponse(); + $this->setOpened(false, $folderId); } catch(BusinessLayerException $ex) { - return new JSONResponse(array( - 'msg' => $ex->getMessage() - ), Http::STATUS_NOT_FOUND); + return $this->error($ex, Http::STATUS_NOT_FOUND); } } /** * @NoAdminRequired + * + * @param string $folderName */ - public function create(){ - $folderName = $this->params('folderName'); - + public function create($folderName){ try { // we need to purge deleted folders if a folder is created to // prevent already exists exceptions $this->folderBusinessLayer->purgeDeleted($this->userId, false); - $folder = $this->folderBusinessLayer->create($folderName, $this->userId); $params = array( 'folders' => array($folder) ); - return new JSONResponse($params); - - + return $params; } catch(BusinessLayerConflictException $ex) { - return new JSONResponse(array( - 'msg' => $ex->getMessage() - ), Http::STATUS_CONFLICT); - + return $this->error($ex, Http::STATUS_CONFLICT); } catch(BusinessLayerValidationException $ex) { - return new JSONResponse(array( - 'msg' => $ex->getMessage() - ), Http::STATUS_UNPROCESSABLE_ENTITY); + return $this->error($ex, Http::STATUS_UNPROCESSABLE_ENTITY); } } @@ -130,17 +121,14 @@ class FolderController extends Controller { /** * @NoAdminRequired + * + * @param int $folderId */ - public function delete(){ - $folderId = (int) $this->params('folderId'); - + public function delete($folderId){ try { $this->folderBusinessLayer->markDeleted($folderId, $this->userId); - return new JSONResponse(); } catch (BusinessLayerException $ex){ - return new JSONResponse(array( - 'msg' => $ex->getMessage() - ), Http::STATUS_NOT_FOUND); + return $this->error($ex, Http::STATUS_NOT_FOUND); } } @@ -202,7 +190,6 @@ class FolderController extends Controller { try { $this->folderBusinessLayer->unmarkDeleted($folderId, $this->userId); - return new JSONResponse(); } catch (BusinessLayerException $ex){ return new JSONResponse(array( 'msg' => $ex->getMessage() diff --git a/controller/itemapicontroller.php b/controller/itemapicontroller.php index 533fb2032..767023877 100644 --- a/controller/itemapicontroller.php +++ b/controller/itemapicontroller.php @@ -14,14 +14,16 @@ namespace OCA\News\Controller; use \OCP\IRequest; -use \OCP\AppFramework\Controller; +use \OCP\AppFramework\ApiController; use \OCP\AppFramework\Http; use \OCP\AppFramework\Http\JSONResponse; use \OCA\News\BusinessLayer\ItemBusinessLayer; use \OCA\News\BusinessLayer\BusinessLayerException; -class ItemApiController extends Controller { +class ItemApiController extends ApiController { + + use JSONHttpError; private $itemBusinessLayer; private $userId; @@ -39,25 +41,19 @@ class ItemApiController extends Controller { /** * @NoAdminRequired * @NoCSRFRequired - * @API + * @CORS + * + * @param int $type + * @param int $id + * @param bool $getRead + * @param int $batchSize + * @param int $offset */ - public function index() { + public function index($type, $id, $getRead, $batchSize=20, $offset=0) { $result = array( 'items' => array() ); - $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, @@ -71,24 +67,24 @@ class ItemApiController extends Controller { array_push($result['items'], $item->toAPI()); } - return new JSONResponse($result); + return $result; } /** * @NoAdminRequired * @NoCSRFRequired - * @API + * @CORS + * + * @param int $type + * @param int $id + * @param int $lastModified */ - public function updated() { + public function updated($type, $id, $lastModified=0) { $result = array( 'items' => array() ); - $lastModified = (int) $this->params('lastModified', 0); - $type = (int) $this->params('type'); - $id = (int) $this->params('id'); - $items = $this->itemBusinessLayer->findAllNew( $id, $type, @@ -101,31 +97,24 @@ class ItemApiController extends Controller { array_push($result['items'], $item->toAPI()); } - return new JSONResponse($result); + $result; } - private function setRead($isRead) { - $itemId = (int) $this->params('itemId'); + private function setRead($isRead, $itemId) { try { $this->itemBusinessLayer->read($itemId, $isRead, $this->userId); - return new JSONResponse(); } catch(BusinessLayerException $ex){ - return new JSONResponse(array('message' => $ex->getMessage()), - Http::STATUS_NOT_FOUND); + return $this->error($ex, Http::STATUS_NOT_FOUND); } } - private function setStarred($isStarred) { - $feedId = (int) $this->params('feedId'); - $guidHash = $this->params('guidHash'); + private function setStarred($isStarred, $feedId, $guidHash) { try { $this->itemBusinessLayer->star($feedId, $guidHash, $isStarred, $this->userId); - return new JSONResponse(); } catch(BusinessLayerException $ex){ - return new JSONResponse(array('message' => $ex->getMessage()), - Http::STATUS_NOT_FOUND); + return $this->error($ex, Http::STATUS_NOT_FOUND); } } @@ -133,59 +122,66 @@ class ItemApiController extends Controller { /** * @NoAdminRequired * @NoCSRFRequired - * @API + * @CORS + * + * @param int $itemId */ - public function read() { - return $this->setRead(true); + public function read($itemId) { + return $this->setRead(true, $itemId); } /** * @NoAdminRequired * @NoCSRFRequired - * @API + * @CORS + * + * @param int $itemId */ - public function unread() { - return $this->setRead(false); + public function unread($itemId) { + return $this->setRead(false, $itemId); } /** * @NoAdminRequired * @NoCSRFRequired - * @API + * @CORS + * + * @param int $feedId + * @param string $guidHash */ - public function star() { - return $this->setStarred(true); + public function star($feedId, $guidHash) { + return $this->setStarred(true, $feedId, $guidHash); } /** * @NoAdminRequired * @NoCSRFRequired - * @API + * @CORS + * + * @param int $feedId + * @param string $guidHash */ - public function unstar() { - return $this->setStarred(false); + public function unstar($feedId, $guidHash) { + return $this->setStarred(false, $feedId, $guidHash); } /** * @NoAdminRequired * @NoCSRFRequired - * @API + * @CORS + * + * @param int $newestItemId */ - public function readAll() { - $newestItemId = (int) $this->params('newestItemId'); - + public function readAll($newestItemId) { $this->itemBusinessLayer->readAll($newestItemId, $this->userId); - return new JSONResponse(); } - private function setMultipleRead($isRead) { - $items = $this->params('items'); - + private function setMultipleRead($isRead, $items) { foreach($items as $id) { try { $this->itemBusinessLayer->read($id, $isRead, $this->userId); @@ -193,34 +189,34 @@ class ItemApiController extends Controller { continue; } } - - return new JSONResponse(); } /** * @NoAdminRequired * @NoCSRFRequired - * @API + * @CORS + * + * @param int[] item ids */ - public function readMultiple() { - return $this->setMultipleRead(true); + public function readMultiple($items) { + return $this->setMultipleRead(true, $items); } /** * @NoAdminRequired * @NoCSRFRequired - * @API + * @CORS + * + * @param int[] item ids */ - public function unreadMultiple() { - return $this->setMultipleRead(false); + public function unreadMultiple($items) { + return $this->setMultipleRead(false, $items); } - private function setMultipleStarred($isStarred) { - $items = $this->params('items'); - + private function setMultipleStarred($isStarred, $items) { foreach($items as $item) { try { $this->itemBusinessLayer->star($item['feedId'], @@ -229,28 +225,30 @@ class ItemApiController extends Controller { continue; } } - - return new JSONResponse(); } /** * @NoAdminRequired * @NoCSRFRequired - * @API + * @CORS + * + * @param int[] item ids */ - public function starMultiple() { - return $this->setMultipleStarred(true); + public function starMultiple($items) { + return $this->setMultipleStarred(true, $items); } /** * @NoAdminRequired * @NoCSRFRequired - * @API + * @CORS + * + * @param int[] item ids */ - public function unstarMultiple() { - return $this->setMultipleStarred(false); + public function unstarMultiple($items) { + return $this->setMultipleStarred(false, $items); } diff --git a/controller/itemcontroller.php b/controller/itemcontroller.php index 99f308be7..b7836fdaa 100644 --- a/controller/itemcontroller.php +++ b/controller/itemcontroller.php @@ -82,7 +82,7 @@ class ItemController extends Controller { // in that case just return an empty array } catch(BusinessLayerException $ex) {} - return new JSONResponse($params); + return $params; } @@ -109,7 +109,7 @@ class ItemController extends Controller { // in that case just return an empty array } catch(BusinessLayerException $ex) {} - return new JSONResponse($params); + return new $params; } @@ -127,7 +127,6 @@ class ItemController extends Controller { public function star(){ try { $this->setStarred(true); - return new JSONResponse(); } catch(BusinessLayerException $ex) { return new JSONResponse(array( 'msg' => $ex->getMessage() @@ -142,7 +141,6 @@ class ItemController extends Controller { public function unstar(){ try { $this->setStarred(false); - return new JSONResponse(); } catch(BusinessLayerException $ex) { return new JSONResponse(array( 'msg' => $ex->getMessage() @@ -164,7 +162,6 @@ class ItemController extends Controller { public function read(){ try { $this->setRead(true); - return new JSONResponse(); } catch(BusinessLayerException $ex) { return new JSONResponse(array( 'msg' => $ex->getMessage() @@ -179,7 +176,6 @@ class ItemController extends Controller { public function unread(){ try { $this->setRead(false); - return new JSONResponse(); } catch(BusinessLayerException $ex) { return new JSONResponse(array( 'msg' => $ex->getMessage() diff --git a/controller/jsonhttperror.php b/controller/jsonhttperror.php new file mode 100644 index 000000000..5948a8a63 --- /dev/null +++ b/controller/jsonhttperror.php @@ -0,0 +1,36 @@ + + * @author Bernhard Posselt + * @copyright Alessandro Cosentino 2012 + * @copyright Bernhard Posselt 2012, 2014 + */ + +namespace OCA\News\Controller; + +use \OCP\AppFramework\Http\JSONResponse; + + +trait JSONHttpError { + + + /** + * @param \Excpetion $exception the message that is returned taken from the + * exception + * @param int the http error code + * @return \OCP\AppFramework\Http\JSONResponse + */ + protected function error($exception, $code) { + return new JSONResponse( + array('message' => $exception->getMessage()), + $code + ); + } + + +} \ No newline at end of file diff --git a/controller/pagecontroller.php b/controller/pagecontroller.php index fe6fe967a..5a9c592f5 100644 --- a/controller/pagecontroller.php +++ b/controller/pagecontroller.php @@ -15,6 +15,7 @@ namespace OCA\News\Controller; use \OCP\IRequest; use \OCP\IConfig; +use \OCP\IL10N; use \OCP\AppFramework\Http\JSONResponse; use \OCP\AppFramework\Controller; @@ -24,8 +25,11 @@ class PageController extends Controller { private $l10n; private $userId; - public function __construct($appName, IRequest $request, IConfig $settings, - $l10n, $userId){ + public function __construct($appName, + IRequest $request, + IConfig $settings, + IL10N $l10n, + $userId){ parent::__construct($appName, $request); $this->settings = $settings; $this->l10n = $l10n; @@ -58,28 +62,25 @@ class PageController extends Controller { 'language' => $language ); - return new JSONResponse($settings); + return $settings; } /** * @NoAdminRequired + * + * */ - public function updateSettings() { - $isShowAll = $this->params('showAll', null); - $isCompact = $this->params('compact', null); - - if($isShowAll !== null) { + public function updateSettings($showAll, $compact) { + if($showAll !== null) { $this->settings->setUserValue($this->userId, $this->appName, - 'showAll', $isShowAll); + 'showAll', $showAll); } - if($isCompact !== null) { + if($compact !== null) { $this->settings->setUserValue($this->userId, $this->appName, - 'compact', $isCompact); + 'compact', $compact); } - - return new JSONResponse(); } } \ No newline at end of file diff --git a/controller/utilityapicontroller.php b/controller/utilityapicontroller.php new file mode 100644 index 000000000..cb36b97b9 --- /dev/null +++ b/controller/utilityapicontroller.php @@ -0,0 +1,68 @@ + + * @author Bernhard Posselt + * @copyright Alessandro Cosentino 2012 + * @copyright Bernhard Posselt 2012, 2014 + */ + +namespace OCA\News\Controller; + +use \OCP\IRequest; +use \OCP\IConfig; +use \OCP\AppFramework\ApiController; +use \OCP\AppFramework\Http; +use \OCP\AppFramework\Http\JSONResponse; +use \OCP\AppFramework\Http\Response; + +use \OCA\News\Utility\Updater; + +class UtilityApiController extends ApiController { + + private $updater; + private $settings; + + public function __construct($appName, + IRequest $request, + Updater $updater, + IConfig $settings){ + parent::__construct($appName, $request); + $this->updater = $updater; + $this->settings = $settings; + } + + + /** + * @NoAdminRequired + * @NoCSRFRequired + * @API + */ + public function version() { + $version = $this->settings->getAppValue($this->appName, + 'installed_version'); + return array('version' => $version); + } + + + /** + * @NoCSRFRequired + */ + public function beforeUpdate() { + $this->updater->beforeUpdate(); + } + + + /** + * @NoCSRFRequired + */ + public function afterUpdate() { + $this->updater->afterUpdate(); + } + + +} -- cgit v1.2.3