diff --git a/airtime_mvc/application/modules/rest/controllers/PodcastEpisodesController.php b/airtime_mvc/application/modules/rest/controllers/PodcastEpisodesController.php index 605bbb521..c9b7e249c 100644 --- a/airtime_mvc/application/modules/rest/controllers/PodcastEpisodesController.php +++ b/airtime_mvc/application/modules/rest/controllers/PodcastEpisodesController.php @@ -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) { diff --git a/airtime_mvc/application/services/PodcastEpisodeService.php b/airtime_mvc/application/services/PodcastEpisodeService.php index db38df1e5..6c2da74a2 100644 --- a/airtime_mvc/application/services/PodcastEpisodeService.php +++ b/airtime_mvc/application/services/PodcastEpisodeService.php @@ -1,5 +1,7 @@ '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]); + } + } + } \ No newline at end of file