SAAS-1077: REST API for podcast episodes
This commit is contained in:
parent
5bd8a508b1
commit
0fc324f3ba
3 changed files with 214 additions and 21 deletions
|
@ -31,7 +31,7 @@ class Podcast extends BasePodcast
|
|||
/** Creates a Podcast object from the given podcast URL.
|
||||
* This is used by our Podcast REST API
|
||||
*
|
||||
* @param $podcastArray An array containing the URL for a Podcast object.
|
||||
* @param $data An array containing the URL for a Podcast object.
|
||||
*
|
||||
* @return array - Podcast Array with a full list of episodes
|
||||
* @throws Exception
|
||||
|
@ -161,7 +161,6 @@ class Podcast extends BasePodcast
|
|||
* Trims the podcast metadata to fit the table's column max size
|
||||
*
|
||||
* @param $podcastArray
|
||||
* @throws PropelException
|
||||
*/
|
||||
private static function validatePodcastMetadata(&$podcastArray)
|
||||
{
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
<?php
|
||||
|
||||
|
||||
class PodcastEpisodeNotFoundException extends Exception
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* Skeleton subclass for representing a row from the 'podcast_episodes' table.
|
||||
|
@ -15,4 +17,103 @@
|
|||
*/
|
||||
class PodcastEpisodes extends BasePodcastEpisodes
|
||||
{
|
||||
|
||||
private static $privateFields = array(
|
||||
"id"
|
||||
);
|
||||
|
||||
/**
|
||||
* @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 create($podcastId, $data)
|
||||
{
|
||||
self::removePrivateFields($data);
|
||||
self::validateEpisodeData($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 deleteById($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]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Trims the episode data to fit the table's column max size
|
||||
*
|
||||
* @param $episodeArray
|
||||
*/
|
||||
private static function validateEpisodeData(&$episodeArray)
|
||||
{
|
||||
$podcastEpisodeTable = PodcastEpisodesPeer::getTableMap();
|
||||
|
||||
foreach ($episodeArray as $key => &$value) {
|
||||
try {
|
||||
// Make sure column exists in table
|
||||
$columnMaxSize = $podcastEpisodeTable->getColumn($key)->getSize();
|
||||
|
||||
if (is_null($columnMaxSize)) {
|
||||
continue;
|
||||
}
|
||||
} catch (PropelException $e) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (strlen($value) > $columnMaxSize) {
|
||||
$value = substr($value, 0, $podcastEpisodeTable->getColumn($key)->getSize());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -12,52 +12,120 @@ class Rest_PodcastEpisodesController extends Zend_Rest_Controller
|
|||
|
||||
public function indexAction()
|
||||
{
|
||||
Logging::info("episodes index");
|
||||
$id = $this->getId();
|
||||
Logging::info($id);
|
||||
if (!$id) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
public function getAction()
|
||||
{
|
||||
Logging::info("episodes get");
|
||||
$id = $this->getId();
|
||||
if (!$id) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
|
||||
$this->getResponse()
|
||||
->setHttpResponseCode(201)
|
||||
->appendBody(json_encode(PodcastEpisodes::getPodcastEpisodes($id)));
|
||||
} catch (PodcastNotFoundException $e) {
|
||||
$this->podcastNotFoundResponse();
|
||||
Logging::error($e->getMessage());
|
||||
} catch (Exception $e) {
|
||||
$this->unknownErrorResponse();
|
||||
Logging::error($e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public function getAction()
|
||||
{
|
||||
//TODO: can we delete this?
|
||||
$id = $this->getId();
|
||||
if (!$id) {
|
||||
return;
|
||||
}
|
||||
|
||||
$episodeId = $this->getEpisodeId();
|
||||
if (!$episodeId) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
$this->getResponse()
|
||||
->setHttpResponseCode(201)
|
||||
->appendBody(json_encode(PodcastEpisodes::getPodcastEpisodeById($episodeId)));
|
||||
|
||||
} catch (PodcastNotFoundException $e) {
|
||||
$this->podcastNotFoundResponse();
|
||||
Logging::error($e->getMessage());
|
||||
} catch (PodcastEpisodeNotFoundException $e) {
|
||||
$this->podcastEpisodeNotFoundResponse();
|
||||
Logging::error($e->getMessage());
|
||||
} catch (Exception $e) {
|
||||
$this->unknownErrorResponse();
|
||||
Logging::error($e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
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)) {
|
||||
$resp = $this->getResponse();
|
||||
$resp->setHttpResponseCode(400);
|
||||
$resp->appendBody("ERROR: Episode ID should not be specified when using POST. POST is only used for importing podcast episodes, and an episode ID will be chosen by Airtime");
|
||||
return;
|
||||
}
|
||||
|
||||
// Make sure a podcast ID was specified
|
||||
$id = $this->getId();
|
||||
if (!$id) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
//$requestData = json_decode($this->getRequest()->getRawBody(), true);
|
||||
$requestData = $this->getRequest()->getPost();
|
||||
|
||||
$episode = PodcastEpisodes::create($id, $requestData);
|
||||
|
||||
$this->getResponse()
|
||||
->setHttpResponseCode(201)
|
||||
->appendBody(json_encode($episode));
|
||||
|
||||
} catch (Exception $e) {
|
||||
$this->unknownErrorResponse();
|
||||
Logging::error($e->getMessage());
|
||||
}
|
||||
|
||||
public function putAction()
|
||||
{
|
||||
Logging::info("episodes put");
|
||||
}
|
||||
|
||||
public function deleteAction()
|
||||
{
|
||||
Logging::info("delete - episodes");
|
||||
|
||||
//TODO: can we delete this?
|
||||
$id = $this->getId();
|
||||
Logging::info($id);
|
||||
if (!$id) {
|
||||
return;
|
||||
}
|
||||
|
||||
$episodeId = $this->getEpisodeId();
|
||||
if (!$episodeId) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
PodcastEpisodes::deleteById($episodeId);
|
||||
$this->getResponse()
|
||||
->setHttpResponseCode(204);
|
||||
} catch (PodcastEpisodeNotFoundException $e) {
|
||||
$this->podcastEpisodeNotFoundResponse();
|
||||
Logging::error($e->getMessage());
|
||||
} catch (Exception $e) {
|
||||
$this->unknownErrorResponse();
|
||||
Logging::error($e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
private function getId()
|
||||
{
|
||||
if (!$id = $this->_getParam('episode_id', false)) {
|
||||
if (!$id = $this->_getParam('id', false)) {
|
||||
$resp = $this->getResponse();
|
||||
$resp->setHttpResponseCode(400);
|
||||
$resp->appendBody("ERROR: No podcast ID specified.");
|
||||
|
@ -66,6 +134,17 @@ class Rest_PodcastEpisodesController extends Zend_Rest_Controller
|
|||
return $id;
|
||||
}
|
||||
|
||||
private function getEpisodeId()
|
||||
{
|
||||
if (!$episodeId = $this->_getParam('episode_id', false)) {
|
||||
$resp = $this->getResponse();
|
||||
$resp->setHttpResponseCode(400);
|
||||
$resp->appendBody("ERROR: No podcast episode ID specified.");
|
||||
return false;
|
||||
}
|
||||
return $episodeId;
|
||||
}
|
||||
|
||||
private function unknownErrorResponse()
|
||||
{
|
||||
$resp = $this->getResponse();
|
||||
|
@ -73,4 +152,18 @@ class Rest_PodcastEpisodesController extends Zend_Rest_Controller
|
|||
$resp->appendBody("An unknown error occurred.");
|
||||
}
|
||||
|
||||
private function podcastNotFoundResponse()
|
||||
{
|
||||
$resp = $this->getResponse();
|
||||
$resp->setHttpResponseCode(404);
|
||||
$resp->appendBody("ERROR: Podcast not found.");
|
||||
}
|
||||
|
||||
private function podcastEpisodeNotFoundResponse()
|
||||
{
|
||||
$resp = $this->getResponse();
|
||||
$resp->setHttpResponseCode(404);
|
||||
$resp->appendBody("ERROR: Podcast episode not found.");
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue