summaryrefslogtreecommitdiffstats
path: root/controller
diff options
context:
space:
mode:
authorBernhard Posselt <dev@bernhard-posselt.com>2014-05-13 21:57:44 +0200
committerBernhard Posselt <dev@bernhard-posselt.com>2014-05-13 21:57:44 +0200
commit066b4b5a22a89fe3b38d37f09153090091928cd5 (patch)
treee01b7eaf641314259de1a54f146fa3e5d582ce02 /controller
parentfb85040af65e77e6f4b2e596e259b38b510a8b60 (diff)
move to new controller layout
Diffstat (limited to 'controller')
-rw-r--r--controller/entityapiserializer.php44
-rw-r--r--controller/feedapicontroller.php62
-rw-r--r--controller/feedcontroller.php103
-rw-r--r--controller/folderapicontroller.php69
-rw-r--r--controller/foldercontroller.php57
-rw-r--r--controller/itemapicontroller.php44
-rw-r--r--controller/itemcontroller.php86
-rw-r--r--controller/pagecontroller.php8
-rw-r--r--controller/utilityapicontroller.php1
9 files changed, 214 insertions, 260 deletions
diff --git a/controller/entityapiserializer.php b/controller/entityapiserializer.php
new file mode 100644
index 000000000..ef5e20fbe
--- /dev/null
+++ b/controller/entityapiserializer.php
@@ -0,0 +1,44 @@
+<?php
+/**
+ * ownCloud - News
+ *
+ * This file is licensed under the Affero General Public License version 3 or
+ * later. See the COPYING file.
+ *
+ * @author Alessandro Cosentino <cosenal@gmail.com>
+ * @author Bernhard Posselt <dev@bernhard-posselt.com>
+ * @copyright Alessandro Cosentino 2012
+ * @copyright Bernhard Posselt 2012, 2014
+ */
+
+namespace OCA\News\Controller;
+
+use \OCP\AppFramework\Http\IResponseSerializer;
+
+class EntityApiSeralizer implements IResponseSerializer {
+
+
+ public function __construct($level) {
+ $this->level = $level;
+ }
+
+
+ /**
+ * Wrap a list of entities in an array with $level as index and serialize
+ * them using the toAPI method
+ */
+ public function serialize($data) {
+ if(!is_array($data)) {
+ $data = array($data);
+ }
+
+ $response = array(
+ $this->level => array();
+ );
+
+ foreach($data as $entity) {
+ $response[$this->level][] = $entity->toAPI()
+ }
+ }
+
+} \ No newline at end of file
diff --git a/controller/feedapicontroller.php b/controller/feedapicontroller.php
index 883732c87..b326a1825 100644
--- a/controller/feedapicontroller.php
+++ b/controller/feedapicontroller.php
@@ -28,6 +28,8 @@ use \OCA\News\BusinessLayer\BusinessLayerConflictException;
class FeedApiController extends ApiController {
+ use JSONHttpError;
+
private $itemBusinessLayer;
private $feedBusinessLayer;
private $folderBusinessLayer;
@@ -66,6 +68,7 @@ class FeedApiController extends ApiController {
);
$feeds = $this->feedBusinessLayer->findAll($this->userId);
+
foreach ($feeds as $feed) {
array_push($result['feeds'], $feed->toAPI());
}
@@ -112,19 +115,9 @@ class FeedApiController extends ApiController {
return $result;
} catch(BusinessLayerConflictException $ex) {
-
- return new JSONResponse(
- array('message' => $ex->getMessage()),
- Http::STATUS_CONFLICT
- );
-
+ return $this->error($ex, Http::STATUS_CONFLICT);
} catch(BusinessLayerException $ex) {
-
- return new JSONResponse(
- array('message' => $ex->getMessage()),
- Http::STATUS_NOT_FOUND
- );
-
+ return $this->error($ex, Http::STATUS_NOT_FOUND);
}
}
@@ -140,10 +133,7 @@ class FeedApiController extends ApiController {
try {
$this->feedBusinessLayer->delete($feedId, $this->userId);
} catch(BusinessLayerException $ex) {
- return new JSONResponse(
- array('message' => $ex->getMessage()),
- Http::STATUS_NOT_FOUND
- );
+ return $this->error($ex, Http::STATUS_NOT_FOUND);
}
}
@@ -152,11 +142,11 @@ class FeedApiController extends ApiController {
* @NoAdminRequired
* @NoCSRFRequired
* @CORS
+ *
+ * @param int $feedId
+ * @param int $newestItemId
*/
- public function read() {
- $feedId = (int) $this->params('feedId');
- $newestItemId = (int) $this->params('newestItemId');
-
+ public function read($feedId, $newestItemId) {
$this->itemBusinessLayer->readFeed($feedId, $newestItemId, $this->userId);
}
@@ -165,16 +155,15 @@ class FeedApiController extends ApiController {
* @NoAdminRequired
* @NoCSRFRequired
* @CORS
+ *
+ * @param int $feedId
+ * @param int $folderId
*/
- public function move() {
- $feedId = (int) $this->params('feedId');
- $folderId = (int) $this->params('folderId');
-
+ public function move($feedId, $folderId) {
try {
$this->feedBusinessLayer->move($feedId, $folderId, $this->userId);
} catch(BusinessLayerException $ex) {
- return new JSONResponse(array('message' => $ex->getMessage()),
- Http::STATUS_NOT_FOUND);
+ return $this->error($ex, Http::STATUS_NOT_FOUND);
}
}
@@ -183,16 +172,15 @@ class FeedApiController extends ApiController {
* @NoAdminRequired
* @NoCSRFRequired
* @CORS
+ *
+ * @param int $feedId
+ * @param string $feedTitle
*/
- public function rename() {
- $feedId = (int) $this->params('feedId');
- $feedTitle = $this->params('feedTitle');
-
+ public function rename($feedId, $feedTitle) {
try {
$this->feedBusinessLayer->rename($feedId, $feedTitle, $this->userId);
} catch(BusinessLayerException $ex) {
- return new JSONResponse(array('message' => $ex->getMessage()),
- Http::STATUS_NOT_FOUND);
+ return $this->error($ex, Http::STATUS_NOT_FOUND);
}
}
@@ -212,17 +200,17 @@ class FeedApiController extends ApiController {
));
}
- return new JSONResponse($result);
+ return $result;
}
/**
* @NoCSRFRequired
+ *
+ * @param string $userId
+ * @param int $feedId
*/
- public function update() {
- $userId = $this->params('userId');
- $feedId = (int) $this->params('feedId');
-
+ public function update($userId, $feedId) {
try {
$this->feedBusinessLayer->update($feedId, $userId);
// ignore update failure (feed could not be reachable etc, we dont care)
diff --git a/controller/feedcontroller.php b/controller/feedcontroller.php
index 02e523dff..5489f555c 100644
--- a/controller/feedcontroller.php
+++ b/controller/feedcontroller.php
@@ -17,7 +17,6 @@ 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;
@@ -29,6 +28,8 @@ use \OCA\News\Db\FeedType;
class FeedController extends Controller {
+ use JSONHttpError;
+
private $feedBusinessLayer;
private $folderBusinessLayer;
private $itemBusinessLayer;
@@ -72,7 +73,7 @@ class FeedController extends Controller {
// simply ignore it and do not add the newestItemId
} catch (BusinessLayerException $ex) {}
- return new JSONResponse($params);
+ return $params;
}
@@ -108,24 +109,22 @@ class FeedController extends Controller {
$feedType = FeedType::SUBSCRIPTIONS;
}
- $params = array(
+ return array(
'activeFeed' => array(
'id' => $feedId,
'type' => $feedType
)
);
-
- return new JSONResponse($params);
}
/**
* @NoAdminRequired
+ *
+ * @param string $url
+ * @param int $parentFolderId
*/
- public function create(){
- $url = $this->params('url');
- $parentFolderId = (int) $this->params('parentFolderId');
-
+ public function create($url, $parentFolderId){
try {
// we need to purge deleted feeds if a feed is created to
// prevent already exists exceptions
@@ -144,47 +143,40 @@ class FeedController extends Controller {
// simply ignore it and do not add the newestItemId
} catch (BusinessLayerException $ex) {}
- 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(BusinessLayerException $ex) {
- return new JSONResponse(array(
- 'msg' => $ex->getMessage()
- ), Http::STATUS_UNPROCESSABLE_ENTITY);
+ return $this->error($ex, Http::STATUS_UNPROCESSABLE_ENTITY);
}
}
/**
* @NoAdminRequired
+ *
+ * @param int $feedId
*/
- public function delete(){
- $feedId = (int) $this->params('feedId');
-
+ public function delete($feedId){
try {
$this->feedBusinessLayer->markDeleted($feedId, $this->userId);
} 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 $feedId
*/
- public function update(){
+ public function update($feedId){
try {
- $feedId = (int) $this->params('feedId');
-
$feed = $this->feedBusinessLayer->update($feedId, $this->userId);
- $params = array(
+ return array(
'feeds' => array(
// only pass unreadcount to not accidentally readd
// the feed again
@@ -195,75 +187,69 @@ class FeedController extends Controller {
)
);
- return new JSONResponse($params);
-
} 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 $feedId
+ * @param int $parentFolderId
*/
- public function move(){
- $feedId = (int) $this->params('feedId');
- $parentFolderId = (int) $this->params('parentFolderId');
-
+ public function move($feedId, $parentFolderId){
try {
$this->feedBusinessLayer->move($feedId, $parentFolderId, $this->userId);
} 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 $feedId
+ * @param string $feedTitle
*/
public function rename() {
- $feedId = (int) $this->params('feedId');
- $feedTitle = $this->params('feedTitle');
-
try {
$this->feedBusinessLayer->rename($feedId, $feedTitle, $this->userId);
} catch(BusinessLayerException $ex) {
- return new JSONResponse(array(
- 'msg' => $ex->getMessage()
- ), Http::STATUS_NOT_FOUND);
+ return $this->error($ex, Http::STATUS_NOT_FOUND);
}
}
+
/**
* @NoAdminRequired
+ *
+ * @param array $json
*/
- public function import() {
- $json = $this->params('json');
-
+ public function import($json) {
$feed = $this->feedBusinessLayer->importArticles($json, $this->userId);
$params = array();
+
if($feed) {
$params['feeds'] = array($feed);
}
- return new JSONResponse($params);
+ return $params;
}
/**
* @NoAdminRequired
+ *
+ * @param int $feedId
+ * @param int $highestItemId
*/
- public function read(){
- $feedId = (int) $this->params('feedId');
- $highestItemId = (int) $this->params('highestItemId');
-
+ public function read($feedId, $highestItemId){
$this->itemBusinessLayer->readFeed($feedId, $highestItemId, $this->userId);
- $params = array(
+ return array(
'feeds' => array(
array(
'id' => $feedId,
@@ -271,22 +257,19 @@ class FeedController extends Controller {
)
)
);
- return new JSONResponse($params);
}
/**
* @NoAdminRequired
+ *
+ * @param int $feedId
*/
public function restore(){
- $feedId = (int) $this->params('feedId');
-
try {
$this->feedBusinessLayer->unmarkDeleted($feedId, $this->userId);
} catch(BusinessLayerException $ex) {
- return new JSONResponse(array(
- 'msg' => $ex->getMessage()
- ), Http::STATUS_NOT_FOUND);
+ return $this->error($ex, Http::STATUS_NOT_FOUND);
}
}
diff --git a/controller/folderapicontroller.php b/controller/folderapicontroller.php
index 40ffc1389..4e9f3e00c 100644
--- a/controller/folderapicontroller.php
+++ b/controller/folderapicontroller.php
@@ -27,6 +27,8 @@ use \OCA\News\BusinessLayer\BusinessLayerValidationException;
class FolderApiController extends ApiController {
+ use JSONHttpError;
+
private $folderBusinessLayer;
private $itemBusinessLayer;
private $userId;
@@ -49,15 +51,9 @@ class FolderApiController extends ApiController {
* @CORS
*/
public function index() {
- $result = array(
- 'folders' => array()
- );
-
- foreach ($this->folderBusinessLayer->findAll($this->userId) as $folder) {
- array_push($result['folders'], $folder->toAPI());
- }
+ $this->registerSerializer(new EntityApiSerializer('folders'));
- return new JSONResponse($result);
+ return $this->folderBusinessLayer->findAll($this->userId);
}
@@ -65,27 +61,21 @@ class FolderApiController extends ApiController {
* @NoAdminRequired
* @NoCSRFRequired
* @CORS
+ *
+ * @param string $name
*/
- public function create() {
- $folderName = $this->params('name');
- $result = array(
- 'folders' => array()
- );
-
+ public function create($name) {
try {
$this->folderBusinessLayer->purgeDeleted($this->userId, false);
$folder = $this->folderBusinessLayer->create($folderName, $this->userId);
- array_push($result['folders'], $folder->toAPI());
+
+ $this->registerSerializer(new EntityApiSerializer('folders'));
+ return $folder;
- return new JSONResponse($result);
-
} catch(BusinessLayerValidationException $ex) {
- return new JSONResponse(array('message' => $ex->getMessage()),
- Http::STATUS_UNPROCESSABLE_ENTITY);
-
+ return $this->error($ex, Http::STATUS_UNPROCESSABLE_ENTITY);
} catch(BusinessLayerConflictException $ex) {
- return new JSONResponse(array('message' => $ex->getMessage()),
- Http::STATUS_CONFLICT);
+ return $this->error($ex, Http::STATUS_CONFLICT);
}
}
@@ -94,15 +84,14 @@ class FolderApiController extends ApiController {
* @NoAdminRequired
* @NoCSRFRequired
* @CORS
+ *
+ * @param int $folderId
*/
- public function delete() {
- $folderId = (int) $this->params('folderId');
-
+ public function delete($folderId) {
try {
$this->folderBusinessLayer->delete($folderId, $this->userId);
} catch(BusinessLayerException $ex) {
- return new JSONResponse(array('message' => $ex->getMessage()),
- Http::STATUS_NOT_FOUND);
+ return $this->error($ex, Http::STATUS_NOT_FOUND);
}
}
@@ -111,25 +100,19 @@ class FolderApiController extends ApiController {
* @NoAdminRequired
* @NoCSRFRequired
* @CORS
+ * @param int $folderId
+ * @param string $name
*/
- public function update() {
- $folderId = (int) $this->params('folderId');
- $folderName = $this->params('name');
-
+ public function update($folderId, $name) {
try {
$this->folderBusinessLayer->rename($folderId, $folderName, $this->userId);
} catch(BusinessLayerValidationException $ex) {
- return new JSONResponse(array('message' => $ex->getMessage()),
- Http::STATUS_UNPROCESSABLE_ENTITY);
-
+ return $this->error($ex, Http::STATUS_UNPROCESSABLE_ENTITY);
} catch(BusinessLayerConflictException $ex) {
- return new JSONResponse(array('message' => $ex->getMessage()),
- Http::STATUS_CONFLICT);
-
+ return $this->error($ex, Http::STATUS_CONFLICT);
} catch(BusinessLayerException $ex) {
- return new JSONResponse(array('message' => $ex->getMessage()),
- Http::STATUS_NOT_FOUND);
+ return $this->error($ex, Http::STATUS_NOT_FOUND);
}
}
@@ -138,11 +121,11 @@ class FolderApiController extends ApiController {
* @NoAdminRequired
* @NoCSRFRequired
* @CORS
+ *
+ * @param int $folderId
+ * @param int $newestItemId
*/
- public function read() {
- $folderId = (int) $this->params('folderId');
- $newestItemId = (int) $this->params('newestItemId');
-
+ public function read($folderId, $newestItemId) {
$this->itemBusinessLayer->readFolder($folderId, $newestItemId, $this->userId);
}
diff --git a/controller/foldercontroller.php b/controller/foldercontroller.php
index 76476dd7a..9b0db2be7 100644
--- a/controller/foldercontroller.php
+++ b/controller/foldercontroller.php
@@ -16,7 +16,6 @@ namespace OCA\News\Controller;
use \OCP\IRequest;
use \OCP\AppFramework\Controller;
use \OCP\AppFramework\Http;
-use \OCP\AppFramework\Http\JSONResponse;
use \OCA\News\BusinessLayer\FolderBusinessLayer;
use \OCA\News\BusinessLayer\FeedBusinessLayer;
@@ -25,6 +24,7 @@ use \OCA\News\BusinessLayer\BusinessLayerException;
use \OCA\News\BusinessLayer\BusinessLayerConflictException;
use \OCA\News\BusinessLayer\BusinessLayerValidationException;
+
class FolderController extends Controller {
use JSONHttpError;
@@ -53,10 +53,9 @@ class FolderController extends Controller {
*/
public function index(){
$folders = $this->folderBusinessLayer->findAll($this->userId);
- $result = array(
+ return array(
'folders' => $folders
);
- return $result;
}
@@ -105,10 +104,9 @@ class FolderController extends Controller {
$this->folderBusinessLayer->purgeDeleted($this->userId, false);
$folder = $this->folderBusinessLayer->create($folderName, $this->userId);
- $params = array(
+ return array(
'folders' => array($folder)
);
- return $params;
} catch(BusinessLayerConflictException $ex) {
return $this->error($ex, Http::STATUS_CONFLICT);
@@ -135,67 +133,54 @@ class FolderController extends Controller {
/**
* @NoAdminRequired
+ *
+ * @param string $folderName
+ * @param int $folderId
*/
- public function rename(){
- $folderName = $this->params('folderName');
- $folderId = (int) $this->params('folderId');
-
+ public function rename($folderName, $folderId){
try {
$folder = $this->folderBusinessLayer->rename($folderId, $folderName,
$this->userId);
- $params = array(
+ return array(
'folders' => array($folder)
);
- return new JSONResponse($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);
} 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
+ * @param int $highestItemId
*/
- public function read(){
- $folderId = (int) $this->params('folderId');
- $highestItemId = (int) $this->params('highestItemId');
-
+ public function read($folderId, $highestItemId){
$this->itemBusinessLayer->readFolder($folderId, $highestItemId, $this->userId);
- $params = array(
+ return array(
'feeds' => $this->feedBusinessLayer->findAll($this->userId)
);
- return new JSONResponse($params);
}
/**
* @NoAdminRequired
+ *
+ * @param int $folderId
*/
- public function restore(){
- $folderId = (int) $this->params('folderId');
-
+ public function restore($folderId){
try {
$this->folderBusinessLayer->unmarkDeleted($folderId, $this->userId);
} catch (BusinessLayerException $ex){
- return new JSONResponse(array(
- 'msg' => $ex->getMessage()
- ), Http::STATUS_NOT_FOUND);
+ return $this->error($ex, Http::STATUS_NOT_FOUND);
}
-
}
diff --git a/controller/itemapicontroller.php b/controller/itemapicontroller.php
index 767023877..8cd8b5578 100644
--- a/controller/itemapicontroller.php
+++ b/controller/itemapicontroller.php
@@ -16,7 +16,6 @@ namespace OCA\News\Controller;
use \OCP\IRequest;
use \OCP\AppFramework\ApiController;
use \OCP\AppFramework\Http;
-use \OCP\AppFramework\Http\JSONResponse;
use \OCA\News\BusinessLayer\ItemBusinessLayer;
use \OCA\News\BusinessLayer\BusinessLayerException;
@@ -50,24 +49,10 @@ class ItemApiController extends ApiController {
* @param int $offset
*/
public function index($type, $id, $getRead, $batchSize=20, $offset=0) {
- $result = array(
- 'items' => array()
- );
-
- $items = $this->itemBusinessLayer->findAll(
- $id,
- $type,
- $batchSize,
- $offset,
- $showAll,
- $this->userId
- );
-
- foreach ($items as $item) {
- array_push($result['items'], $item->toAPI());
- }
+ $this->registerSerializer(new EntityApiSerializer('items'));
- return $result;
+ return $this->itemBusinessLayer->findAll($id, $type, $batchSize, $offset,
+ $showAll, $this->userId);
}
@@ -81,23 +66,10 @@ class ItemApiController extends ApiController {
* @param int $lastModified
*/
public function updated($type, $id, $lastModified=0) {
- $result = array(
- 'items' => array()
- );
-
- $items = $this->itemBusinessLayer->findAllNew(
- $id,
- $type,
- $lastModified,
- true,
- $this->userId
- );
-
- foreach ($items as $item) {
- array_push($result['items'], $item->toAPI());
- }
+ $this->registerSerializer(new EntityApiSerializer('items'));
- $result;
+ return $this->itemBusinessLayer->findAllNew($id, $type, $lastModified,
+ true, $this->userId);
}
@@ -219,8 +191,8 @@ class ItemApiController extends ApiController {
private function setMultipleStarred($isStarred, $items) {
foreach($items as $item) {
try {
- $this->itemBusinessLayer->star($item['feedId'],
- $item['guidHash'], $isStarred, $this->userId);
+ $this->itemBusinessLayer->star($item['feedId'], $item['guidHash'],
+ $isStarred, $this->userId);
} catch(BusinessLayerException $ex) {
continue;
}
diff --git a/controller/itemcontroller.php b/controller/itemcontroller.php
index b7836fdaa..3313f583b 100644
--- a/controller/itemcontroller.php
+++ b/controller/itemcontroller.php
@@ -17,7 +17,6 @@ use \OCP\IRequest;
use \OCP\IConfig;
use \OCP\AppFramework\Controller;
use \OCP\AppFramework\Http;
-use \OCP\AppFramework\Http\JSONResponse;
use \OCA\News\BusinessLayer\BusinessLayerException;
use \OCA\News\Busin