Update item

This commit is contained in:
weryques 2018-01-16 10:47:29 -02:00
parent ec07dd9cd2
commit e961943e8c
3 changed files with 81 additions and 0 deletions

View File

@ -256,6 +256,43 @@ class TAINACAN_REST_Items_Controller extends WP_REST_Controller {
return $this->items_repository->can_delete($item);
}
/**
* @param WP_REST_Request $request
*
* @return WP_Error|WP_REST_Response
*/
public function update_item( $request ) {
$item_id = $request['item_id'];
$body = json_decode($request->get_body(), true);
if(!empty($body)){
$attributes = ['ID' => $item_id];
foreach ($body as $att => $value){
$attributes[$att] = $value;
}
$updated_item = $this->items_repository->update($attributes);
return new WP_REST_Response($updated_item->__toArray(), 200);
}
return new WP_REST_Response([
'error_message' => 'The body could not be empty',
'body' => $body
], 400);
}
/**
* @param WP_REST_Request $request
*
* @return bool|WP_Error
*/
public function update_item_permissions_check( $request ) {
$item = $this->items_repository->fetch($request['item_id']);
return $this->items_repository->can_edit($item);
}
}
?>

View File

@ -197,7 +197,19 @@ class Items 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\Item(wp_update_post($entity));
}
/**

View File

@ -142,6 +142,38 @@ class TAINACAN_REST_Items_Controller extends TAINACAN_UnitApiTestCase {
$this->assertNull($no_post);
}
public function test_update_item(){
$collection = $this->tainacan_entity_factory->create_entity('collection', '', true);
$item = $this->tainacan_entity_factory->create_entity(
'item',
array(
'title' => 'SCRUM e PMBOK',
'description' => 'Unidos no Gerenciamento de Projetos',
'collection' => $collection,
),
true
);
$new_attributes = json_encode([
'title' => 'SCRUM e XP',
'description' => 'Direto da trincheiras',
]);
$request = new \WP_REST_Request(
'PATCH', $this->namespace . '/items/' . $item->get_id()
);
$request->set_body($new_attributes);
$response = $this->server->dispatch($request);
$data = $response->get_data();
$this->assertNotEquals($item->get_title(), $data['title']);
$this->assertEquals('SCRUM e XP', $data['title']);
}
}
?>