Refactoring fields controller

This commit is contained in:
weryques 2018-03-06 13:06:46 -03:00
parent 75a05524f3
commit 404e0a5426
2 changed files with 26 additions and 59 deletions

View File

@ -388,7 +388,7 @@ class TAINACAN_REST_Fields_Controller extends TAINACAN_REST_Controller {
$collection_id = $request['collection_id'];
$body = json_decode($request->get_body(), true);
if(!empty($body) && $collection_id){
if(!empty($body)){
$attributes = [];
$field_id = $request['field_id'];
@ -399,31 +399,32 @@ class TAINACAN_REST_Fields_Controller extends TAINACAN_REST_Controller {
$field = $this->field_repository->fetch($field_id);
$error_message = __('Field with that ID not found', 'tainacan');
if($field){
// These conditions are for verify if endpoints are used correctly
if(!$collection_id && $field->get_collection_id() !== 'default') {
$error_message = __('That field is not a default field', 'tainacan');
return new WP_REST_Response( [
'error_message' => $error_message,
'field_id' => $field_id
] );
} elseif ($collection_id && $field->get_collection_id() === 'default'){
$error_message = __('That field is not a collection field', 'tainacan');
return new WP_REST_Response( [
'error_message' => $error_message,
'field_id' => $field_id
] );
}
$prepared_metadata = $this->prepare_item_for_updating($field, $attributes);
if($prepared_metadata->validate()){
$updated_metadata = $this->field_repository->update($prepared_metadata);
$items = $this->item_repository->fetch([], $collection_id, 'WP_Query');
$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);
}
$response = $this->prepare_item_for_response($up_metadata->get_field(), $request);
return new WP_REST_Response($response, 200);
}
$response = $this->prepare_item_for_response($updated_metadata, $request);
return new WP_REST_Response($response, 200);
@ -436,44 +437,10 @@ class TAINACAN_REST_Fields_Controller extends TAINACAN_REST_Controller {
], 400);
}
return new WP_REST_Response([
'error_message' => __('Field with that ID not found', 'tainacan'),
return new WP_REST_Response( [
'error_message' => $error_message,
'field_id' => $field_id
], 400);
} elseif (!empty($body)){
$attributes = [];
$field_id = $request['field_id'];
foreach ($body as $att => $value){
$attributes[$att] = $value;
}
$field = $this->field_repository->fetch($field_id);
if($field && $field->get_collection_id() === 'default') {
$prepared_metadata = $this->prepare_item_for_updating( $field, $attributes );
if ( $prepared_metadata->validate() ) {
$updated_metadata = $this->field_repository->update( $prepared_metadata );
$response = $this->prepare_item_for_response($updated_metadata, $request);
return new WP_REST_Response($response, 200);
}
return new WP_REST_Response([
'error_message' => __('One or more values are invalid.', 'tainacan'),
'errors' => $prepared_metadata->get_errors(),
'metadata' => $this->prepare_item_for_response($prepared_metadata, $request)
], 400);
}
return new WP_REST_Response([
'error_message' => __('Field with that ID not found or that field is not a default field', 'tainacan'),
'field_id' => $field_id
], 400);
] );
}
return new WP_REST_Response([

View File

@ -206,10 +206,10 @@ class TAINACAN_REST_Item_Metadata_Controller extends TAINACAN_REST_Controller {
if ( $item_metadata->validate() ) {
if($item->can_edit()) {
$field_updated = $this->item_metadata_repository->update( $item_metadata );
$field_updated = $this->item_metadata_repository->update( $item_metadata );
$prepared_item = $this->prepare_item_for_response($field_updated, $request);
$prepared_item['field']['field_type_object'] = $this->prepare_item_for_response($field_updated->get_field()->get_field_type_object(), $request);
$prepared_item = $this->prepare_item_for_response($field_updated, $request);
$prepared_item['field']['field_type_object'] = $this->prepare_item_for_response($field_updated->get_field()->get_field_type_object(), $request);
}
elseif($field->get_accept_suggestion()) {
$log = $this->item_metadata_repository->suggest( $item_metadata );