Merge pull request #1374 from jygaulier/API_STORY

report des routes api/story de 3.8
This commit is contained in:
Benoît Burnichon
2015-05-11 12:18:45 +02:00
7 changed files with 110 additions and 13 deletions

View File

@@ -1963,7 +1963,7 @@ class V1Controller extends Controller
$this->app->abort(400, 'Request body does not contains a valid "story" object');
}
$collection = \collection::get_from_base_id($this->app, $data->{'collection_id'});
$collection = \collection::get_from_base_id($this->app, $data->{'base_id'});
if (!$this->getAclForUser()->has_right_on_base($collection->get_base_id(), 'canaddrecord')) {
$this->app->abort(403, sprintf('You can not create a story on this collection %s', $collection->get_base_id()));
@@ -2010,7 +2010,7 @@ class V1Controller extends Controller
return $record->get_serialize_key();
}
public function createRecordStoryAction(Request $request, $databox_id, $story_id)
public function addRecordsToStoryAction(Request $request, $databox_id, $story_id)
{
$content = $request->getContent();
@@ -2046,6 +2046,76 @@ class V1Controller extends Controller
return $result->createResponse();
}
public function setStoryCoverAction(Request $request, $databox_id, $story_id)
{
$content = $request->getContent();
$data = @json_decode($content);
if (JSON_ERROR_NONE !== json_last_error()) {
$this->app->abort(400, 'Json response cannot be decoded or the encoded data is deeper than the recursion limit');
}
$schemaStoryCover = $this->getJsonSchemaRetriever()
->retrieve('file://'.$this->app['root.path'].'/lib/conf.d/json_schema/story_cover.json');
$validator = $this->getJsonSchemaValidator();
$validator->check($data, $schemaStoryCover);
if (false === $validator->isValid()) {
$this->app->abort(400, 'Request body contains not a valid "story cover" object');
}
$story = new \record_adapter($this->app, $databox_id, $story_id);
// we do NOT let "set_story_cover()" fail : pass false as last arg
$record_key = $this->setStoryCover($story, $data->{'record_id'}, false);
$result = Result::create($request, array($record_key));
return $result->createResponse();
}
protected function setStoryCover(\record_adapter $story, $record_id, $can_fail=false)
{
try {
$record = new \record_adapter($this->app, $story->get_sbas_id(), $record_id);
} catch (\Exception_Record_AdapterNotFound $e) {
$this->app->abort(404, sprintf('Record identified by databox_id %s and record_id %s could not be found', $story->get_sbas_id(), $record_id));
}
if (!$story->hasChild($record)) {
$this->app->abort(404, sprintf('Record identified by databox_id %s and record_id %s is not in the story', $story->get_sbas_id(), $record_id));
}
if ($record->get_type() !== 'image') {
// this can fail so we can loop on many records during story creation...
if($can_fail) {
return false;
}
$this->app->abort(403, sprintf('Record identified by databox_id %s and record_id %s is not an image', $story->get_sbas_id(), $record_id));
}
foreach ($record->get_subdefs() as $name => $value) {
if (!in_array($name, array('thumbnail', 'preview'))) {
continue;
}
/** @var MediaVorus $mediavorus */
$mediavorus = $this->app['mediavorus'];
$media = $mediavorus->guess($value->get_pathfile());
$story->substitute_subdef($name, $media, $this->app);
$this->app['phraseanet.logger']($story->get_databox())->log(
$story,
\Session_Logger::EVENT_SUBSTITUTE,
$name == 'document' ? 'HD' : $name,
''
);
}
$this->getDispatcher()->dispatch(PhraseaEvents::RECORD_EDIT, new RecordEdit($story));
return $record->get_serialize_key();
}
public function getCurrentUserAction(Request $request)
{
$ret = ["user" => $this->listUser($this->getAuthenticatedUser())];
@@ -2135,6 +2205,14 @@ class V1Controller extends Controller
return null;
}
public function ensureJsonContentType(Request $request)
{
if ($request->getContentType() != 'json') {
$this->app->abort(406, 'Invalid Content Type given.');
}
}
/**
* @return EventDispatcherInterface
*/

View File

@@ -225,9 +225,16 @@ class V1 implements ControllerProviderInterface, ServiceProviderInterface
->assert('record_id', '\d+');
$controllers->get('/stories/{any_id}/{anyother_id}/', 'controller.api.v1:getBadRequestAction');
$controllers->post('/stories', 'controller.api.v1:createStoriesAction');
$controllers->post('/stories', 'controller.api.v1:createStoriesAction')
->before('controller.api.v1:ensureJsonContentType');
$controllers->post('/stories/{databox_id}/{story_id}/records', 'controller.api.v1:createRecordStoryAction')
$controllers->post('/stories/{databox_id}/{story_id}/addrecords', 'controller.api.v1:addRecordsToStoryAction')
->before('controller.api.v1:ensureJsonContentType')
->assert('databox_id', '\d+')
->assert('story_id', '\d+');
$controllers->post('/stories/{databox_id}/{story_id}/setcover', 'controller.api.v1:setStoryCoverAction')
->before('controller.api.v1:ensureJsonContentType')
->assert('databox_id', '\d+')
->assert('story_id', '\d+');

View File

@@ -56,9 +56,9 @@ class ApiExceptionHandlerSubscriber implements EventSubscriberInterface
$code = 404;
} elseif ($e instanceof HttpExceptionInterface) {
if (503 === $e->getStatusCode()) {
$code = \API_V1_result::ERROR_MAINTENANCE;
$code = Result::ERROR_MAINTENANCE;
} else if (406 === $e->getStatusCode()) {
$code = \API_V1_result::ERROR_UNACCEPTABLE;
$code = Result::ERROR_UNACCEPTABLE;
} else {
$code = 500;
}

View File

@@ -564,8 +564,7 @@ class record_adapter implements RecordInterface, cache_cacheableInterface
}
/**
*
* @return Array
* @return media_subdef[]
*/
public function get_subdefs()
{

View File

@@ -16,10 +16,10 @@
"description": "The databox identifier where the record is localized",
"type": "integer"
},
"collection_id": {
"description": "The collection identifier where the record is localized",
"base_id": {
"description": "The base identifier where the record is localized",
"type": "integer"
}
},
"required": ["collection_id"]
"required": ["base_id"]
}

View File

@@ -0,0 +1,13 @@
{
"$schema": "http://json-schema.org/draft-04/schema#",
"title": "Story cover",
"description": "A record to be used as source of cover, the record must be an element of the story",
"type": "object",
"properties": {
"record_id": {
"description": "The identifier of the record used for cover (in the same databox as the story)",
"type": "integer"
}
},
"required": ["record_id"]
}

View File

@@ -72,7 +72,7 @@ abstract class ApiTestCase extends \PhraseanetWebTestCase
$this->setToken($this->userAccessToken);
$route = '/api/v1/stories';
$story['collection_id'] = self::$DI['collection']->get_base_id();
$story['base_id'] = self::$DI['collection']->get_base_id();
$story['title'] = uniqid('story');
$file = new File(
@@ -115,7 +115,7 @@ abstract class ApiTestCase extends \PhraseanetWebTestCase
$this->setToken($this->userAccessToken);
$story = \record_adapter::createStory(self::$DI['app'], self::$DI['collection']);
$route = sprintf('/api/v1/stories/%s/%s/records', $story->get_sbas_id(), $story->get_record_id());
$route = sprintf('/api/v1/stories/%s/%s/addrecords', $story->get_sbas_id(), $story->get_record_id());
$file = new File(
self::$DI['app'],