Create endpoint to POST filters and your test.

This commit is contained in:
weryques 2018-01-10 13:56:55 -02:00
parent 6222feca03
commit 0541d78b5f
3 changed files with 161 additions and 0 deletions

View File

@ -0,0 +1,109 @@
<?php
use Tainacan\Entities;
use Tainacan\Repositories;
class TAINACAN_REST_Filters_Controller extends WP_REST_Controller {
private $collection;
private $collection_repository;
private $metadata;
private $metadata_repository;
private $filter;
private $filter_repository;
/**
* TAINACAN_REST_Filters_Controller constructor.
*/
public function __construct() {
$this->namespace = '/tainacan/v2';
$this->rest_base = 'filters';
$this->collection = new Entities\Collection();
$this->collection_repository = new Repositories\Collections();
$this->metadata = new Entities\Metadata();
$this->metadata_repository = new Repositories\Metadatas();
$this->filter = new Entities\Filter();
$this->filter_repository = new Repositories\Filters();
add_action('rest_api_init', array($this, 'register_routes'));
}
public function register_routes() {
register_rest_route($this->namespace, '/' . $this->rest_base, array(
array(
'methods' => WP_REST_Server::CREATABLE,
'callback' => array($this, 'create_item'),
'permission_callback' => array($this, 'create_item_permissions_check')
)
));
}
/**
* @param WP_REST_Request $request
*
* @return object|void|WP_Error
*/
public function prepare_item_for_database( $request ) {
$body = json_decode($request->get_body(), true);
$collection_id = $body['collection_id'];
$metadata_id = $body['metadata_id'];
$filter = $body['filter'];
foreach ($filter as $attribute => $value){
try {
$set_ = 'set_'. $attribute;
$this->filter->$set_($value);
} catch (\Error $error){
//
}
}
$this->filter->set_collection_id($collection_id);
$this->filter->set_metadata($metadata_id);
}
/**
* @param WP_REST_Request $request
*
* @return WP_Error|WP_REST_Response
*/
public function create_item( $request ) {
if(!empty($request->get_body())){
$this->prepare_item_for_database($request);
if ($this->filter->validate()){
$filter_inserted = $this->filter_repository->insert($this->filter);
return new WP_REST_Response($filter_inserted->__toArray(), 200);
}
return new WP_REST_Response([
'error_message' => 'One or more attributes are invalid',
'error' => $this->filter->get_errors()
], 400);
}
return new WP_REST_Response([
'error_message' => 'The body could not be empty.',
'body' => $request->get_body()
], 400);
}
/**
* @param WP_REST_Request $request
*
* @return bool|WP_Error
*/
public function create_item_permissions_check( $request ) {
return $this->filter_repository->can_edit($this->filter);
}
}
?>

View File

@ -5,6 +5,7 @@ $rest_items_controller = new TAINACAN_REST_Items_Controller();
$rest_metadata_controller = new TAINACAN_REST_Metadata_Controller();
$rest_taxonomies_controller = new TAINACAN_REST_Taxonomies_Controller();
$rest_terms_controller = new TAINACAN_REST_Terms_Controller();
$rest_filters_controller = new TAINACAN_REST_Filters_Controller();
// Add here other endpoints imports
?>

View File

@ -0,0 +1,51 @@
<?php
namespace Tainacan\Tests;
class TAINACAN_REST_Terms_Controller extends TAINACAN_UnitApiTestCase {
public function test_create_filter(){
$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'
)
);
$request_body = json_encode(
array(
'collection_id' => $collection->get_id(),
'metadata_id' => $metadata->get_id(),
'filter' => [
'name' => 'Filter name',
'description' => 'This is RANGE!',
'filter_type' => 'range'
]
)
);
$request = new \WP_REST_Request('POST', $this->namespace . '/filters');
$request->set_body($request_body);
$response = $this->server->dispatch($request);
$data = $response->get_data();
$this->assertEquals('range', $data['filter_type']);
$this->assertEquals('Filter name', $data['name']);
}
}
?>