Added error messages

This commit is contained in:
weryques 2017-12-13 13:07:01 -02:00
parent fbc84371c5
commit e4175f83ab
6 changed files with 89 additions and 33 deletions

View File

@ -169,7 +169,13 @@ class TAINACAN_REST_Collections_Controller extends WP_REST_Controller {
return new WP_REST_Response($collection->__toJSON(), 201); return new WP_REST_Response($collection->__toJSON(), 201);
} }
return new WP_REST_Response($prepared_post->get_errors(), 400); return new WP_REST_Response([
[
'error_message' => __('One or more values are invalid.', 'tainacan'),
'errors' => $prepared_post->get_errors()
],
$prepared_post->__toJSON()
], 400);
} }
/** /**

View File

@ -196,7 +196,14 @@ class TAINACAN_REST_Items_Controller extends WP_REST_Controller {
return new WP_REST_Response($metadata_added->get_item()->__toJSON(), 201 ); return new WP_REST_Response($metadata_added->get_item()->__toJSON(), 201 );
} }
return new WP_REST_Response($item->get_errors(), 400);
return new WP_REST_Response([
[
'error_message' => __('One or more values are invalid.', 'tainacan'),
'errors' => $item->get_errors()
],
$item->__toJSON()
], 400);
} }
/** /**

View File

@ -45,6 +45,11 @@ class TAINACAN_REST_Metadata_Controller extends WP_REST_Controller {
'callback' => array($this, 'create_item'), 'callback' => array($this, 'create_item'),
'permission_callback' => array($this, 'create_item_permissions_check') 'permission_callback' => array($this, 'create_item_permissions_check')
), ),
array(
'methods' => WP_REST_Server::DELETABLE,
'callback' => array($this, 'delete_item'),
'permission_callback' => array($this, 'delete_teim_permissions_check')
)
) )
); );
register_rest_route($this->namespace, '/' . $this->rest_base . '/item/(?P<item_id>[\d]+)', register_rest_route($this->namespace, '/' . $this->rest_base . '/item/(?P<item_id>[\d]+)',
@ -119,7 +124,13 @@ class TAINACAN_REST_Metadata_Controller extends WP_REST_Controller {
return new WP_REST_Response($this->metadata->__toJSON(), 201); return new WP_REST_Response($this->metadata->__toJSON(), 201);
} }
} else { } else {
return new WP_REST_Response($this->metadata->get_errors(), 200); return new WP_REST_Response([
[
'error_message' => __('One or more values are invalid.', 'tainacan'),
'errors' => $this->metadata->get_errors()
],
$this->metadata->__toJSON(),
], 400);
} }
} elseif (!empty($request['item_id']) && !empty($request->get_body())){ } elseif (!empty($request['item_id']) && !empty($request->get_body())){
$body = json_decode($request->get_body(), true); $body = json_decode($request->get_body(), true);
@ -139,7 +150,13 @@ class TAINACAN_REST_Metadata_Controller extends WP_REST_Controller {
return new WP_REST_Response( $metadata_updated->__toJSON(), 201 ); return new WP_REST_Response( $metadata_updated->__toJSON(), 201 );
} else { } else {
return new WP_REST_Response( $item_metadata->get_errors(), 200); return new WP_REST_Response( [
[
'error_message' => __('One or more values are invalid.', 'tainacan'),
'errors' => $item_metadata->get_errors()
],
$item_metadata->__toJSON(),
], 400);
} }
} else { } else {
return new WP_REST_Response($request->get_body(), 400); return new WP_REST_Response($request->get_body(), 400);
@ -155,13 +172,7 @@ class TAINACAN_REST_Metadata_Controller extends WP_REST_Controller {
if($request['item_id']) { if($request['item_id']) {
foreach ( $item as $metadata ) { foreach ( $item as $metadata ) {
$metadata_as_json[] = $metadata->__toJSON();
$metadata_as_json[] = json_encode(
array_merge(
json_decode($metadata->get_metadata()->__toJSON(), true),
['value' => $metadata->get_value()]
)
);
} }
} else { } else {
foreach ( $item as $metadata ) { foreach ( $item as $metadata ) {
@ -182,20 +193,18 @@ class TAINACAN_REST_Metadata_Controller extends WP_REST_Controller {
$prepared_item = $this->prepare_item_for_response($collection_metadata, $request); $prepared_item = $this->prepare_item_for_response($collection_metadata, $request);
return new WP_REST_Response($prepared_item, 200);
} elseif(!empty($request['item_id'])){
$item_id = $request['item_id'];
$item = new Entities\Item($item_id);
$item_metadata = $this->item_metadata_repository->fetch($item, 'OBJECT');
$prepared_item = $this->prepare_item_for_response($item_metadata, $request);
return new WP_REST_Response($prepared_item, 200); return new WP_REST_Response($prepared_item, 200);
} }
return new WP_REST_Response($request->get_body(), 400); $item_id = $request['item_id'];
$item = new Entities\Item($item_id);
$item_metadata = $this->item_metadata_repository->fetch($item, 'OBJECT');
$prepared_item = $this->prepare_item_for_response($item_metadata, $request);
return new WP_REST_Response($prepared_item, 200);
} }
public function get_item_permissions_check( $request ) { public function get_item_permissions_check( $request ) {
@ -210,6 +219,20 @@ class TAINACAN_REST_Metadata_Controller extends WP_REST_Controller {
return parent::get_collection_params(); // TODO: Change the autogenerated stub return parent::get_collection_params(); // TODO: Change the autogenerated stub
} }
public function delete_item( $request ) {
if(!empty($request->get_body())){
$body = json_decode($request->get_body());
$collection_id = $request['collection_id'];
$metadata_id = $body['metadata_id'];
return new WP_REST_Response(['error' => 'Not Implemented.'], 400);
}
}
public function delete_item_permissions_check( $request ) {
return parent::delete_item_permissions_check( $request ); // TODO: Change the autogenerated stub
}
} }
?> ?>

View File

@ -27,9 +27,10 @@ class Item_Metadata_Entity extends Entity {
} }
public function __toJSON(){ public function __toJSON(){
$json['value'] = $this->get_value(); $json['value'] = $this->get_value();
$json['item'] = $this->get_item(); $json['item'] = $this->get_item()->__toJSON();
$json['metadata'] = $this->get_metadata(); $json['metadata'] = $this->get_metadata()->__toJSON();
return json_encode($json); return json_encode($json);
} }

View File

@ -17,7 +17,9 @@ class Item_Metadata extends Repository {
delete_post_meta($item_metadata->item->get_id(), $item_metadata->metadata->get_id()); delete_post_meta($item_metadata->item->get_id(), $item_metadata->metadata->get_id());
if (is_array($item_metadata->get_value())){ if (is_array($item_metadata->get_value())){
foreach ($item_metadata->get_value() as $value){ $values = $item_metadata->get_value();
foreach ($values as $value){
add_post_meta($item_metadata->item->get_id(), $item_metadata->metadata->get_id(), wp_slash( $value )); add_post_meta($item_metadata->item->get_id(), $item_metadata->metadata->get_id(), wp_slash( $value ));
} }
} }
@ -34,8 +36,23 @@ class Item_Metadata extends Repository {
} }
public function delete($object){ /**
* Delete Item Metadata
*
* @param $item_metadata
*
* @return mixed|void
*/
public function delete($item_metadata){
// if(is_array($item_metadata->get_value())){
// $values = $item_metadata->get_value();
//
// foreach ($values as $value){
// delete_post_meta($item_metadata->item->get_id(), $item_metadata->metadata->get_id(), wp_slash($value));
// }
// } else {
// delete_post_meta($item_metadata->item->get_id(), $item_metadata->metadata->get_id(), wp_slash($item_metadata->get_value()));
// }
} }
/** /**

View File

@ -58,11 +58,12 @@ class TAINACAN_REST_Metadata_Controller extends TAINACAN_UnitApiTestCase {
$response = $this->server->dispatch($request); $response = $this->server->dispatch($request);
$metadata_updated = json_decode($response->get_data(), true); $item_metadata_updated = json_decode($response->get_data(), true);
$metadata = json_decode($item_metadata_updated['metadata'], true);
$this->assertEquals($metadata_added['id'], $metadata_updated['id']); $this->assertEquals($metadata_added['id'], $metadata['id']);
$metav = get_post_meta($item->get_id(), $metadata_updated['id'], true); $metav = get_post_meta($item->get_id(), $metadata['id'], true);
$this->assertEquals('Valorado', $metav); $this->assertEquals('Valorado', $metav);
} }
@ -139,10 +140,11 @@ class TAINACAN_REST_Metadata_Controller extends TAINACAN_UnitApiTestCase {
$this->assertContainsOnly('string', $data); $this->assertContainsOnly('string', $data);
$metadata = json_decode($data[0], true); $item_metadata = json_decode($data[0], true);
$metadata = json_decode($item_metadata['metadata'], true);
$this->assertEquals('Data', $metadata['name']); $this->assertEquals('Data', $metadata['name']);
$this->assertEquals('12/12/2017', $metadata['value']); $this->assertEquals('12/12/2017', $item_metadata['value']);
} }
} }