Refactoring some controllers

This commit is contained in:
weryques 2018-01-30 15:33:06 -02:00
parent bd0b34cd2c
commit 60bd3753c4
15 changed files with 120 additions and 78 deletions

View File

@ -6,7 +6,7 @@
style="width: 100%"
@selection-change="handleSelectionChange"
stripe>
<el-table-column type="selection" width="30">
<el-table-column type="selection" width="55">
</el-table-column>
<el-table-column width="55">
<template v-if="scope.row.featured_image" slot-scope="scope">

View File

@ -7,8 +7,13 @@
@selection-change="handleSelectionChange">
<el-table-column type="selection" width="55">
</el-table-column>
<el-table-column width="55">
<template v-if="scope.row.featured_image" slot-scope="scope">
<img class="table-thumb" :src="`${scope.row.featured_image}`"/>
</template>
</el-table-column>
<el-table-column label="Nome" show-overflow-tooltip>
<template slot-scope="scope"><router-link :to="`/items/${scope.row.id}`" tag="a">{{ scope.row.name }}</router-link></template>
<template slot-scope="scope"><router-link :to="`/items/${scope.row.id}`" tag="a">{{ scope.row.title }}</router-link></template>
</el-table-column>
<el-table-column property="description" label="Descrição" show-overflow-tooltip>
</el-table-column>
@ -67,7 +72,7 @@ export default {
<style scoped>
.table-thumb {
max-height: 38px !important;
max-height: 55px !important;
vertical-align: middle !important;
}

View File

@ -158,7 +158,7 @@ class TAINACAN_REST_Collections_Controller extends TAINACAN_REST_Controller {
*/
public function get_item_permissions_check($request){
$collection = $this->collections_repository->fetch($request['collection_id']);
return $this->collections_repository->can_read($collection);
return $collection->can_read();
}
/**
@ -257,7 +257,7 @@ class TAINACAN_REST_Collections_Controller extends TAINACAN_REST_Controller {
*/
public function delete_item_permissions_check( $request ) {
$collection = $this->collections_repository->fetch($request['collection_id']);
return $this->collections_repository->can_delete($collection);
return $collection->can_delete();
}
/**
@ -273,13 +273,19 @@ class TAINACAN_REST_Collections_Controller extends TAINACAN_REST_Controller {
$body = json_decode($request->get_body(), true);
if(!empty($body)){
$attributes = ['ID' => $collection_id];
$attributes = [];
foreach ($body as $att => $value){
$attributes[$att] = $value;
}
$updated_collection = $this->collections_repository->update($attributes);
$collection = $this->collections_repository->fetch($collection_id);
$updated_collection = $this->collections_repository->update($collection, $attributes);
if(!($updated_collection instanceof Entities\Collection)){
return new WP_REST_Response($updated_collection, 400);
}
return new WP_REST_Response($updated_collection->__toArray(), 200);
}
@ -301,7 +307,7 @@ class TAINACAN_REST_Collections_Controller extends TAINACAN_REST_Controller {
*/
public function update_item_permissions_check( $request ) {
$collection = $this->collections_repository->fetch($request['collection_id']);
return $this->collections_repository->can_edit($collection);
return $collection->can_edit();
}
/**

View File

@ -147,12 +147,12 @@ class TAINACAN_REST_Items_Controller extends TAINACAN_REST_Controller {
*/
public function get_item_permissions_check( $request ) {
$item = $this->items_repository->fetch($request['item_id']);
return $this->items_repository->can_read($item);
return $item->can_read();
}
public function get_items_permissions_check( $request ) {
$collection = $this->collections_repository->fetch($request['collection_id']);
return $this->collections_repository->can_read($collection);
return $collection->can_read();
}
/**
@ -239,11 +239,12 @@ class TAINACAN_REST_Items_Controller extends TAINACAN_REST_Controller {
*/
public function create_item_permissions_check( $request ) {
$collection = $this->collections_repository->fetch($request['collection_id']);
if ($collection instanceof Entities\Collection) {
if ($collection instanceof Entities\Collection) {
return $collection->get_items_capabilities()->edit_posts;
}
return false;
return false;
}
/**
@ -272,7 +273,7 @@ class TAINACAN_REST_Items_Controller extends TAINACAN_REST_Controller {
*/
public function delete_item_permissions_check( $request ) {
$item = $this->items_repository->fetch($request['item_id']);
return $this->items_repository->can_delete($item);
return $item->can_delete();
}
/**
@ -286,13 +287,19 @@ class TAINACAN_REST_Items_Controller extends TAINACAN_REST_Controller {
$body = json_decode($request->get_body(), true);
if(!empty($body)){
$attributes = ['ID' => $item_id];
$attributes = [];
foreach ($body as $att => $value){
$attributes[$att] = $value;
}
$updated_item = $this->items_repository->update($attributes);
$item = $this->items_repository->fetch($item_id);
$updated_item = $this->items_repository->update($item, $attributes);
if(!($updated_item instanceof Entities\Item)){
return new WP_REST_Response($updated_item, 400);
}
return new WP_REST_Response($updated_item->__toArray(), 200);
}
@ -311,7 +318,7 @@ class TAINACAN_REST_Items_Controller extends TAINACAN_REST_Controller {
*/
public function update_item_permissions_check( $request ) {
$item = $this->items_repository->fetch($request['item_id']);
return $this->items_repository->can_edit($item);
return $item->can_edit();
}
}

View File

@ -39,7 +39,30 @@ class TAINACAN_REST_Metadata_Controller extends TAINACAN_REST_Controller {
* Both of GETs return the metadata of matched objects
*/
public function register_routes() {
register_rest_route($this->namespace, '/collection/(?P<collection_id>[\d]+)/' . $this->rest_base ,
register_rest_route($this->namespace, '/collection/(?P<collection_id>[\d]+)/' . $this->rest_base . '/(?P<metadata_id>[\d]+)',
array(
array(
'methods' => WP_REST_Server::DELETABLE,
'callback' => array($this, 'delete_item'),
'permission_callback' => array($this, 'delete_item_permissions_check')
),
array(
'methods' => WP_REST_Server::EDITABLE,
'callback' => array($this, 'update_item'),
'permission_callback' => array($this, 'update_item_permissions_check')
)
)
);
register_rest_route($this->namespace, '/item/(?P<item_id>[\d]+)/' . $this->rest_base . '/(?P<metadata_id>[\d]+)',
array(
array(
'methods' => WP_REST_Server::EDITABLE,
'callback' => array($this, 'update_item'),
'permission_callback' => array($this, 'update_item_permissions_check')
)
)
);
register_rest_route($this->namespace, '/collection/(?P<collection_id>[\d]+)/' . $this->rest_base,
array(
array(
'methods' => WP_REST_Server::READABLE,
@ -52,16 +75,6 @@ class TAINACAN_REST_Metadata_Controller extends TAINACAN_REST_Controller {
'callback' => array($this, 'create_item'),
'permission_callback' => array($this, 'create_item_permissions_check')
),
array(
'methods' => WP_REST_Server::DELETABLE,
'callback' => array($this, 'delete_item'),
'permission_callback' => array($this, 'delete_item_permissions_check')
),
array(
'methods' => WP_REST_Server::EDITABLE,
'callback' => array($this, 'update_item'),
'permission_callback' => array($this, 'update_item_permissions_check')
)
)
);
register_rest_route($this->namespace, '/item/(?P<item_id>[\d]+)/'. $this->rest_base,
@ -77,11 +90,6 @@ class TAINACAN_REST_Metadata_Controller extends TAINACAN_REST_Controller {
'callback' => array($this, 'create_item'),
'permission_callback' => array($this, 'create_item_permissions_check')
),
array(
'methods' => WP_REST_Server::EDITABLE,
'callback' => array($this, 'update_item'),
'permission_callback' => array($this, 'update_item_permissions_check')
)
)
);
}
@ -306,7 +314,7 @@ class TAINACAN_REST_Metadata_Controller extends TAINACAN_REST_Controller {
$body = json_decode( $request->get_body(), true );
$item_id = $request['item_id'];
$metadata_id = $body['metadata_id'];
$metadata_id = $request['metadata_id'];
$value = $body['values'];
$item = $this->item_repository->fetch( $item_id );
@ -335,13 +343,21 @@ class TAINACAN_REST_Metadata_Controller extends TAINACAN_REST_Controller {
$body = json_decode($request->get_body(), true);
if(!empty($body)){
$attributes = ['ID' => $body['metadata_id']];
$attributes = [];
$metadata_id = $request['metadata_id'];
foreach ($body['values'] as $att => $value){
$attributes[$att] = $value;
}
$updated_metadata = $this->metadata_repository->update($attributes);
$metadata = $this->metadata_repository->fetch($metadata_id);
$updated_metadata = $this->metadata_repository->update($metadata, $attributes);
if(!($updated_metadata instanceof Entities\Metadata)){
return new WP_REST_Response($updated_metadata, 400);
}
$items = $this->item_repository->fetch([], $collection_id, 'WP_Query');

View File

@ -216,20 +216,21 @@ class Collections extends Repository {
return $new_collection;
}
public function update($object){
$map = $this->get_map();
$entity = [];
foreach ($object as $key => $value) {
if($key != 'ID') {
$entity[$map[$key]['map']] = $value ;
} elseif ($key == 'ID'){
$entity[$key] = (int) $value;
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();
}
}
return new Entities\Collection(wp_update_post($entity));
if($object->validate()){
return $this->insert($object);
}
return $object->get_errors();
}
/**

View File

@ -168,7 +168,7 @@ class Filters extends Repository {
return new Entities\Filter(wp_trash_post($args[0]));
}
public function update($object){
public function update($object, $new_values = null){
$map = $this->get_map();
$entity = [];

View File

@ -31,7 +31,7 @@ class Item_Metadata extends Repository {
return new Entities\Item_Metadata_Entity($item_metadata->get_item(), $item_metadata->get_metadata());
}
public function update($item_metadata){
public function update($item_metadata, $new_values = null){
$unique = !$item_metadata->is_multiple();
if ($unique) {

View File

@ -239,20 +239,21 @@ class Items extends Repository {
return $this->fetch_output($wp_query, $output);
}
public function update($object){
$map = $this->get_map();
$entity = [];
foreach ($object as $key => $value) {
if($key != 'ID') {
$entity[$map[$key]['map']] = $value ;
} elseif ($key == 'ID'){
$entity[$key] = (int) $value;
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();
}
}
return new Entities\Item(wp_update_post($entity));
if($object->validate()){
return $this->insert($object);
}
return $object->get_errors();
}
/**

View File

@ -182,7 +182,7 @@ class Logs extends Repository {
}
public function update($object){
public function update($object, $new_values = null){
}

View File

@ -288,20 +288,28 @@ class Metadatas extends Repository {
return $this->fetch( $args, $output );
}
public function update($object){
$map = $this->get_map();
$entity = [];
foreach ($object as $key => $value) {
if($key != 'ID') {
$entity[$map[$key]['map']] = $value ;
} elseif ($key == 'ID'){
$entity[$key] = (int) $value;
/**
* @param $object
* @param $new_values
*
* @return mixed|string|Entities\Entity
* @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();
}
}
return new Entities\Metadata(wp_update_post($entity));
if($object->validate()){
return $this->insert($object);
}
return $object->get_errors();
}
public function delete($object){

View File

@ -381,7 +381,7 @@ abstract class Repository {
* @param $object
* @return mixed
*/
public abstract function update($object);
public abstract function update($object, $new_values = null);
/**
* @return mixed

View File

@ -184,7 +184,7 @@ class Taxonomies extends Repository {
}
}
public function update($object){
public function update($object, $new_values = null){
$map = $this->get_map();
$entity = [];

View File

@ -155,7 +155,7 @@ class Terms extends Repository {
}
}
public function update($object){
public function update($object, $new_values = null){
$map = $this->get_map();
$entity = [];

View File

@ -158,14 +158,13 @@ class TAINACAN_REST_Metadata_Controller extends TAINACAN_UnitApiTestCase {
$meta_values = json_encode(
array(
'metadata_id' => $metadata->get_id(),
'values' => '19/01/2018'
)
);
$request = new \WP_REST_Request(
'PATCH',
$this->namespace . '/item/' . $item->get_id() . '/metadata'
$this->namespace . '/item/' . $item->get_id() . '/metadata/' . $metadata->get_id()
);
$request->set_body($meta_values);
@ -185,7 +184,6 @@ class TAINACAN_REST_Metadata_Controller extends TAINACAN_UnitApiTestCase {
#### UPDATE METADATA IN COLLECTION ####
$values = json_encode([
'metadata_id' => $metadata->get_id(),
'values' => [
'name' => 'Dia/Mês/Ano',
'description' => 'Continua descrevendo o dado do campo.'
@ -194,7 +192,7 @@ class TAINACAN_REST_Metadata_Controller extends TAINACAN_UnitApiTestCase {
$request = new \WP_REST_Request(
'PATCH',
$this->namespace . '/collection/' . $collection->get_id() . '/metadata'
$this->namespace . '/collection/' . $collection->get_id() . '/metadata/' . $metadata->get_id()
);
$request->set_body($values);