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

View File

@ -7,8 +7,13 @@
@selection-change="handleSelectionChange"> @selection-change="handleSelectionChange">
<el-table-column type="selection" width="55"> <el-table-column type="selection" width="55">
</el-table-column> </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> <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>
<el-table-column property="description" label="Descrição" show-overflow-tooltip> <el-table-column property="description" label="Descrição" show-overflow-tooltip>
</el-table-column> </el-table-column>
@ -67,7 +72,7 @@ export default {
<style scoped> <style scoped>
.table-thumb { .table-thumb {
max-height: 38px !important; max-height: 55px !important;
vertical-align: middle !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){ public function get_item_permissions_check($request){
$collection = $this->collections_repository->fetch($request['collection_id']); $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 ) { public function delete_item_permissions_check( $request ) {
$collection = $this->collections_repository->fetch($request['collection_id']); $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); $body = json_decode($request->get_body(), true);
if(!empty($body)){ if(!empty($body)){
$attributes = ['ID' => $collection_id]; $attributes = [];
foreach ($body as $att => $value){ foreach ($body as $att => $value){
$attributes[$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); 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 ) { public function update_item_permissions_check( $request ) {
$collection = $this->collections_repository->fetch($request['collection_id']); $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 ) { public function get_item_permissions_check( $request ) {
$item = $this->items_repository->fetch($request['item_id']); $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 ) { public function get_items_permissions_check( $request ) {
$collection = $this->collections_repository->fetch($request['collection_id']); $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 ) { public function create_item_permissions_check( $request ) {
$collection = $this->collections_repository->fetch($request['collection_id']); $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 $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 ) { public function delete_item_permissions_check( $request ) {
$item = $this->items_repository->fetch($request['item_id']); $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); $body = json_decode($request->get_body(), true);
if(!empty($body)){ if(!empty($body)){
$attributes = ['ID' => $item_id]; $attributes = [];
foreach ($body as $att => $value){ foreach ($body as $att => $value){
$attributes[$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); 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 ) { public function update_item_permissions_check( $request ) {
$item = $this->items_repository->fetch($request['item_id']); $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 * Both of GETs return the metadata of matched objects
*/ */
public function register_routes() { 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(
array( array(
'methods' => WP_REST_Server::READABLE, 'methods' => WP_REST_Server::READABLE,
@ -52,16 +75,6 @@ class TAINACAN_REST_Metadata_Controller extends TAINACAN_REST_Controller {
'callback' => array($this, 'create_item'), 'callback' => array($this, 'create_item'),
'permission_callback' => array($this, 'create_item_permissions_check') '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, 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'), 'callback' => array($this, 'create_item'),
'permission_callback' => array($this, 'create_item_permissions_check') '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 ); $body = json_decode( $request->get_body(), true );
$item_id = $request['item_id']; $item_id = $request['item_id'];
$metadata_id = $body['metadata_id']; $metadata_id = $request['metadata_id'];
$value = $body['values']; $value = $body['values'];
$item = $this->item_repository->fetch( $item_id ); $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); $body = json_decode($request->get_body(), true);
if(!empty($body)){ if(!empty($body)){
$attributes = ['ID' => $body['metadata_id']]; $attributes = [];
$metadata_id = $request['metadata_id'];
foreach ($body['values'] as $att => $value){ foreach ($body['values'] as $att => $value){
$attributes[$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'); $items = $this->item_repository->fetch([], $collection_id, 'WP_Query');

View File

@ -216,20 +216,21 @@ class Collections extends Repository {
return $new_collection; return $new_collection;
} }
public function update($object){ public function update($object, $new_values = null){
$map = $this->get_map(); foreach ($new_values as $key => $value) {
try {
$entity = []; $set_ = 'set_' . $key;
$object->$set_( $value );
foreach ($object as $key => $value) { } catch (\Error $error){
if($key != 'ID') { return $error->getMessage();
$entity[$map[$key]['map']] = $value ;
} elseif ($key == 'ID'){
$entity[$key] = (int) $value;
} }
} }
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])); return new Entities\Filter(wp_trash_post($args[0]));
} }
public function update($object){ public function update($object, $new_values = null){
$map = $this->get_map(); $map = $this->get_map();
$entity = []; $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()); 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(); $unique = !$item_metadata->is_multiple();
if ($unique) { if ($unique) {

View File

@ -239,20 +239,21 @@ class Items extends Repository {
return $this->fetch_output($wp_query, $output); return $this->fetch_output($wp_query, $output);
} }
public function update($object){ public function update($object, $new_values = null){
$map = $this->get_map(); foreach ($new_values as $key => $value) {
try {
$entity = []; $set_ = 'set_' . $key;
$object->$set_( $value );
foreach ($object as $key => $value) { } catch (\Error $error){
if($key != 'ID') { return $error->getMessage();
$entity[$map[$key]['map']] = $value ;
} elseif ($key == 'ID'){
$entity[$key] = (int) $value;
} }
} }
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 ); return $this->fetch( $args, $output );
} }
public function update($object){ /**
$map = $this->get_map(); * @param $object
* @param $new_values
$entity = []; *
* @return mixed|string|Entities\Entity
foreach ($object as $key => $value) { * @throws \Exception
if($key != 'ID') { */
$entity[$map[$key]['map']] = $value ; public function update($object, $new_values = null){
} elseif ($key == 'ID'){ foreach ($new_values as $key => $value) {
$entity[$key] = (int) $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){ public function delete($object){

View File

@ -381,7 +381,7 @@ abstract class Repository {
* @param $object * @param $object
* @return mixed * @return mixed
*/ */
public abstract function update($object); public abstract function update($object, $new_values = null);
/** /**
* @return mixed * @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(); $map = $this->get_map();
$entity = []; $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(); $map = $this->get_map();
$entity = []; $entity = [];

View File

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