diff --git a/src/api/endpoints/class-tainacan-rest-filters-controller.php b/src/api/endpoints/class-tainacan-rest-filters-controller.php new file mode 100644 index 000000000..5186eb765 --- /dev/null +++ b/src/api/endpoints/class-tainacan-rest-filters-controller.php @@ -0,0 +1,109 @@ +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); + } +} + +?> \ No newline at end of file diff --git a/src/api/tainacan-rest-creator.php b/src/api/tainacan-rest-creator.php index 68fb304d1..0e26c3e04 100644 --- a/src/api/tainacan-rest-creator.php +++ b/src/api/tainacan-rest-creator.php @@ -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 ?> \ No newline at end of file diff --git a/tests/test-api-filters.php b/tests/test-api-filters.php new file mode 100644 index 000000000..c0d81c763 --- /dev/null +++ b/tests/test-api-filters.php @@ -0,0 +1,51 @@ +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']); + } + +} + +?> \ No newline at end of file