Refactoring Sequence Edit API #200

Sequence edit now do not rely on Bulk edit actions.
Front end still needs to change
This commit is contained in:
leogermani 2019-12-10 22:15:50 -03:00
parent c2f02e048d
commit 04ed966369
3 changed files with 391 additions and 1 deletions

View File

@ -0,0 +1,183 @@
<?php
namespace Tainacan\API\EndPoints;
use \Tainacan\API\REST_Controller;
use Tainacan\Entities;
use Tainacan\Repositories;
use Tainacan\Entities\Entity;
class REST_Sequence_Edit_Controller extends REST_Controller {
public function __construct() {
$this->rest_base = 'sequence-edit';
parent::__construct();
add_action('init', array(&$this, 'init_objects'), 11);
}
public function init_objects() {
$this->items_repository = Repositories\Items::get_instance();
$this->collections_repository = Repositories\Collections::get_instance();
}
/**
*
*
* @throws \Exception
*/
public function register_routes() {
register_rest_route($this->namespace, '/collection/(?P<collection_id>[\d]+)/' . $this->rest_base,
array(
array(
'methods' => \WP_REST_Server::CREATABLE,
'callback' => array($this, 'create_item'),
'permission_callback' => array($this, 'sequence_edit_permissions_check'),
'args' => $this->get_create_params()
),
)
);
//register_rest_route($this->namespace, '/collection/(?P<collection_id>[\d]+)/' . $this->rest_base . '/(?P<group_id>[0-9a-f]+)',
// array(
// array(
// 'methods' => \WP_REST_Server::READABLE,
// 'callback' => array($this, 'get_item'),
// 'permission_callback' => array($this, 'sequence_edit_permissions_check'),
// ),
// )
//);
register_rest_route($this->namespace, '/collection/(?P<collection_id>[\d]+)/' . $this->rest_base . '/(?P<group_id>[0-9a-f]+)/(?P<sequence_index>[\d]+)',
array(
array(
'methods' => \WP_REST_Server::READABLE,
'callback' => array($this, 'get_item_in_sequence'),
'permission_callback' => array($this, 'sequence_edit_permissions_check'),
),
)
);
}
public function prepare_item_for_response($item, $request) {
}
public function sequence_edit_permissions_check($request) {
$collection = $this->collections_repository->fetch($request['collection_id']);
if ($collection instanceof Entities\Collection) {
return current_user_can($collection->get_items_capabilities()->edit_others_posts);
}
return false;
}
public function create_item($request) {
$body = json_decode($request->get_body(), true);
$args = [];
if (isset($body['items_ids']) && is_array($body['items_ids']) && !empty($body['items_ids'])) {
$args['items_ids'] = $body['items_ids'];
$count = sizeof($args['items_ids']);
} elseif ( isset($body['use_query']) && $body['use_query'] ) {
unset($body['use_query']['paged']);
unset($body['use_query']['offset']);
unset($body['use_query']['perpage']);
$query_args = $this->prepare_filters($body['use_query']);
$collection_id = $request['collection_id'];
$args = [
'query' => $query_args,
'collection_id' => $collection_id
];
// calculate size
$query_args['posts_per_page'] = 1;
$items_q = $this->items_repository->fetch( $query_args, $collection_id );
$count = $items_q->found_posts;
} else {
return new \WP_REST_Response([
'error_message' => __('You mus specify items_ids OR use_query', 'tainacan'),
], 400);
}
$new_group_id = uniqid();
update_option('tnc_transient_' . $new_group_id, $args);
$response = [
'id' => $new_group_id,
'items_count' => $count
];
$rest_response = new \WP_REST_Response($response, 200);
return $rest_response;
}
public function get_item_in_sequence($request) {
$group_id = $request['group_id'];
$index = (int) $request['sequence_index'];
$group = get_option('tnc_transient_' . $group_id);
//ar_dump($group);
if ( is_array($group) ) {
if ( isset($group['items_ids']) && is_array($group['items_ids']) ) {
$index = $index - 1;
if ( isset( $group['items_ids'][$index] ) ) {
return new \WP_REST_Response( $group['items_ids'][$index], 200 );
}
} elseif (
is_array($group) &&
isset($group['collection_id']) &&
isset($group['query']) &&
is_array($group['query'])
) {
$group['query']['paged'] = $index;
$group['query']['posts_per_page'] = 1;
$items = $this->items_repository->fetch_ids( $group['query'], $group['collection_id'] );
if ( is_array($items) && !empty($items) ) {
return new \WP_REST_Response( $items[0], 200 );
}
}
}
return new \WP_REST_Response([
'error_message' => __('Item not found.', 'tainacan'),
], 404);
}
/**
* @param null $object_name
*
* @return array|void
*/
public function get_create_params($object_name = null) {
$query_params['items_ids'] = [
'type' => 'array',
'items' => ['type' => 'integer'],
'description' => __( 'Array of items IDs', 'tainacan' ),
];
$query_params['use_query'] = [
'description' => __( 'The query used to define the items in the group', 'tainacan' ),
];
return $query_params;
}
}
?>

View File

@ -21,6 +21,7 @@ $rest_exposers_controller = new \Tainacan\API\EndPoints\REST_Exposers_Control
new \Tainacan\API\EndPoints\REST_Metadatum_Mappers_Controller();
$rest_facets_controller = new \Tainacan\API\EndPoints\REST_Facets_Controller();
$rest_oaipmh_expose_controller = new \Tainacan\API\EndPoints\REST_Oaipmh_Expose_Controller();
$rest_sequence_edit_controller = new \Tainacan\API\EndPoints\REST_Sequence_Edit_Controller();
// Add here other endpoints imports
?>
?>

