diff --git a/src/api/endpoints/class-tainacan-rest-filters-controller.php b/src/api/endpoints/class-tainacan-rest-filters-controller.php index a0088b972..ba8332674 100644 --- a/src/api/endpoints/class-tainacan-rest-filters-controller.php +++ b/src/api/endpoints/class-tainacan-rest-filters-controller.php @@ -45,6 +45,11 @@ class TAINACAN_REST_Filters_Controller extends WP_REST_Controller { 'methods' => WP_REST_Server::DELETABLE, 'callback' => array($this, 'delete_item'), 'permission_callback' => array($this, 'delete_item_permissions_check') + ), + array( + 'methods' => WP_REST_Server::EDITABLE, + 'callback' => array($this, 'update_item'), + 'permission_callback' => array($this, 'update_item_permissions_check') ) )); } @@ -150,6 +155,34 @@ class TAINACAN_REST_Filters_Controller extends WP_REST_Controller { $filter = $this->filter_repository->fetch($request['filter_id']); return $this->filter_repository->can_delete($filter); } -} + public function update_item( $request ) { + $filter_id = $request['filter_id']; + + $body = json_decode($request->get_body(), true); + + if(!empty($body)){ + $attributes = ['ID' => $filter_id]; + + foreach ($body as $att => $value){ + $attributes[$att] = $value; + } + + $updated_filter = $this->filter_repository->update($attributes); + + return new WP_REST_Response($updated_filter->__toArray(), 200); + } + + return new WP_REST_Response([ + 'error_message' => 'The body could not be empty', + 'body' => $body + ], 400); + + } + + public function update_item_permissions_check( $request ) { + $filter = $this->filter_repository->fetch($request['filter_id']); + return $this->filter_repository->can_edit($filter); + } +} ?> \ No newline at end of file diff --git a/src/classes/repositories/class-tainacan-filters.php b/src/classes/repositories/class-tainacan-filters.php index 960958d5a..0084df1f2 100644 --- a/src/classes/repositories/class-tainacan-filters.php +++ b/src/classes/repositories/class-tainacan-filters.php @@ -168,7 +168,19 @@ class Filters extends Repository { } public function update($object){ + $map = $this->get_map(); + $entity = []; + + foreach ($object as $key => $value) { + if($key != 'ID') { + $entity[$map[$key]['map']] = $value ; + } elseif ($key == 'ID'){ + $entity[$key] = (int) $value; + } + } + + return new Entities\Filter(wp_update_post($entity)); } /** diff --git a/tests/test-api-filters.php b/tests/test-api-filters.php index a072c943f..81fe8717c 100644 --- a/tests/test-api-filters.php +++ b/tests/test-api-filters.php @@ -116,6 +116,55 @@ class TAINACAN_REST_Terms_Controller extends TAINACAN_UnitApiTestCase { $this->assertNull(get_post($filter->get_id())); } + public function test_update_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', + ) + ); + + $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, + ), + true + ); + + $new_attributes = json_encode([ + 'name' => 'Faceta', + ]); + + $request = new \WP_REST_Request( + 'PATCH', $this->namespace . '/filters/' . $filter->get_id() + ); + + $request->set_body($new_attributes); + + $response = $this->server->dispatch($request); + + $data = $response->get_data(); + + $this->assertNotEquals('filtro', $data['name']); + $this->assertEquals('Faceta', $data['name']); + } } ?> \ No newline at end of file