Update collection metadata

This commit is contained in:
weryques 2018-01-19 14:56:36 -02:00
parent 93af8fc5c2
commit 856ea0b4a7
5 changed files with 109 additions and 18 deletions

View File

@ -223,6 +223,7 @@ class TAINACAN_REST_Items_Controller extends WP_REST_Controller {
* @param WP_REST_Request $request
*
* @return bool|WP_Error
* @throws Exception
*/
public function create_item_permissions_check( $request ) {
return $this->items_repository->can_edit($this->item);
@ -250,6 +251,7 @@ class TAINACAN_REST_Items_Controller extends WP_REST_Controller {
* @param WP_REST_Request $request
*
* @return bool|WP_Error
* @throws Exception
*/
public function delete_item_permissions_check( $request ) {
$item = $this->items_repository->fetch($request['item_id']);
@ -288,6 +290,7 @@ class TAINACAN_REST_Items_Controller extends WP_REST_Controller {
* @param WP_REST_Request $request
*
* @return bool|WP_Error
* @throws Exception
*/
public function update_item_permissions_check( $request ) {
$item = $this->items_repository->fetch($request['item_id']);

View File

@ -51,6 +51,11 @@ class TAINACAN_REST_Metadata_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')
)
)
);
@ -173,6 +178,7 @@ class TAINACAN_REST_Metadata_Controller extends WP_REST_Controller {
* @param $request
*
* @return bool|WP_Error
* @throws Exception
*/
public function create_item_permissions_check( $request ) {
if(!empty($request['item_id'])){
@ -238,6 +244,7 @@ class TAINACAN_REST_Metadata_Controller extends WP_REST_Controller {
* @param WP_REST_Request $request
*
* @return bool|WP_Error
* @throws Exception
*/
public function get_items_permissions_check( $request ) {
if(!empty($request['item_id'])){
@ -274,6 +281,7 @@ class TAINACAN_REST_Metadata_Controller extends WP_REST_Controller {
* @param WP_REST_Request $request
*
* @return bool|WP_Error
* @throws Exception
*/
public function delete_item_permissions_check( $request ) {
if(!empty($request['item_id'])){
@ -289,38 +297,78 @@ class TAINACAN_REST_Metadata_Controller extends WP_REST_Controller {
* @return WP_Error|WP_REST_Response
*/
public function update_item( $request ) {
if($request['item_id']) {
$body = json_decode( $request->get_body(), true );
$item_id = $request['item_id'];
$metadata_id = $body['metadata_id'];
$value = $body['values'];
$item = $this->item_repository->fetch( $item_id );
$metadata = $this->metadata_repository->fetch( $metadata_id );
$item_metadata = new Entities\Item_Metadata_Entity( $item, $metadata );
$item_metadata->set_value( $value );
if ( $item_metadata->validate() ) {
$metadata_updated = $this->item_metadata_repository->update( $item_metadata );
return new WP_REST_Response( $metadata_updated->__toArray(), 200 );
} else {
return new WP_REST_Response( [
'error_message' => __( 'One or more values are invalid.', 'tainacan' ),
'errors' => $item_metadata->get_errors(),
'item_metadata' => $item_metadata->__toArray(),
], 400 );
}
}
$collection_id = $request['collection_id'];
$body = json_decode($request->get_body(), true);
$item_id = $request['item_id'];
$metadata_id = $body['metadata_id'];
$value = $body['values'];
if(!empty($body)){
$attributes = ['ID' => $body['metadata_id']];
$item = $this->item_repository->fetch($item_id);
$metadata = $this->metadata_repository->fetch($metadata_id);
foreach ($body['values'] as $att => $value){
$attributes[$att] = $value;
}
$item_metadata = new Entities\Item_Metadata_Entity($item, $metadata);
$item_metadata->set_value($value);
$updated_metadata = $this->metadata_repository->update($attributes);
if($item_metadata->validate()) {
$metadata_updated = $this->item_metadata_repository->update( $item_metadata );
$items = $this->item_repository->fetch([], $collection_id, 'WP_Query');
return new WP_REST_Response( $metadata_updated->__toArray(), 200 );
} else {
return new WP_REST_Response([
'error_message' => __('One or more values are invalid.', 'tainacan'),
'errors' => $item_metadata->get_errors(),
'item_metadata' => $item_metadata->__toArray(),
], 400);
$up_metadata = '';
if($items->have_posts()){
while ($items->have_posts()){
$items->the_post();
$item = new Entities\Item($items->post);
$item_meta = new Entities\Item_Metadata_Entity($item, $updated_metadata);
$up_metadata = $this->item_metadata_repository->update($item_meta);
}
return new WP_REST_Response($up_metadata->get_metadata()->__toArray(), 201);
}
return new WP_REST_Response($updated_metadata->__toArray(), 201);
}
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
* @throws Exception
*/
public function update_item_permissions_check( $request ) {
$item = $this->item_repository->fetch($request['item_id']);
$item = $this->item_repository->fetch($request['item_id'] ? $request['item_id'] : $request['collection_id']);
return $this->item_repository->can_edit($item);
}
}

View File

@ -284,7 +284,19 @@ class Metadatas 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\Metadata(wp_update_post($entity));
}
public function delete($object){

View File

@ -2,7 +2,6 @@
return [
'tests_dir' => '/tmp/wordpress-tests-lib',
'tests_url' => 'http://localhost/wordpress-test-folder/wordpress-test/'
];
?>

View File

@ -176,6 +176,35 @@ class TAINACAN_REST_Metadata_Controller extends TAINACAN_UnitApiTestCase {
$metav = get_post_meta($item->get_id(), $metadata_updated['id'], true);
$this->assertEquals('19/01/2018', $metav);
#### UPDATE METADATA IN COLLECTION ####
$values = json_encode([
'metadata_id' => $metadata->get_id(),
'values' => [
'name' => 'Dia/Mês/Ano',
'description' => 'Continua descrevendo o dado do campo.'
]
]);
$request = new \WP_REST_Request(
'PATCH',
$this->namespace . '/metadata/collection/' . $collection->get_id()
);
$request->set_body($values);
$response = $this->server->dispatch($request);
$data = $response->get_data();
$this->assertEquals($metadata->get_id(), $data['id']);
// Mantém-se o valor antigo no item
$metav = get_post_meta($item->get_id(), $data['id'], true);
$this->assertEquals('19/01/2018', $metav);
}
}