Update collection
This commit is contained in:
parent
eac7db72c4
commit
ec07dd9cd2
|
@ -252,7 +252,26 @@ class TAINACAN_REST_Collections_Controller extends WP_REST_Controller {
|
|||
* @return string|WP_Error|WP_REST_Response
|
||||
*/
|
||||
public function update_item( $request ) {
|
||||
return 'Não implementado';
|
||||
$collection_id = $request['collection_id'];
|
||||
|
||||
$body = json_decode($request->get_body(), true);
|
||||
|
||||
if(!empty($body)){
|
||||
$attributes = ['ID' => $collection_id];
|
||||
|
||||
foreach ($body as $att => $value){
|
||||
$attributes[$att] = $value;
|
||||
}
|
||||
|
||||
$updated_collection = $this->collections_repository->update($attributes);
|
||||
|
||||
return new WP_REST_Response($updated_collection->__toArray(), 200);
|
||||
}
|
||||
|
||||
return new WP_REST_Response([
|
||||
'error_message' => 'The body could not be empty',
|
||||
'body' => $body
|
||||
], 400);
|
||||
}
|
||||
|
||||
|
||||
|
@ -264,11 +283,8 @@ class TAINACAN_REST_Collections_Controller extends WP_REST_Controller {
|
|||
* @return bool|WP_Error
|
||||
*/
|
||||
public function update_item_permissions_check( $request ) {
|
||||
if(current_user_can('edit_posts')){
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
$collection = $this->collections_repository->fetch($request['collection_id']);
|
||||
return $this->collections_repository->can_edit($collection);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -168,7 +168,19 @@ class Collections 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\Collection(wp_update_post($entity));
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -129,6 +129,37 @@ class TAINACAN_REST_Collections_Controller extends TAINACAN_UnitApiTestCase {
|
|||
|
||||
$this->assertNotEmpty($post_meta);
|
||||
}
|
||||
|
||||
public function test_update_collection(){
|
||||
$collection = $this->tainacan_entity_factory->create_entity(
|
||||
'collection',
|
||||
array(
|
||||
'name' => 'testeApi',
|
||||
'description' => 'adasdasdsa',
|
||||
'default_order' => 'DESC',
|
||||
'status' => 'publish'
|
||||
),
|
||||
true
|
||||
);
|
||||
|
||||
$new_values = json_encode([
|
||||
'name' => 'Test API',
|
||||
'description' => 'Collection for test.'
|
||||
]);
|
||||
|
||||
$request = new \WP_REST_Request(
|
||||
'PATCH', $this->namespace . '/collections/' . $collection->get_id()
|
||||
);
|
||||
|
||||
$request->set_body($new_values);
|
||||
|
||||
$response = $this->server->dispatch($request);
|
||||
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertNotEquals($collection->get_name(), $data['name']);
|
||||
$this->assertEquals('Test API', $data['name']);
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
|
|
Loading…
Reference in New Issue