View File

@ -0,0 +1,206 @@
<?php
namespace Tainacan\Tests;
use Tainacan\Entities;
/**
* Sample test case.
*/
class SequenceEdit extends TAINACAN_UnitApiTestCase {
public $items_ids = [];
function setUp() {
parent::setUp();
$collection = $this->tainacan_entity_factory->create_entity(
'collection',
array(
'name' => 'test_col',
'status' => 'publish'
),
true
);
$this->collection = $collection;
$metadatum = $this->tainacan_entity_factory->create_entity(
'metadatum',
array(
'name' => 'metadado',
'status' => 'publish',
'collection' => $collection,
'metadata_type' => 'Tainacan\Metadata_Types\Text',
),
true
);
$this->metadatum = $metadatum;
for ($i = 1; $i<=40; $i++) {
$title = 'testeItem ' . str_pad($i, 2, "0", STR_PAD_LEFT);
$item = $this->tainacan_entity_factory->create_entity(
'item',
array(
'title' => $title,
'collection' => $collection,
'status' => 'publish'
),
true
);
$this->items_ids[] = $item->get_id();
$this->tainacan_item_metadata_factory->create_item_metadata($item, $metadatum, $i % 2 == 0 ? 'even' : 'odd');
$this->tainacan_item_metadata_factory->create_item_metadata($item, $collection->get_core_title_metadatum(), $title);
}
$this->api_baseroute = $this->namespace . '/collection/' . $collection->get_id() . '/sequence-edit';
}
function test_setup() {
$this->assertEquals(40, sizeof($this->items_ids));
}
function test_init_by_query() {
$query = [
'metaquery' => [
[
'key' => $this->metadatum->get_id(),
'value' => 'even'
]
],
'orderby' => 'title'
];
$request = new \WP_REST_Request(
'POST', $this->api_baseroute
);
$request->set_body( json_encode(['use_query' => $query]) );
$response = $this->server->dispatch($request);
$this->assertEquals(200, $response->get_status());
$data = $response->get_data();
$this->assertEquals(20, $data['items_count']);
$group_id = $data['id'];
$items = \Tainacan\Repositories\Items::get_instance()->fetch( [
'meta_query' => [
[
'key' => $this->metadatum->get_id(),
'value' => 'even'
]
],
'nopaging' => 1,
'orderby' => 'post_title'
], $this->collection->get_id(), 'OBJECT');
$request = new \WP_REST_Request(
'GET', $this->api_baseroute . '/' . $group_id . '/1'
);
$response = $this->server->dispatch($request);
$data = $response->get_data();
$this->assertEquals(200, $response->get_status());
$this->assertEquals($items[0]->get_id(), $data);
$request = new \WP_REST_Request(
'GET', $this->api_baseroute . '/' . $group_id . '/7'
);
$response = $this->server->dispatch($request);
$data = $response->get_data();
$this->assertEquals(200, $response->get_status());
$this->assertEquals($items[6]->get_id(), $data);
$request = new \WP_REST_Request(
'GET', $this->api_baseroute . '/' . $group_id . '/20'
);
$response = $this->server->dispatch($request);
$data = $response->get_data();
$this->assertEquals(200, $response->get_status());
$this->assertEquals($items[19]->get_id(), $data);
$request = new \WP_REST_Request(
'GET', $this->api_baseroute . '/' . $group_id . '/30'
);
$response = $this->server->dispatch($request);
$data = $response->get_data();
$this->assertEquals(404, $response->get_status());
}
function test_init_by_ids() {
$ids = array_slice($this->items_ids, 2, 7);
$request = new \WP_REST_Request(
'POST', $this->api_baseroute
);
$request->set_body( json_encode(['items_ids' => $ids]) );
$response = $this->server->dispatch($request);
$this->assertEquals(200, $response->get_status());
$data = $response->get_data();
$this->assertEquals(7, $data['items_count']);
$group_id = $data['id'];
$request = new \WP_REST_Request(
'GET', $this->api_baseroute . '/' . $group_id . '/1'
);
$response = $this->server->dispatch($request);
$data = $response->get_data();
$this->assertEquals(200, $response->get_status());
$this->assertEquals($ids[0], $data);
$request = new \WP_REST_Request(
'GET', $this->api_baseroute . '/' . $group_id . '/3'
);
$response = $this->server->dispatch($request);
$data = $response->get_data();
$this->assertEquals(200, $response->get_status());
$this->assertEquals($ids[2], $data);
$request = new \WP_REST_Request(
'GET', $this->api_baseroute . '/' . $group_id . '/6'
);
$response = $this->server->dispatch($request);
$data = $response->get_data();
$this->assertEquals(200, $response->get_status());
$this->assertEquals($ids[5], $data);
$request = new \WP_REST_Request(
'GET', $this->api_baseroute . '/' . $group_id . '/7'
);
$response = $this->server->dispatch($request);
$data = $response->get_data();
$this->assertEquals(200, $response->get_status());
$this->assertEquals($ids[6], $data);
$request = new \WP_REST_Request(
'GET', $this->api_baseroute . '/' . $group_id . '/8'
);
$response = $this->server->dispatch($request);
$data = $response->get_data();
$this->assertEquals(404, $response->get_status());
}
}