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( $updated_collection->__toArray(), 200 );
}
return new WP_REST_Response([
'error_message' => 'The body could not be empty',
'error_message' => __('One or more values are invalid.', 'tainacan'),
'errors' => $collection_prepared->get_errors(),
'collection' => $collection_prepared->__toArray()
], 400);
}
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', 'tainacan'),
'body' => $body
], 400);
}

View File

@ -264,11 +264,12 @@ 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);
if($prepared_metadata->validate()){
$updated_metadata = $this->field_repository->update($prepared_metadata);
$items = $this->item_repository->fetch([], $collection_id, 'WP_Query');
@ -290,7 +291,20 @@ class TAINACAN_REST_Fields_Controller extends TAINACAN_REST_Controller {
}
return new WP_REST_Response([
'error_message' => 'The body could not be empty',
'error_message' => __('One or more values are invalid.', 'tainacan'),
'errors' => $prepared_metadata->get_errors(),
'metadata' => $prepared_metadata->__toArray()
], 400);
}
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', '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' => 'The body could not be empty',
'error_message' => __('One or more values are invalid.', 'tainacan'),
'errors' => $prepared_filter->get_errors(),
'filters' => $prepared_filter->__toArray()
], 400);
}
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', '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' => 'The body could not be empty',
'error_message' => __('One or more values are invalid.', 'tainacan'),
'errors' => $prepared_item->get_errors(),
'item' => $prepared_item->__toArray()
], 400);
}
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', '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' => 'The body could not be empty',
'error_message' => __('One or more values are invalid.', 'tainacan'),
'errors' => $prepared_taxonomy->get_errors(),
'taxonomy' => $prepared_taxonomy->__toArray()
], 400);
}
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', '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' => 'The body could not be empty',
'error_message' => __('One or more values are invalid.', 'tainacan'),
'errors' => $prepared_term->get_errors(),
'term' => $prepared_term->__toArray()
], 400);
}
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', 'tainacan'),
'body' => $body
], 400);
}

View File

@ -224,22 +224,9 @@ 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();
}
/**
* @param $args ( is a array like [post_id, [is_permanently => bool]] )
*

View File

@ -359,22 +359,9 @@ 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();
}
public function delete($object){
}

View File

@ -169,22 +169,9 @@ 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();
}
/**
* fetch filter based on ID or WP_Query args
*

View File

@ -232,22 +232,9 @@ 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();
}
/**
* @param $args ( is a array like [post_id, [is_permanently => bool]] )
*

View File

@ -186,22 +186,9 @@ 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();
}
public function delete($args){
$taxonomy_id = $args[0];
$taxonomy_name = $args[1];

View File

@ -159,26 +159,10 @@ 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()){
public function update($object, $tax_name = null){
return new Entities\Term($this->insert($object), $tax_name);
}
return $object->get_errors();
}
public function delete($args){
return wp_delete_term($args[0], $args[1]);
}