diff --git a/src/api/class-tainacan-rest-controller.php b/src/api/class-tainacan-rest-controller.php index 0418aacec..c730231f0 100644 --- a/src/api/class-tainacan-rest-controller.php +++ b/src/api/class-tainacan-rest-controller.php @@ -2,6 +2,25 @@ class TAINACAN_REST_Controller extends WP_REST_Controller { + /** + * @param $object + * @param $new_values + * + * @return Tainacan\Entities\Entity + */ + protected function prepare_item_for_updating($object, $new_values){ + + foreach ($new_values as $key => $value) { + try { + $set_ = 'set_' . $key; + $object->$set_( $value ); + } catch (\Error $error){ + // Do nothing + } + } + + return $object; + } /** * @param $entity diff --git a/src/api/endpoints/class-tainacan-rest-collections-controller.php b/src/api/endpoints/class-tainacan-rest-collections-controller.php index 61a47c78d..d87d58dc6 100644 --- a/src/api/endpoints/class-tainacan-rest-collections-controller.php +++ b/src/api/endpoints/class-tainacan-rest-collections-controller.php @@ -293,17 +293,30 @@ class TAINACAN_REST_Collections_Controller extends TAINACAN_REST_Controller { $collection = $this->collections_repository->fetch($collection_id); - $updated_collection = $this->collections_repository->update($collection, $attributes); + if($collection) { + $collection_prepared = $this->prepare_item_for_updating( $collection, $attributes ); - if(!($updated_collection instanceof Entities\Collection)){ - return new WP_REST_Response($updated_collection, 400); + if ( $collection_prepared->validate() ) { + $updated_collection = $this->collections_repository->update( $collection ); + + return new WP_REST_Response( $updated_collection->__toArray(), 200 ); + } + + return new WP_REST_Response([ + 'error_message' => __('One or more values are invalid.', 'tainacan'), + 'errors' => $collection_prepared->get_errors(), + 'collection' => $collection_prepared->__toArray() + ], 400); } - return new WP_REST_Response($updated_collection->__toArray(), 200); + return new WP_REST_Response([ + 'error_message' => __('Collection with that ID not found', 'tainacan' ), + 'collection_id' => $collection_id + ], 400); } return new WP_REST_Response([ - 'error_message' => 'The body could not be empty', + 'error_message' => __('The body could not be empty', 'tainacan'), 'body' => $body ], 400); } diff --git a/src/api/endpoints/class-tainacan-rest-fields-controller.php b/src/api/endpoints/class-tainacan-rest-fields-controller.php index 4643ae07b..4631b1e99 100644 --- a/src/api/endpoints/class-tainacan-rest-fields-controller.php +++ b/src/api/endpoints/class-tainacan-rest-fields-controller.php @@ -264,33 +264,47 @@ class TAINACAN_REST_Fields_Controller extends TAINACAN_REST_Controller { $field = $this->field_repository->fetch($field_id); - $updated_metadata = $this->field_repository->update($field, $attributes); + if($field){ - if(!($updated_metadata instanceof Entities\Field)){ - return new WP_REST_Response($updated_metadata, 400); - } + $prepared_metadata = $this->prepare_item_for_updating($field, $attributes); - $items = $this->item_repository->fetch([], $collection_id, 'WP_Query'); + if($prepared_metadata->validate()){ + $updated_metadata = $this->field_repository->update($prepared_metadata); - $up_metadata = ''; - if($items->have_posts()){ - while ($items->have_posts()){ - $items->the_post(); + $items = $this->item_repository->fetch([], $collection_id, 'WP_Query'); - $item = new Entities\Item($items->post); - $item_meta = new Entities\Item_Metadata_Entity($item, $updated_metadata); + $up_metadata = ''; + if($items->have_posts()){ + while ($items->have_posts()){ + $items->the_post(); - $up_metadata = $this->item_metadata_repository->update($item_meta); + $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_field()->__toArray(), 201); + } + + return new WP_REST_Response($updated_metadata->__toArray(), 201); } - return new WP_REST_Response($up_metadata->get_field()->__toArray(), 201); + return new WP_REST_Response([ + 'error_message' => __('One or more values are invalid.', 'tainacan'), + 'errors' => $prepared_metadata->get_errors(), + 'metadata' => $prepared_metadata->__toArray() + ], 400); } - return new WP_REST_Response($updated_metadata->__toArray(), 201); + return new WP_REST_Response([ + 'error_message' => __('Field with that ID not found', 'tainacan'), + 'field_id' => $field_id + ], 400); } return new WP_REST_Response([ - 'error_message' => 'The body could not be empty', + 'error_message' => __('The body could not be empty', 'tainacan'), 'body' => $body ], 400); } diff --git a/src/api/endpoints/class-tainacan-rest-filters-controller.php b/src/api/endpoints/class-tainacan-rest-filters-controller.php index 26211f931..d28f1dc1b 100644 --- a/src/api/endpoints/class-tainacan-rest-filters-controller.php +++ b/src/api/endpoints/class-tainacan-rest-filters-controller.php @@ -215,17 +215,31 @@ class TAINACAN_REST_Filters_Controller extends TAINACAN_REST_Controller { $filter = $this->filter_repository->fetch($filter_id); - $updated_filter = $this->filter_repository->update($filter, $attributes); + if($filter) { + $prepared_filter = $this->prepare_item_for_updating($filter, $attributes); - if(!($updated_filter instanceof Entities\Filter)){ - return new WP_REST_Response($updated_filter, 400); + if($prepared_filter->validate()) { + $updated_filter = $this->filter_repository->update( $prepared_filter ); + + return new WP_REST_Response($updated_filter->__toArray(), 200); + } + + return new WP_REST_Response([ + 'error_message' => __('One or more values are invalid.', 'tainacan'), + 'errors' => $prepared_filter->get_errors(), + 'filters' => $prepared_filter->__toArray() + ], 400); } - return new WP_REST_Response($updated_filter->__toArray(), 200); + return new WP_REST_Response([ + 'error_message' => __('Filter with that ID not found', 'tainacan' ), + 'filter_id' => $filter_id + ], 400); + } return new WP_REST_Response([ - 'error_message' => 'The body could not be empty', + 'error_message' => __('The body could not be empty', 'tainacan'), 'body' => $body ], 400); diff --git a/src/api/endpoints/class-tainacan-rest-items-controller.php b/src/api/endpoints/class-tainacan-rest-items-controller.php index 0c638f218..ed1a1b139 100644 --- a/src/api/endpoints/class-tainacan-rest-items-controller.php +++ b/src/api/endpoints/class-tainacan-rest-items-controller.php @@ -295,17 +295,30 @@ class TAINACAN_REST_Items_Controller extends TAINACAN_REST_Controller { $item = $this->items_repository->fetch($item_id); - $updated_item = $this->items_repository->update($item, $attributes); + if($item){ + $prepared_item = $this->prepare_item_for_updating($item, $attributes); - if(!($updated_item instanceof Entities\Item)){ - return new WP_REST_Response($updated_item, 400); + if($prepared_item->validate()){ + $updated_item = $this->items_repository->update($prepared_item); + + return new WP_REST_Response($updated_item->__toArray(), 200); + } + + return new WP_REST_Response([ + 'error_message' => __('One or more values are invalid.', 'tainacan'), + 'errors' => $prepared_item->get_errors(), + 'item' => $prepared_item->__toArray() + ], 400); } - return new WP_REST_Response($updated_item->__toArray(), 200); + return new WP_REST_Response([ + 'error_message' => __('Item with that ID not found', 'tainacan' ), + 'item_id' => $item_id + ], 400); } return new WP_REST_Response([ - 'error_message' => 'The body could not be empty', + 'error_message' => __('The body could not be empty', 'tainacan'), 'body' => $body ], 400); } diff --git a/src/api/endpoints/class-tainacan-rest-taxonomies-controller.php b/src/api/endpoints/class-tainacan-rest-taxonomies-controller.php index 91c94cc74..a0d5b934a 100644 --- a/src/api/endpoints/class-tainacan-rest-taxonomies-controller.php +++ b/src/api/endpoints/class-tainacan-rest-taxonomies-controller.php @@ -286,17 +286,30 @@ class TAINACAN_REST_Taxonomies_Controller extends TAINACAN_REST_Controller { $taxonomy = $this->taxonomy_repository->fetch($taxonomy_id); - $updated_taxonomy = $this->taxonomy_repository->update($taxonomy, $attributes); + if($taxonomy){ + $prepared_taxonomy = $this->prepare_item_for_updating($taxonomy, $attributes); - if(!($updated_taxonomy instanceof Entities\Taxonomy)){ - return new WP_REST_Response($updated_taxonomy, 400); + if($prepared_taxonomy->validate()){ + $updated_taxonomy = $this->taxonomy_repository->update($prepared_taxonomy); + + return new WP_REST_Response($updated_taxonomy->__toArray(), 200); + } + + return new WP_REST_Response([ + 'error_message' => __('One or more values are invalid.', 'tainacan'), + 'errors' => $prepared_taxonomy->get_errors(), + 'taxonomy' => $prepared_taxonomy->__toArray() + ], 400); } - return new WP_REST_Response($updated_taxonomy->__toArray(), 200); + return new WP_REST_Response([ + 'error_message' => __('Taxonomy with that ID not found', 'tainacan' ), + 'taxonomy_id' => $taxonomy_id + ], 400); } return new WP_REST_Response([ - 'error_message' => 'The body could not be empty', + 'error_message' => __('The body could not be empty', 'tainacan'), 'body' => $body ], 400); } diff --git a/src/api/endpoints/class-tainacan-rest-terms-controller.php b/src/api/endpoints/class-tainacan-rest-terms-controller.php index 9f339e896..b00a94182 100644 --- a/src/api/endpoints/class-tainacan-rest-terms-controller.php +++ b/src/api/endpoints/class-tainacan-rest-terms-controller.php @@ -201,17 +201,31 @@ class TAINACAN_REST_Terms_Controller extends TAINACAN_REST_Controller { $term = $this->terms_repository->fetch($term_id, $taxonomy); - $updated_term = $this->terms_repository->update([$term, $tax_name], $attributes); + if($term){ + $prepared_term = $this->prepare_item_for_updating($term, $attributes); - if(!($updated_term instanceof Entities\Term)){ - return new WP_REST_Response($updated_term, 400); + if($prepared_term->validate()){ + $updated_term = $this->terms_repository->update($prepared_term, $tax_name); + + return new WP_REST_Response($updated_term->__toArray(), 200); + } + + return new WP_REST_Response([ + 'error_message' => __('One or more values are invalid.', 'tainacan'), + 'errors' => $prepared_term->get_errors(), + 'term' => $prepared_term->__toArray() + ], 400); } - return new WP_REST_Response($updated_term->__toArray(), 200); + return new WP_REST_Response([ + 'error_message' => __('Term or Taxonomy with that IDs not found', 'tainacan' ), + 'term_id' => $term_id, + 'taxonomy_id' => $taxonomy_id + ], 400); } return new WP_REST_Response([ - 'error_message' => 'The body could not be empty', + 'error_message' => __('The body could not be empty', 'tainacan'), 'body' => $body ], 400); } diff --git a/src/classes/repositories/class-tainacan-collections.php b/src/classes/repositories/class-tainacan-collections.php index 0b5524cb8..d0d4f970e 100644 --- a/src/classes/repositories/class-tainacan-collections.php +++ b/src/classes/repositories/class-tainacan-collections.php @@ -224,20 +224,7 @@ class Collections extends Repository { } public function update($object, $new_values = null){ - foreach ($new_values as $key => $value) { - try { - $set_ = 'set_' . $key; - $object->$set_( $value ); - } catch (\Error $error){ - return $error->getMessage(); - } - } - - if($object->validate()){ - return $this->insert($object); - } - - return $object->get_errors(); + return $this->insert($object); } /** diff --git a/src/classes/repositories/class-tainacan-fields.php b/src/classes/repositories/class-tainacan-fields.php index a55e9a091..518a9e1c6 100644 --- a/src/classes/repositories/class-tainacan-fields.php +++ b/src/classes/repositories/class-tainacan-fields.php @@ -359,20 +359,7 @@ class Fields extends Repository { * @throws \Exception */ public function update($object, $new_values = null){ - foreach ($new_values as $key => $value) { - try { - $set_ = 'set_' . $key; - $object->$set_( $value ); - } catch (\Error $error){ - return $error->getMessage(); - } - } - - if($object->validate()){ - return $this->insert($object); - } - - return $object->get_errors(); + return $this->insert($object); } public function delete($object){ diff --git a/src/classes/repositories/class-tainacan-filters.php b/src/classes/repositories/class-tainacan-filters.php index 137b5f805..dc6e9dc2a 100644 --- a/src/classes/repositories/class-tainacan-filters.php +++ b/src/classes/repositories/class-tainacan-filters.php @@ -169,20 +169,7 @@ class Filters extends Repository { } public function update($object, $new_values = null){ - foreach ($new_values as $key => $value) { - try { - $set_ = 'set_' . $key; - $object->$set_( $value ); - } catch (\Error $error){ - return $error->getMessage(); - } - } - - if($object->validate()){ - return $this->insert($object); - } - - return $object->get_errors(); + return $this->insert($object); } /** diff --git a/src/classes/repositories/class-tainacan-items.php b/src/classes/repositories/class-tainacan-items.php index 19f03e3ae..4bcb88048 100644 --- a/src/classes/repositories/class-tainacan-items.php +++ b/src/classes/repositories/class-tainacan-items.php @@ -232,20 +232,7 @@ class Items extends Repository { } public function update($object, $new_values = null){ - foreach ($new_values as $key => $value) { - try { - $set_ = 'set_' . $key; - $object->$set_( $value ); - } catch (\Error $error){ - return $error->getMessage(); - } - } - - if($object->validate()){ - return $this->insert($object); - } - - return $object->get_errors(); + return $this->insert($object); } /** diff --git a/src/classes/repositories/class-tainacan-taxonomies.php b/src/classes/repositories/class-tainacan-taxonomies.php index 24518f01c..5098bc379 100644 --- a/src/classes/repositories/class-tainacan-taxonomies.php +++ b/src/classes/repositories/class-tainacan-taxonomies.php @@ -186,20 +186,7 @@ class Taxonomies extends Repository { } public function update($object, $new_values = null){ - foreach ($new_values as $key => $value) { - try { - $set_ = 'set_' . $key; - $object->$set_( $value ); - } catch (\Error $error){ - return $error->getMessage(); - } - } - - if($object->validate()){ - return $this->insert($object); - } - - return $object->get_errors(); + return $this->insert($object); } public function delete($args){ diff --git a/src/classes/repositories/class-tainacan-terms.php b/src/classes/repositories/class-tainacan-terms.php index ad8f27e98..c3f77cd4f 100644 --- a/src/classes/repositories/class-tainacan-terms.php +++ b/src/classes/repositories/class-tainacan-terms.php @@ -159,24 +159,8 @@ class Terms extends Repository { } } - public function update($object, $new_values = null){ - $tax_name = $object[1]; - $object = $object[0]; - - foreach ($new_values as $key => $value) { - try { - $set_ = 'set_' . $key; - $object->$set_( $value ); - } catch (\Error $error){ - return $error->getMessage(); - } - } - - if($object->validate()){ - return new Entities\Term($this->insert($object), $tax_name); - } - - return $object->get_errors(); + public function update($object, $tax_name = null){ + return new Entities\Term($this->insert($object), $tax_name); } public function delete($args){