Refactoring update methods

This commit is contained in:
weryques 2018-02-15 14:43:00 -02:00
parent 6fc655e5b6
commit 816ef577c8
13 changed files with 147 additions and 128 deletions

View File

@ -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

View File

@ -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);
}

View File

@ -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);
}

View File

@ -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);

View File

@ -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);
}

View File

@ -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);
}

View File

@ -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);
}

View File

@ -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);
}
/**

View File

@ -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){

View File

@ -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);
}
/**

View File

@ -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);
}
/**

View File

@ -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){

View File

@ -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){