summaryrefslogtreecommitdiffstats
path: root/controller
diff options
context:
space:
mode:
authorBernhard Posselt <dev@bernhard-posselt.com>2014-05-14 02:16:31 +0200
committerBernhard Posselt <dev@bernhard-posselt.com>2014-05-14 02:16:31 +0200
commitacc2df1251a1c1b9ec5ede13bdf46d516dc64b0d (patch)
treece8503baa37cc05379dfd43a65e9acf0b41e0d97 /controller
parentdb4c29e89d77955c4930731ade08816f5567fe84 (diff)
use more flexible serializer that ignores nulls, non entity values and responses
Diffstat (limited to 'controller')
-rw-r--r--controller/entityapiserializer.php48
-rw-r--r--controller/feedapicontroller.php37
-rw-r--r--controller/folderapicontroller.php9
-rw-r--r--controller/itemapicontroller.php5
4 files changed, 55 insertions, 44 deletions
diff --git a/controller/entityapiserializer.php b/controller/entityapiserializer.php
index 8bd17ba29..b84006750 100644
--- a/controller/entityapiserializer.php
+++ b/controller/entityapiserializer.php
@@ -12,6 +12,9 @@
namespace OCA\News\Controller;
use \OCP\AppFramework\Http\IResponseSerializer;
+use \OCP\AppFramework\Http\Response;
+
+use \OCA\News\Db\IAPI;
class EntityApiSerializer implements IResponseSerializer {
@@ -23,24 +26,49 @@ class EntityApiSerializer implements IResponseSerializer {
/**
- * Wrap a list of entities in an array with $level as index and serialize
- * them using the toAPI method
+ * Call toAPI() method on all entities. Works on
+ * @param mixed $data:
+ * * Entity
+ * * Entity[]
+ * * array('level' => Entity[])
+ * * Response
*/
public function serialize($data) {
- if(!is_array($data)) {
- $data = array($data);
+
+ if($data === null || $data instanceof Response) {
+ return $data;
}
- $response = array(
- $this->level => array()
- );
+ if($data instanceof IAPI) {
+ return array(
+ $this->level => array($data->toAPI())
+ );
+ }
- foreach($data as $entity) {
- $response[$this->level][] = $entity->toAPI();
+ if(is_array($data) && array_key_exists($this->level, $data)) {
+ $data[$this->level] = $this->convert($data[$this->level]);
+ } elseif(is_array($data)) {
+ $data = array(
+ $this->level => $this->convert($data)
+ );
}
- return $response;
+ return $data;
}
+ private function convert($entities) {
+ $converted = array();
+
+ foreach($entities as $entity) {
+ if($entity instanceof IAPI) {
+ $converted[] = $entity->toAPI();
+ } else {
+ $converted[] = $entity;
+ }
+ }
+
+ return $converted;
+ }
+
} \ No newline at end of file
diff --git a/controller/feedapicontroller.php b/controller/feedapicontroller.php
index 7d68e5f56..a6f52f9ea 100644
--- a/controller/feedapicontroller.php
+++ b/controller/feedapicontroller.php
@@ -51,6 +51,7 @@ class FeedApiController extends ApiController {
$this->userId = $userId;
$this->logger = $logger;
$this->loggerParams = $loggerParams;
+ $this->registerSerializer(new EntityApiSerializer('feeds'));
}
@@ -63,22 +64,15 @@ class FeedApiController extends ApiController {
$result = array(
'feeds' => array(),
- 'starredCount' => $this->itemBusinessLayer->starredCount($this->userId)
+ 'starredCount' => $this->itemBusinessLayer->starredCount($this->userId),
+ 'feeds' => $this->feedBusinessLayer->findAll($this->userId)
);
- $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);
-
- // An exception occurs if there is a newest item. If there is none,
- // simply ignore it and do not add the newestItemId
+ $result['newestItemId'] = $this->itemBusinessLayer->getNewestItemId($this->userId);
+
+ // in case there are no items, ignore
} catch(BusinessLayerException $ex) {}
return $result;
@@ -97,18 +91,15 @@ class FeedApiController extends ApiController {
try {
$this->feedBusinessLayer->purgeDeleted($this->userId, false);
- $feed = $this->feedBusinessLayer->create($url, $folderId,
- $this->userId);
+ $feed = $this->feedBusinessLayer->create($url, $folderId, $this->userId);
$result = array(
- 'feeds' => array($feed->toAPI())
+ 'feeds' => array($feed)
);
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
+ // in case there are no items, ignore
} catch(BusinessLayerException $ex) {}
return $result;
@@ -193,10 +184,10 @@ class FeedApiController extends ApiController {
$result = array('feeds' => array());
foreach ($feeds as $feed) {
- array_push($result['feeds'], array(
- 'id' => $feed->getId(),
+ $result['feeds'][] = array(
+ 'id' => $feed->getId(),
'userId' => $feed->getUserId()
- ));
+ );
}
return $result;
diff --git a/controller/folderapicontroller.php b/controller/folderapicontroller.php
index 274264ccc..feb901204 100644
--- a/controller/folderapicontroller.php
+++ b/controller/folderapicontroller.php
@@ -41,6 +41,7 @@ class FolderApiController extends ApiController {
$this->folderBusinessLayer = $folderBusinessLayer;
$this->itemBusinessLayer = $itemBusinessLayer;
$this->userId = $userId;
+ $this->registerSerializer(new EntityApiSerializer('folders'));
}
@@ -50,8 +51,6 @@ class FolderApiController extends ApiController {
* @CORS
*/
public function index() {
- $this->registerSerializer(new EntityApiSerializer('folders'));
-
return $this->folderBusinessLayer->findAll($this->userId);
}
@@ -66,11 +65,7 @@ class FolderApiController extends ApiController {
public function create($name) {
try {
$this->folderBusinessLayer->purgeDeleted($this->userId, false);
- $folder = $this->folderBusinessLayer->create($name, $this->userId);
-
- $this->registerSerializer(new EntityApiSerializer('folders'));
- return $folder;
-
+ return $this->folderBusinessLayer->create($name, $this->userId);
} catch(BusinessLayerValidationException $ex) {
return $this->error($ex, Http::STATUS_UNPROCESSABLE_ENTITY);
} catch(BusinessLayerConflictException $ex) {
diff --git a/controller/itemapicontroller.php b/controller/itemapicontroller.php
index 9e4fb672c..8110c3ab0 100644
--- a/controller/itemapicontroller.php
+++ b/controller/itemapicontroller.php
@@ -34,6 +34,7 @@ class ItemApiController extends ApiController {
parent::__construct($appName, $request);
$this->itemBusinessLayer = $itemBusinessLayer;
$this->userId = $userId;
+ $this->registerSerializer(new EntityApiSerializer('items'));
}
@@ -49,8 +50,6 @@ class ItemApiController extends ApiController {
* @param int $offset
*/
public function index($type, $id, $getRead, $batchSize=20, $offset=0) {
- $this->registerSerializer(new EntityApiSerializer('items'));
-
return $this->itemBusinessLayer->findAll($id, $type, $batchSize, $offset,
$getRead, $this->userId);
}
@@ -66,8 +65,6 @@ class ItemApiController extends ApiController {
* @param int $lastModified
*/
public function updated($type, $id, $lastModified=0) {
- $this->registerSerializer(new EntityApiSerializer('items'));
-
return $this->itemBusinessLayer->findAllNew($id, $type, $lastModified,
true, $this->userId);
}