Refactor podcast episodes rest controller index action

This commit is contained in:
drigato 2015-10-26 11:28:10 -04:00
parent e8980e7a79
commit e994033109
2 changed files with 24 additions and 21 deletions

View file

@ -25,12 +25,6 @@ class Rest_PodcastEpisodesController extends Zend_Rest_Controller
return; return;
} }
try { try {
$podcast = PodcastQuery::create()->findPk($id);
if (!$podcast) {
throw new PodcastNotFoundException();
}
$totalPodcastEpisodesCount = PodcastEpisodesQuery::create() $totalPodcastEpisodesCount = PodcastEpisodesQuery::create()
->filterByDbPodcastId($id) ->filterByDbPodcastId($id)
->count(); ->count();
@ -44,21 +38,9 @@ class Rest_PodcastEpisodesController extends Zend_Rest_Controller
$sortColumn = $this->_getParam('sort', PodcastEpisodesPeer::ID); $sortColumn = $this->_getParam('sort', PodcastEpisodesPeer::ID);
$sortDir = $this->_getParam('sort_dir', Criteria::ASC); $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() $this->getResponse()
->setHttpResponseCode(201) ->setHttpResponseCode(201)
->appendBody(json_encode($episodesArray)); ->appendBody(json_encode($this->_service->getPodcastEpisodes($id, $offset, $limit, $sortColumn, $sortDir)));
} catch (PodcastNotFoundException $e) { } catch (PodcastNotFoundException $e) {
$this->podcastNotFoundResponse(); $this->podcastNotFoundResponse();

View file

@ -206,14 +206,35 @@ class Application_Service_PodcastEpisodeService extends Application_Service_Thir
return $episode->toArray(BasePeer::TYPE_FIELDNAME); return $episode->toArray(BasePeer::TYPE_FIELDNAME);
} }
public function getPodcastEpisodes($podcastId) /**
* Returns an array of Podcast episodes, with the option to paginate the results.
*
* @param $podcastId
* @param int $offset
* @param int $limit
* @param string $sortColumn
* @param string $sortDir
* @return array
* @throws PodcastNotFoundException
*/
public function getPodcastEpisodes($podcastId,
$offset=0,
$limit=10,
$sortColumn=PodcastEpisodesPeer::ID,
$sortDir=Criteria::ASC)
{ {
$podcast = PodcastQuery::create()->findPk($podcastId); $podcast = PodcastQuery::create()->findPk($podcastId);
if (!$podcast) { if (!$podcast) {
throw new PodcastNotFoundException(); throw new PodcastNotFoundException();
} }
$episodes = PodcastEpisodesQuery::create()->findByDbPodcastId($podcastId); $episodes = PodcastEpisodesQuery::create()
->filterByDbPodcastId($podcastId)
->setLimit($limit)
->setOffset($offset)
->orderBy($sortColumn, $sortDir)
->find();
$episodesArray = array(); $episodesArray = array();
foreach ($episodes as $episode) { foreach ($episodes as $episode) {
array_push($episodesArray, $episode->toArray(BasePeer::TYPE_FIELDNAME)); array_push($episodesArray, $episode->toArray(BasePeer::TYPE_FIELDNAME));