Update filter

This commit is contained in:
weryques 2018-01-16 11:41:24 -02:00
parent e961943e8c
commit 036d5e2661
3 changed files with 95 additions and 1 deletions

View File

@ -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);
}
}
?>

View File

@ -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));
}
/**

View File

@ -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']);
}
}
?>