Get item metadata and Get Collection metadata

This commit is contained in:
weryques 2017-12-12 15:17:55 -02:00
parent 1174fe329c
commit 3550c8b614
3 changed files with 124 additions and 6 deletions

View File

@ -151,19 +151,61 @@ class TAINACAN_REST_Metadata_Controller extends WP_REST_Controller {
}
public function prepare_item_for_response( $item, $request ) {
$metadata_as_json = [];
if($request['item_id']) {
foreach ( $item as $metadata ) {
$metadata_as_json[] = json_encode(
array_merge(
json_decode($metadata->get_metadata()->__toJSON(), true),
['value' => $metadata->get_value()]
)
);
}
} else {
foreach ( $item as $metadata ) {
$metadata_as_json[] = $metadata->__toJSON();
}
}
return $metadata_as_json;
}
public function get_items( $request ) {
$id = !empty($request['collection_id']) ? $request['collection_id'] : (!empty($request['item_id']) ? $request['item_id'] : '');
if(!empty($request['collection_id'])){
$collection_id = $request['collection_id'];
$this->metadata_repository;
$collection = new Entities\Collection($collection_id);
$collection_metadata = $this->metadata_repository->fetch_by_collection($collection, [], 'OBJECT');
$prepared_item = $this->prepare_item_for_response($collection_metadata, $request);
return new WP_REST_Response($prepared_item, 200);
} elseif(!empty($request['item_id'])){
$item_id = $request['item_id'];
$item = new Entities\Item($item_id);
$item_metadata = $this->item_metadata_repository->fetch($item, 'OBJECT');
$prepared_item = $this->prepare_item_for_response($item_metadata, $request);
return new WP_REST_Response($prepared_item, 200);
}
return new WP_REST_Response($request->get_body(), 400);
}
public function get_item_permissions_check( $request ) {
return true;
}
public function get_items_permissions_check( $request ) {
return true;
}
public function get_collection_params() {
return parent::get_collection_params(); // TODO: Change the autogenerated stub
}

View File

@ -55,8 +55,7 @@ class Entity_Factory {
$this->entity = new $this->entity_type();
$this->repository = new $this->repository_type();
if($publish)
{
if($publish) {
$this->entity->set_status('publish');
}

View File

@ -41,8 +41,7 @@ class TAINACAN_REST_Metadata_Controller extends TAINACAN_UnitApiTestCase {
$this->assertEquals('Moeda', $metadata_added['name']);
####################
#################### Add value to metadata of item ##########################
$meta_values = json_encode(
array(
@ -68,6 +67,84 @@ class TAINACAN_REST_Metadata_Controller extends TAINACAN_UnitApiTestCase {
$this->assertEquals('Valorado', $metav);
}
public function test_get_item_and_collection_metadata(){
global $Tainacan_Item_Metadata;
$collection = $this->tainacan_entity_factory->create_entity(
'collection',
array(
'name' => 'Statement'
),
true
);
$item = $this->tainacan_entity_factory->create_entity(
'item',
array(
'title' => 'No name',
'description' => 'No description',
'collection' => $collection
),
true
);
$field = $this->tainacan_field_factory->create_field('text', '', true);
$metadata = $this->tainacan_entity_factory->create_entity(
'metadata',
array(
'name' => 'Data',
'description' => 'Descreve valor do campo data.',
'collection' => $collection,
'status' => 'publish',
'field_type' => $field->get_primitive_type(),
),
true
);
$item_metadata = new \Tainacan\Entities\Item_Metadata_Entity($item, $metadata);
$item_metadata->set_value('12/12/2017');
$item_metadata->validate();
$Tainacan_Item_Metadata->insert($item_metadata);
#################### Get metadata of collection ######################
$request = new \WP_REST_Request(
'GET',
$this->namespaced_route . '/metadata/collection/' . $collection->get_id()
);
$response = $this->server->dispatch($request);
$data = $response->get_data();
$this->assertContainsOnly('string', $data);
$metadata = json_decode($data[0], true);
$this->assertEquals('Data', $metadata['name']);
################### Get metadata of item with value #######################
$request = new \WP_REST_Request(
'GET',
$this->namespaced_route . '/metadata/item/' . $item->get_id()
);
$response = $this->server->dispatch($request);
$data = $response->get_data();
$this->assertContainsOnly('string', $data);
$metadata = json_decode($data[0], true);
$this->assertEquals('Data', $metadata['name']);
$this->assertEquals('12/12/2017', $metadata['value']);
}
}
?>