mirror of
https://github.com/chylex/Nextcloud-News.git
synced 2025-04-09 10:15:44 +02:00
use more flexible serializer that ignores nulls, non entity values and responses
This commit is contained in:
parent
db4c29e89d
commit
acc2df1251
@ -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()
|
||||
);
|
||||
|
||||
foreach($data as $entity) {
|
||||
$response[$this->level][] = $entity->toAPI();
|
||||
if($data instanceof IAPI) {
|
||||
return array(
|
||||
$this->level => array($data->toAPI())
|
||||
);
|
||||
}
|
||||
|
||||
return $response;
|
||||
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 $data;
|
||||
}
|
||||
|
||||
|
||||
private function convert($entities) {
|
||||
$converted = array();
|
||||
|
||||
foreach($entities as $entity) {
|
||||
if($entity instanceof IAPI) {
|
||||
$converted[] = $entity->toAPI();
|
||||
} else {
|
||||
$converted[] = $entity;
|
||||
}
|
||||
}
|
||||
|
||||
return $converted;
|
||||
}
|
||||
|
||||
}
|
@ -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;
|
||||
|
@ -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) {
|
||||
|
@ -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);
|
||||
}
|
||||
|
@ -15,6 +15,9 @@ namespace OCA\News\Controller;
|
||||
|
||||
require_once(__DIR__ . "/../../classloader.php");
|
||||
|
||||
|
||||
use \OCP\AppFramework\Http\Response;
|
||||
|
||||
use \OCA\News\Db\Item;
|
||||
|
||||
|
||||
@ -40,6 +43,7 @@ class EntityApiSerializerTest extends \PHPUnit_Framework_TestCase {
|
||||
$item2->setRead();
|
||||
|
||||
$serializer = new EntityApiSerializer('items');
|
||||
|
||||
$result = $serializer->serialize(array($item, $item2));
|
||||
|
||||
$this->assertTrue($result['items'][0]['unread']);
|
||||
@ -47,4 +51,51 @@ class EntityApiSerializerTest extends \PHPUnit_Framework_TestCase {
|
||||
}
|
||||
|
||||
|
||||
public function testResponseNoChange() {
|
||||
$response = new Response();
|
||||
$serializer = new EntityApiSerializer('items');
|
||||
|
||||
$result = $serializer->serialize($response);
|
||||
|
||||
$this->assertEquals($response, $result);
|
||||
}
|
||||
|
||||
|
||||
public function testCompleteArraysTransformed() {
|
||||
$item = new Item();
|
||||
$item->setUnread();
|
||||
|
||||
$item2 = new Item();
|
||||
$item2->setRead();
|
||||
|
||||
$serializer = new EntityApiSerializer('items');
|
||||
|
||||
$in = array(
|
||||
'items' => array($item, $item2),
|
||||
'test' => 1
|
||||
);
|
||||
|
||||
$result = $serializer->serialize($in);
|
||||
|
||||
$this->assertTrue($result['items'][0]['unread']);
|
||||
$this->assertFalse($result['items'][1]['unread']);
|
||||
$this->assertEquals(1, $result['test']);
|
||||
}
|
||||
|
||||
|
||||
public function noEntityNoChange() {
|
||||
$serializer = new EntityApiSerializer('items');
|
||||
|
||||
$in = array(
|
||||
'items' => array('hi', '2'),
|
||||
'test' => 1
|
||||
);
|
||||
|
||||
$result = $serializer->serialize($in);
|
||||
|
||||
$this->assertEquals('hi', $result['items'][0]);
|
||||
$this->assertEquals('2', $result['items'][1]);
|
||||
$this->assertEquals(1, $result['test']);
|
||||
}
|
||||
|
||||
}
|
@ -98,7 +98,7 @@ class FeedApiControllerTest extends \PHPUnit_Framework_TestCase {
|
||||
$response = $this->feedAPI->index();
|
||||
|
||||
$this->assertEquals(array(
|
||||
'feeds' => array($feeds[0]->toAPI()),
|
||||
'feeds' => $feeds,
|
||||
'starredCount' => $starredCount,
|
||||
'newestItemId' => $newestItemId
|
||||
), $response);
|
||||
@ -127,7 +127,7 @@ class FeedApiControllerTest extends \PHPUnit_Framework_TestCase {
|
||||
$response = $this->feedAPI->index();
|
||||
|
||||
$this->assertEquals(array(
|
||||
'feeds' => array($feeds[0]->toAPI()),
|
||||
'feeds' => $feeds,
|
||||
'starredCount' => $starredCount,
|
||||
), $response);
|
||||
}
|
||||
@ -179,7 +179,7 @@ class FeedApiControllerTest extends \PHPUnit_Framework_TestCase {
|
||||
$response = $this->feedAPI->create('url', 3);
|
||||
|
||||
$this->assertEquals(array(
|
||||
'feeds' => array($feeds[0]->toAPI()),
|
||||
'feeds' => $feeds,
|
||||
'newestItemId' => 3
|
||||
), $response);
|
||||
}
|
||||
@ -207,7 +207,7 @@ class FeedApiControllerTest extends \PHPUnit_Framework_TestCase {
|
||||
$response = $this->feedAPI->create('ho', 3);
|
||||
|
||||
$this->assertEquals(array(
|
||||
'feeds' => array($feeds[0]->toAPI())
|
||||
'feeds' => $feeds
|
||||
), $response);
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user