Skip to content

Commit

Permalink
Allow the use of the type query parameter to filter the results of a …
Browse files Browse the repository at this point in the history
…rest/gallery/items?urls=... request. This allows the client to pass the entire list of member urls and have the rest server filter the results based on the specified types.

(cherry picked from commit 3fe10b1)
  • Loading branch information
Tim Almdal committed Jun 4, 2010
1 parent 04b90c3 commit a600185
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 4 deletions.
22 changes: 18 additions & 4 deletions modules/gallery/helpers/items_rest.php
Expand Up @@ -19,23 +19,37 @@
*/
class items_rest_Core {
/**
* To retrieve a collection of items, you can specify the following query parameters to specify the
* type of the collection. If both are specified, then the url parameter is used and the
* ancestor_for is ignored.
* To retrieve a collection of items, you can specify the following query parameters to specify
* the type of the collection. If both are specified, then the url parameter is used and the
* ancestor_for is ignored. Specifying the "type" parameter with the urls parameter, will
* filter the results based on the specified type. Using the type parameter with the
* ancestor_for parameter makes no sense and will be ignored.
*
* urls=url1,url2,url3
* return items that match the specified urls. Typically used to return the member detail
*
* ancestor_for=url
* return the ancestors of the specified item
*
* type=<comma separate list of photo, movie or album>
* limit the type to types in this list. eg, "type=photo,movie"
*/
static function get($request) {
$items = array();
if (isset($request->params->urls)) {
foreach (json_decode($request->params->urls) as $url) {
if (isset($request->params->type)) {
$types = explode(",", $request->params->type);
}
$item = rest::resolve($url);
if (access::can("view", $item)) {
$items[] = items_rest::format_restful_item($item);
if (isset($types)) {
if (in_array($item->type, $types)) {
$items[] = items_rest::format_restful_item($item);
}
} else {
$items[] = items_rest::format_restful_item($item);
}
}
}
} else if (isset($request->params->ancestor_for)) {
Expand Down
85 changes: 85 additions & 0 deletions modules/gallery/tests/Items_Rest_Helper_Test.php
Expand Up @@ -50,6 +50,91 @@ public function get_url_test() {
items_rest::get($request));
}

public function get_url_filter_album_test() {
$album1 = test::random_album();
$photo1 = test::random_photo($album1);
$album2 = test::random_album($album1);
$photo2 = test::random_photo($album2);
$album1->reload();
$album2->reload();

$request = new stdClass();
$request->params = new stdClass();
$request->params->urls = json_encode(array(
rest::url("item", $photo1),
rest::url("item", $album2)));
$request->params->type = "album";
$this->assert_equal_array(
array(
array("url" => rest::url("item", $album2),
"entity" => $album2->as_restful_array(),
"relationships" => array(
"tags" => array(
"url" => rest::url("item_tags", $album2),
"members" => array())),
"members" => array(
rest::url("item", $photo2)))),
items_rest::get($request));
}

public function get_url_filter_photo_test() {
$album1 = test::random_album();
$photo1 = test::random_photo($album1);
$album2 = test::random_album($album1);
$photo2 = test::random_photo($album2);
$album1->reload();
$album2->reload();

$request = new stdClass();
$request->params = new stdClass();
$request->params->urls = json_encode(array(
rest::url("item", $photo1),
rest::url("item", $album2)));
$request->params->type = "photo";
$this->assert_equal_array(
array(
array("url" => rest::url("item", $photo1),
"entity" => $photo1->as_restful_array(),
"relationships" => array(
"tags" => array(
"url" => rest::url("item_tags", $photo1),
"members" => array())))),
items_rest::get($request));
}

public function get_url_filter_albums_photos_test() {
$album1 = test::random_album();
$photo1 = test::random_photo($album1);
$album2 = test::random_album($album1);
$photo2 = test::random_photo($album2);
$album1->reload();
$album2->reload();

$request = new stdClass();
$request->params = new stdClass();
$request->params->urls = json_encode(array(
rest::url("item", $photo1),
rest::url("item", $album2)));
$request->params->type = "photo,album";
$this->assert_equal_array(
array(
array("url" => rest::url("item", $photo1),
"entity" => $photo1->as_restful_array(),
"relationships" => array(
"tags" => array(
"url" => rest::url("item_tags", $photo1),
"members" => array()))),
array("url" => rest::url("item", $album2),
"entity" => $album2->as_restful_array(),
"relationships" => array(
"tags" => array(
"url" => rest::url("item_tags", $album2),
"members" => array())),
"members" => array(
rest::url("item", $photo2)))),
items_rest::get($request));
}

public function get_ancestor_test() {
$album1 = test::random_album();
$photo1 = test::random_photo($album1);
Expand Down

0 comments on commit a600185

Please sign in to comment.