SAAS-1140: Paginate station podcast feed episodes
Added pagination to PodcastEpisodes controller
This commit is contained in:
parent
23ccc268d1
commit
1aee6ba97a
2 changed files with 111 additions and 6 deletions
|
@ -12,15 +12,47 @@ class Rest_PodcastEpisodesController extends Zend_Rest_Controller
|
|||
|
||||
public function indexAction()
|
||||
{
|
||||
// podcast ID
|
||||
$id = $this->getId();
|
||||
if (!$id) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
|
||||
$podcast = PodcastQuery::create()->findPk($id);
|
||||
if (!$podcast) {
|
||||
throw new PodcastNotFoundException();
|
||||
}
|
||||
|
||||
$totalPodcastEpisodesCount = PodcastEpisodesQuery::create()
|
||||
->filterByDbPodcastId($id)
|
||||
->count();
|
||||
|
||||
// Check if offset and limit were sent with request.
|
||||
// Default limit to zero and offset to $totalFileCount
|
||||
$offset = $this->_getParam('offset', 0);
|
||||
$limit = $this->_getParam('limit', $totalPodcastEpisodesCount);
|
||||
|
||||
//Sorting parameters
|
||||
$sortColumn = $this->_getParam('sort', PodcastEpisodesPeer::ID);
|
||||
$sortDir = $this->_getParam('sort_dir', Criteria::ASC);
|
||||
|
||||
$episodes = PodcastEpisodesQuery::create()
|
||||
->filterByDbPodcastId($id)
|
||||
->setLimit($limit)
|
||||
->setOffset($offset)
|
||||
->orderBy($sortColumn, $sortDir)
|
||||
->find();
|
||||
|
||||
$episodesArray = array();
|
||||
foreach ($episodes as $episode) {
|
||||
array_push($episodesArray, $episode->toArray(BasePeer::TYPE_FIELDNAME));
|
||||
}
|
||||
|
||||
$this->getResponse()
|
||||
->setHttpResponseCode(201)
|
||||
->appendBody(json_encode(PodcastEpisodes::getPodcastEpisodes($id)));
|
||||
->appendBody(json_encode($episodesArray));
|
||||
|
||||
} catch (PodcastNotFoundException $e) {
|
||||
$this->podcastNotFoundResponse();
|
||||
Logging::error($e->getMessage());
|
||||
|
@ -46,7 +78,7 @@ class Rest_PodcastEpisodesController extends Zend_Rest_Controller
|
|||
try {
|
||||
$this->getResponse()
|
||||
->setHttpResponseCode(201)
|
||||
->appendBody(json_encode(PodcastEpisodes::getPodcastEpisodeById($episodeId)));
|
||||
->appendBody(json_encode(Application_Service_PodcastEpisodeService::getPodcastEpisodeById($episodeId)));
|
||||
|
||||
} catch (PodcastNotFoundException $e) {
|
||||
$this->podcastNotFoundResponse();
|
||||
|
@ -62,7 +94,6 @@ class Rest_PodcastEpisodesController extends Zend_Rest_Controller
|
|||
|
||||
public function postAction()
|
||||
{
|
||||
Logging::info("episodes post");
|
||||
//If we do get an episode ID on a POST, then that doesn't make any sense
|
||||
//since POST is only for creating.
|
||||
if ($episodeId = $this->_getParam('episode_id', false)) {
|
||||
|
@ -82,7 +113,7 @@ class Rest_PodcastEpisodesController extends Zend_Rest_Controller
|
|||
$requestData = json_decode($this->getRequest()->getRawBody(), true);
|
||||
//$requestData = $this->getRequest()->getPost();
|
||||
|
||||
$episode = PodcastEpisodes::create($id, $requestData);
|
||||
$episode = Application_Service_PodcastEpisodeService::createPodcastEpisode($id, $requestData);
|
||||
|
||||
$this->getResponse()
|
||||
->setHttpResponseCode(201)
|
||||
|
@ -111,7 +142,7 @@ class Rest_PodcastEpisodesController extends Zend_Rest_Controller
|
|||
}
|
||||
|
||||
try {
|
||||
PodcastEpisodes::deleteById($episodeId);
|
||||
Application_Service_PodcastEpisodeService::deletePodcastEpisodeById($episodeId);
|
||||
$this->getResponse()
|
||||
->setHttpResponseCode(204);
|
||||
} catch (PodcastEpisodeNotFoundException $e) {
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
<?php
|
||||
|
||||
class PodcastEpisodeNotFoundException extends Exception {}
|
||||
|
||||
class Application_Service_PodcastEpisodeService extends Application_Service_ThirdPartyCeleryService implements Publish
|
||||
{
|
||||
/**
|
||||
|
@ -25,6 +27,10 @@ class Application_Service_PodcastEpisodeService extends Application_Service_Thir
|
|||
self::DOWNLOAD => 'podcast-download'
|
||||
];
|
||||
|
||||
private static $privateFields = array(
|
||||
"id"
|
||||
);
|
||||
|
||||
/**
|
||||
* Given an array of episodes, store them in the database as placeholder objects until
|
||||
* they can be processed by Celery
|
||||
|
@ -166,4 +172,72 @@ class Application_Service_PodcastEpisodeService extends Application_Service_Thir
|
|||
->findOneByDbFileId($fileId)
|
||||
->delete();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $episodeId
|
||||
* @return array
|
||||
* @throws PodcastEpisodeNotFoundException
|
||||
*/
|
||||
public static function getPodcastEpisodeById($episodeId)
|
||||
{
|
||||
$episode = PodcastEpisodesQuery::create()->findPk($episodeId);
|
||||
if (!$episode) {
|
||||
throw new PodcastEpisodeNotFoundException();
|
||||
}
|
||||
|
||||
return $episode->toArray(BasePeer::TYPE_FIELDNAME);
|
||||
}
|
||||
|
||||
public static function getPodcastEpisodes($podcastId)
|
||||
{
|
||||
$podcast = PodcastQuery::create()->findPk($podcastId);
|
||||
if (!$podcast) {
|
||||
throw new PodcastNotFoundException();
|
||||
}
|
||||
|
||||
$episodes = PodcastEpisodesQuery::create()->findByDbPodcastId($podcastId);
|
||||
$episodesArray = array();
|
||||
foreach ($episodes as $episode) {
|
||||
array_push($episodesArray, $episode->toArray(BasePeer::TYPE_FIELDNAME));
|
||||
}
|
||||
|
||||
return $episodesArray;
|
||||
}
|
||||
|
||||
public static function createPodcastEpisode($podcastId, $data)
|
||||
{
|
||||
self::removePrivateFields($data);
|
||||
|
||||
try {
|
||||
$episode = new PodcastEpisodes();
|
||||
$episode->setDbPodcastId($podcastId);
|
||||
$episode->fromArray($data, BasePeer::TYPE_FIELDNAME);
|
||||
$episode->save();
|
||||
|
||||
return $episode->toArray(BasePeer::TYPE_FIELDNAME);
|
||||
} catch (Exception $e) {
|
||||
$episode->delete();
|
||||
throw $e;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static function deletePodcastEpisodeById($episodeId)
|
||||
{
|
||||
$episode = PodcastEpisodesQuery::create()->findByDbId($episodeId);
|
||||
|
||||
if ($episode) {
|
||||
$episode->delete();
|
||||
} else {
|
||||
throw new PodcastEpisodeNotFoundException();
|
||||
}
|
||||
}
|
||||
|
||||
private static function removePrivateFields(&$data)
|
||||
{
|
||||
foreach (self::$privateFields as $key) {
|
||||
unset($data[$key]);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue