Fetch filters
This commit is contained in:
parent
b3d92b7abb
commit
027fd43030
|
@ -38,6 +38,11 @@ class TAINACAN_REST_Filters_Controller extends WP_REST_Controller {
|
|||
'methods' => WP_REST_Server::CREATABLE,
|
||||
'callback' => array($this, 'create_item'),
|
||||
'permission_callback' => array($this, 'create_item_permissions_check')
|
||||
),
|
||||
array(
|
||||
'methods' => WP_REST_Server::READABLE,
|
||||
'callback' => array($this, 'get_items'),
|
||||
'permission_callback' => array($this, 'get_items_permissions_check')
|
||||
)
|
||||
));
|
||||
register_rest_route($this->namespace, '/' . $this->rest_base . '/(?P<filter_id>[\d]+)', array(
|
||||
|
@ -132,6 +137,11 @@ class TAINACAN_REST_Filters_Controller extends WP_REST_Controller {
|
|||
return $this->filter_repository->can_edit($this->filter);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param WP_REST_Request $request
|
||||
*
|
||||
* @return WP_Error|WP_REST_Response
|
||||
*/
|
||||
public function delete_item( $request ) {
|
||||
$filter_id = $request['filter_id'];
|
||||
|
||||
|
@ -151,11 +161,21 @@ class TAINACAN_REST_Filters_Controller extends WP_REST_Controller {
|
|||
], 400);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param WP_REST_Request $request
|
||||
*
|
||||
* @return bool|WP_Error
|
||||
*/
|
||||
public function delete_item_permissions_check( $request ) {
|
||||
$filter = $this->filter_repository->fetch($request['filter_id']);
|
||||
return $this->filter_repository->can_delete($filter);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $request
|
||||
*
|
||||
* @return WP_Error|WP_REST_Response
|
||||
*/
|
||||
public function update_item( $request ) {
|
||||
$filter_id = $request['filter_id'];
|
||||
|
||||
|
@ -180,9 +200,57 @@ class TAINACAN_REST_Filters_Controller extends WP_REST_Controller {
|
|||
|
||||
}
|
||||
|
||||
/**
|
||||
* @param WP_REST_Request $request
|
||||
*
|
||||
* @return bool|WP_Error
|
||||
*/
|
||||
public function update_item_permissions_check( $request ) {
|
||||
$filter = $this->filter_repository->fetch($request['filter_id']);
|
||||
return $this->filter_repository->can_edit($filter);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $object
|
||||
* @param WP_REST_Request $request
|
||||
*
|
||||
* @return array|mixed|WP_Error|WP_REST_Response
|
||||
*/
|
||||
public function prepare_item_for_response( $object, $request ) {
|
||||
|
||||
if(is_array($object)){
|
||||
$filters = [];
|
||||
|
||||
foreach ($object as $item){
|
||||
$filters[] = $item->__toArray();
|
||||
}
|
||||
|
||||
return $filters;
|
||||
}
|
||||
|
||||
return $object;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param WP_REST_Request $request
|
||||
*
|
||||
* @return WP_Error|WP_REST_Response
|
||||
*/
|
||||
public function get_items( $request ) {
|
||||
$filters = $this->filter_repository->fetch([], 'OBJECT');
|
||||
|
||||
$response = $this->prepare_item_for_response($filters, $request);
|
||||
|
||||
return new WP_REST_Response($response, 200);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param WP_REST_Request $request
|
||||
*
|
||||
* @return bool|WP_Error
|
||||
*/
|
||||
public function get_items_permissions_check( $request ) {
|
||||
return $this->filter_repository->can_read($this->filter);
|
||||
}
|
||||
}
|
||||
?>
|
|
@ -117,6 +117,11 @@ class TAINACAN_REST_Terms_Controller extends WP_REST_Controller {
|
|||
return $this->terms_repository->can_edit($this->term);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param WP_REST_Request $request
|
||||
*
|
||||
* @return WP_Error|WP_REST_Response
|
||||
*/
|
||||
public function delete_item( $request ) {
|
||||
$term_id = $request['term_id'];
|
||||
$taxonomy_id = $request['taxonomy_id'];
|
||||
|
@ -136,11 +141,21 @@ class TAINACAN_REST_Terms_Controller extends WP_REST_Controller {
|
|||
return new WP_REST_Response($is_deleted, 200);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param WP_REST_Request $request
|
||||
*
|
||||
* @return bool|WP_Error
|
||||
*/
|
||||
public function delete_item_permissions_check( $request ) {
|
||||
$term = new Entities\Term($this->terms_repository->fetch($request['term_id']));
|
||||
return $this->terms_repository->can_delete($term);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param WP_REST_Request $request
|
||||
*
|
||||
* @return WP_Error|WP_REST_Response
|
||||
*/
|
||||
public function update_item( $request ) {
|
||||
$term_id = $request['term_id'];
|
||||
$taxonomy_id = $request['taxonomy_id'];
|
||||
|
@ -172,6 +187,11 @@ class TAINACAN_REST_Terms_Controller extends WP_REST_Controller {
|
|||
], 400);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param WP_REST_Request $request
|
||||
*
|
||||
* @return bool|WP_Error
|
||||
*/
|
||||
public function update_item_permissions_check( $request ) {
|
||||
$term = new Entities\Term($this->terms_repository->fetch($request['term_id']));
|
||||
return $this->terms_repository->can_edit($term);
|
||||
|
|
|
@ -131,6 +131,7 @@ class TAINACAN_REST_Terms_Controller extends TAINACAN_UnitApiTestCase {
|
|||
array(
|
||||
'name' => 'Metadata filtered',
|
||||
'description' => 'Is filtered',
|
||||
'collection_id' => $collection->get_id()
|
||||
)
|
||||
);
|
||||
|
||||
|
@ -165,6 +166,75 @@ class TAINACAN_REST_Terms_Controller extends TAINACAN_UnitApiTestCase {
|
|||
$this->assertNotEquals('filtro', $data['name']);
|
||||
$this->assertEquals('Faceta', $data['name']);
|
||||
}
|
||||
|
||||
public function test_fetch_filters(){
|
||||
$collection = $this->tainacan_entity_factory->create_entity(
|
||||
'collection',
|
||||
array(
|
||||
'name' => 'Collection filtered',
|
||||
'description' => 'Is filtered',
|
||||
),
|
||||
true
|
||||
);
|
||||
|
||||
$metadata = $this->tainacan_entity_factory->create_entity(
|
||||
'metadata',
|
||||
array(
|
||||
'name' => 'Metadata filtered',
|
||||
'description' => 'Is filtered',
|
||||
'collection_id' => $collection->get_id()
|
||||
)
|
||||
);
|
||||
|
||||
$metadata2 = $this->tainacan_entity_factory->create_entity(
|
||||
'metadata',
|
||||
array(
|
||||
'name' => 'Other filtered',
|
||||
'description' => 'Is filtered',
|
||||
'collection_id' => $collection->get_id()
|
||||
)
|
||||
);
|
||||
|
||||
$filter_type = $this->tainacan_filter_factory->create_filter('range');
|
||||
|
||||
$filter = $this->tainacan_entity_factory->create_entity(
|
||||
'filter',
|
||||
array(
|
||||
'name' => 'filtro',
|
||||
'collection' => $collection,
|
||||
'description' => 'descricao',
|
||||
'metadata' => $metadata,
|
||||
'filter_type' => $filter_type,
|
||||
'status' => 'publish'
|
||||
),
|
||||
true
|
||||
);
|
||||
|
||||
$filter2 = $this->tainacan_entity_factory->create_entity(
|
||||
'filter',
|
||||
array(
|
||||
'name' => 'filtro2',
|
||||
'collection' => $collection,
|
||||
'description' => 'descricao',
|
||||
'metadata' => $metadata2,
|
||||
'filter_type' => $filter_type,
|
||||
'status' => 'publish'
|
||||
),
|
||||
true
|
||||
);
|
||||
|
||||
$request = new \WP_REST_Request('GET', $this->namespace . '/filters');
|
||||
|
||||
$response = $this->server->dispatch($request);
|
||||
|
||||
$data = $response->get_data();
|
||||
|
||||
$first_filter = $data[0];
|
||||
$second_filter = $data[1];
|
||||
|
||||
$this->assertEquals($filter2->get_name(), $first_filter['name']);
|
||||
$this->assertEquals($filter->get_name(), $second_filter['name']);
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
Loading…
Reference in New Issue