New routes - Item Metadata

This commit is contained in:
weryques 2018-01-31 13:50:10 -02:00
parent f721b2d841
commit 1e2c2a4321
5 changed files with 317 additions and 151 deletions

View File

@ -0,0 +1,285 @@
<?php
use Tainacan\Entities;
use Tainacan\Repositories;
class TAINACAN_REST_Item_Metadata_Controller extends TAINACAN_REST_Controller {
private $field;
private $item_metadata_repository;
private $item_repository;
private $collection_repository;
public function __construct() {
$this->namespace = 'tainacan/v2';
$this->rest_base = 'metadata';
add_action('rest_api_init', array($this, 'register_routes'));
add_action('init', array(&$this, 'init_objects'), 11);
}
/**
* Initialize objects after post_type register
*/
public function init_objects() {
$this->field = new Entities\Field();
$this->field_repository = new Repositories\Fields();
$this->item_metadata_repository = new Repositories\Item_Metadata();
$this->item_repository = new Repositories\Items();
$this->collection_repository = new Repositories\Collections();
}
/**
* If POST on field/collection/<collection_id>, then
* a field will be created in matched collection and all your item will receive this field
*
* If POST on field/item/<item_id>, then a value will be added in a field and field passed
* id body of requisition
*
* Both of GETs return the field of matched objects
*/
public function register_routes() {
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, '/item/(?P<item_id>[\d]+)/'. $this->rest_base,
array(
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array($this, 'get_items'),
'permission_callback' => array($this, 'get_items_permissions_check'),
'args' => $this->get_collection_params(),
)
)
);
}
/**
* @param WP_REST_Request $request
*
* @return object|void|WP_Error
*/
public function prepare_item_for_database( $request ) {
$meta = json_decode($request[0]->get_body(), true);
foreach ($meta as $key => $value){
$set_ = 'set_' . $key;
$this->field->$set_($value);
}
$collection = new Entities\Collection($request[1]);
$this->field->set_collection($collection);
}
/**
* @param WP_REST_Request $request
*
* @return WP_Error|WP_REST_Response
*/
// public function create_item( $request ) {
// if (!empty($request['item_id']) && !empty($request->get_body())){
// $body = json_decode($request->get_body(), true);
//
// $item_id = $request['item_id'];
// $field_id = $body['metadata_id'];
// $value = $body['values'];
//
// $item = $this->item_repository->fetch($item_id);
// $field = $this->metadata_repository->fetch($field_id);
//
// $item_metadata = new Entities\Item_Metadata_Entity($item, $field);
// $item_metadata->set_value($value);
//
// if($item_metadata->validate()) {
// $field_updated = $this->item_metadata_repository->insert( $item_metadata );
//
// return new WP_REST_Response( $field_updated->__toArray(), 201 );
// } else {
// return new WP_REST_Response([
// 'error_message' => __('One or more values are invalid.', 'tainacan'),
// 'errors' => $item_metadata->get_errors(),
// 'item_metadata' => $item_metadata->__toArray(),
// ], 400);
// }
// } else {
// return new WP_REST_Response([
// 'error_message' => __('Body can not be empty.', 'tainacan'),
// 'item' => $request->get_body()
// ], 400);
// }
// }
/**
* @param $request
*
* @return bool|WP_Error
* @throws Exception
*/
// public function create_item_permissions_check( $request ) {
// if(isset($request['item_id'])){
// return $this->item_repository->can_edit(new Entities\Item());
// }
//
// return $this->collection_repository->can_edit(new Entities\Collection());
// }
/**
* @param mixed $item
* @param WP_REST_Request $request
*
* @return array|WP_Error|WP_REST_Response
*/
public function prepare_item_for_response( $item, $request ) {
$field_as = [];
foreach ( $item as $field ) {
$field_as[] = $field->__toArray();
}
return $field_as;
}
/**
* @param WP_REST_Request $request
*
* @return WP_Error|WP_REST_Response
*/
public function get_items( $request ) {
$item_id = $request['item_id'];
$item = new Entities\Item($item_id);
$item_metadata = $this->item_metadata_repository->fetch($item, 'OBJECT');
$prepared_item = $this->prepare_item_for_response($item_metadata, $request);
return new WP_REST_Response($prepared_item, 200);
}
/**
* @param WP_REST_Request $request
*
* @return bool|WP_Error
* @throws Exception
*/
public function get_items_permissions_check( $request ) {
if(isset($request['item_id'])){
$item = $this->item_repository->fetch($request['item_id']);
if($item instanceof Entities\Item) {
return $item->can_read();
}
}
return false;
}
/**
* @return array
*/
public function get_collection_params() {
return parent::get_collection_params(); // TODO: Change the autogenerated stub
}
/**
* @param WP_REST_Request $request
*
* @return WP_Error|WP_REST_Response
*/
public function delete_item( $request ) {
if(!empty($request->get_body())){
$body = json_decode($request->get_body());
$collection_id = $request['collection_id'];
$field_id = $body['metadata_id'];
return new WP_REST_Response(['error' => 'Not Implemented.'], 400);
}
}
/**
* @param WP_REST_Request $request
*
* @return bool|WP_Error
* @throws Exception
*/
public function delete_item_permissions_check( $request ) {
if(isset($request['item_id'])){
$item = $this->item_repository->fetch($request['item_id']);
if($item instanceof Entities\Item) {
return $item->can_delete();
}
}
return false;
}
/**
* @param WP_REST_Request $request
*
* @return WP_Error|WP_REST_Response
*/
public function update_item( $request ) {
$body = json_decode( $request->get_body(), true );
if($body) {
$item_id = $request['item_id'];
$field_id = $request['metadata_id'];
$value = $body['values'];
$item = $this->item_repository->fetch( $item_id );
$field = $this->field_repository->fetch( $field_id );
$item_metadata = new Entities\Item_Metadata_Entity( $item, $field );
$item_metadata->set_value( $value );
if ( $item_metadata->validate() ) {
$field_updated = $this->item_metadata_repository->update( $item_metadata );
return new WP_REST_Response( $field_updated->__toArray(), 200 );
} else {
return new WP_REST_Response( [
'error_message' => __( 'One or more values are invalid.', 'tainacan' ),
'errors' => $item_metadata->get_errors(),
'item_metadata' => $item_metadata->__toArray(),
], 400 );
}
}
return new WP_REST_Response([
'error_message' => 'The body could not be empty',
'body' => $body
], 400);
}
/**
* @param WP_REST_Request $request
*
* @return bool|WP_Error
* @throws Exception
*/
public function update_item_permissions_check( $request ) {
if (isset($request['item_id'])) {
$item = $this->item_repository->fetch($request['item_id']);
if ($item instanceof Entities\Item) {
return $item->can_edit();
}
}
return false;
}
}
?>

View File

@ -11,7 +11,7 @@ class TAINACAN_REST_Metadata_Controller extends TAINACAN_REST_Controller {
public function __construct() {
$this->namespace = 'tainacan/v2';
$this->rest_base = 'field';
$this->rest_base = 'fields';
add_action('rest_api_init', array($this, 'register_routes'));
add_action('init', array(&$this, 'init_objects'), 11);
@ -52,15 +52,6 @@ class TAINACAN_REST_Metadata_Controller extends TAINACAN_REST_Controller {
)
)
);
register_rest_route($this->namespace, '/item/(?P<item_id>[\d]+)/' . $this->rest_base . '/(?P<field_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(
@ -76,21 +67,6 @@ class TAINACAN_REST_Metadata_Controller extends TAINACAN_REST_Controller {
),
)
);
register_rest_route($this->namespace, '/item/(?P<item_id>[\d]+)/'. $this->rest_base,
array(
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array($this, 'get_items'),
'permission_callback' => array($this, 'get_items_permissions_check'),
'args' => $this->get_collection_params(),
),
array(
'methods' => WP_REST_Server::CREATABLE,
'callback' => array($this, 'create_item'),
'permission_callback' => array($this, 'create_item_permissions_check')
),
)
);
}
/**
@ -117,7 +93,7 @@ class TAINACAN_REST_Metadata_Controller extends TAINACAN_REST_Controller {
* @return WP_Error|WP_REST_Response
*/
public function create_item( $request ) {
if(!empty($request['collection_id'])){
if(!empty($request->get_body())){
$collection_id = $request['collection_id'];
try {
@ -154,36 +130,13 @@ class TAINACAN_REST_Metadata_Controller extends TAINACAN_REST_Controller {
'field' => $this->field->__toArray(),
], 400);
}
} elseif (!empty($request['item_id']) && !empty($request->get_body())){
$body = json_decode($request->get_body(), true);
$item_id = $request['item_id'];
$field_id = $body['field_id'];
$value = $body['values'];
$item = $this->item_repository->fetch($item_id);
$field = $this->metadata_repository->fetch($field_id);
$item_metadata = new Entities\Item_Metadata_Entity($item, $field);
$item_metadata->set_value($value);
if($item_metadata->validate()) {
$field_updated = $this->item_metadata_repository->insert( $item_metadata );
return new WP_REST_Response( $field_updated->__toArray(), 201 );
} else {
return new WP_REST_Response([
'error_message' => __('One or more values are invalid.', 'tainacan'),
'errors' => $item_metadata->get_errors(),
'item_metadata' => $item_metadata->__toArray(),
], 400);
}
} else {
return new WP_REST_Response([
'error_message' => __('Body can not be empty.', 'tainacan'),
'item' => $request->get_body()
], 400);
}
}
/**
@ -193,10 +146,6 @@ class TAINACAN_REST_Metadata_Controller extends TAINACAN_REST_Controller {
* @throws Exception
*/
public function create_item_permissions_check( $request ) {
if(isset($request['item_id'])){
return $this->item_repository->can_edit(new Entities\Item());
}
return $this->collection_repository->can_edit(new Entities\Collection());
}
@ -209,16 +158,9 @@ class TAINACAN_REST_Metadata_Controller extends TAINACAN_REST_Controller {
public function prepare_item_for_response( $item, $request ) {
$field_as = [];
if($request['item_id']) {
foreach ( $item as $field ) {
$field_as[] = $field->__toArray();
}
} else if($request['collection_id']){
foreach ( $item as $field ) {
$field_as[] = $field->__toArray();
}
}
return $field_as;
}
@ -229,7 +171,6 @@ class TAINACAN_REST_Metadata_Controller extends TAINACAN_REST_Controller {
* @return WP_Error|WP_REST_Response
*/
public function get_items( $request ) {
if(!empty($request['collection_id'])){
$collection_id = $request['collection_id'];
$collection = new Entities\Collection($collection_id);
@ -241,17 +182,6 @@ class TAINACAN_REST_Metadata_Controller extends TAINACAN_REST_Controller {
return new WP_REST_Response($prepared_item, 200);
}
$item_id = $request['item_id'];
$item = new Entities\Item($item_id);
$item_metadata = $this->item_metadata_repository->fetch($item, 'OBJECT');
$prepared_item = $this->prepare_item_for_response($item_metadata, $request);
return new WP_REST_Response($prepared_item, 200);
}
/**
* @param WP_REST_Request $request
*
@ -259,14 +189,7 @@ class TAINACAN_REST_Metadata_Controller extends TAINACAN_REST_Controller {
* @throws Exception
*/
public function get_items_permissions_check( $request ) {
if(isset($request['item_id'])){
$item = $this->item_repository->fetch($request['item_id']);
if($item instanceof Entities\Item) {
return $item->can_read();
}
} elseif (isset($request['collection_id'])) {
if (isset($request['collection_id'])) {
$collection = $this->collection_repository->fetch($request['collection_id']);
if ($collection instanceof Entities\Collection) {
@ -307,14 +230,7 @@ class TAINACAN_REST_Metadata_Controller extends TAINACAN_REST_Controller {
* @throws Exception
*/
public function delete_item_permissions_check( $request ) {
if(isset($request['item_id'])){
$item = $this->item_repository->fetch($request['item_id']);
if($item instanceof Entities\Item) {
return $item->can_delete();
}
} elseif (isset($request['collection_id'])) {
if (isset($request['collection_id'])) {
$collection = $this->collection_repository->fetch($request['collection_id']);
if ($collection instanceof Entities\Collection) {
@ -331,35 +247,6 @@ class TAINACAN_REST_Metadata_Controller extends TAINACAN_REST_Controller {
* @return WP_Error|WP_REST_Response
*/
public function update_item( $request ) {
if($request['item_id']) {
$body = json_decode( $request->get_body(), true );
$item_id = $request['item_id'];
$field_id = $request['field_id'];
$value = $body['values'];
$item = $this->item_repository->fetch( $item_id );
$field = $this->metadata_repository->fetch( $field_id );
$item_metadata = new Entities\Item_Metadata_Entity( $item, $field );
$item_metadata->set_value( $value );
if ( $item_metadata->validate() ) {
$field_updated = $this->item_metadata_repository->update( $item_metadata );
return new WP_REST_Response( $field_updated->__toArray(), 200 );
} else {
return new WP_REST_Response( [
'error_message' => __( 'One or more values are invalid.', 'tainacan' ),
'errors' => $item_metadata->get_errors(),
'item_metadata' => $item_metadata->__toArray(),
], 400 );
}
}
// We need to rethink this endpoint. Its confusing...
// and there is no need to iterato through all items...
$collection_id = $request['collection_id'];
$body = json_decode($request->get_body(), true);
@ -412,14 +299,7 @@ class TAINACAN_REST_Metadata_Controller extends TAINACAN_REST_Controller {
* @throws Exception
*/
public function update_item_permissions_check( $request ) {
if (isset($request['item_id'])) {
$item = $this->item_repository->fetch($request['item_id']);
if ($item instanceof Entities\Item) {
return $item->can_edit();
}
} elseif(isset($request['collection_id'])) {
if(isset($request['collection_id'])) {
$collection = $this->collection_repository->fetch($request['collection_id']);
if ($collection instanceof Entities\Collection) {

View File

@ -7,6 +7,7 @@ $rest_metadata_controller = new TAINACAN_REST_Metadata_Controller();
$rest_taxonomies_controller = new TAINACAN_REST_Taxonomies_Controller();
$rest_terms_controller = new TAINACAN_REST_Terms_Controller();
$rest_filters_controller = new TAINACAN_REST_Filters_Controller();
$rest_item_metadata_controller = new TAINACAN_REST_Item_Metadata_Controller();
// Add here other endpoints imports
?>

View File

@ -48,8 +48,8 @@ function tainacan_autoload($class_name){
$lower = $sliced[0];
$sliced[0] = strtolower( $lower );
$dir = CLASSES_DIR . implode( DIRECTORY_SEPARATOR, $sliced ) . '/';
$dir = str_replace( '_', '-', $dir );
$dir = implode( DIRECTORY_SEPARATOR, $sliced ) . '/';
$dir = CLASSES_DIR . str_replace( '_', '-', $dir );
} else {
$dir = CLASSES_DIR;
}

View File

@ -34,7 +34,7 @@ class TAINACAN_REST_Metadata_Controller extends TAINACAN_UnitApiTestCase {
$request = new \WP_REST_Request(
'POST',
$this->namespace . '/collection/' . $collection->get_id() . '/field'
$this->namespace . '/collection/' . $collection->get_id() . '/fields'
);
$request->set_body($field);
@ -93,7 +93,7 @@ class TAINACAN_REST_Metadata_Controller extends TAINACAN_UnitApiTestCase {
$request = new \WP_REST_Request(
'GET',
$this->namespace . '/collection/' . $collection->get_id() . '/field'
$this->namespace . '/collection/' . $collection->get_id() . '/fields'
);
$response = $this->server->dispatch($request);
@ -108,7 +108,7 @@ class TAINACAN_REST_Metadata_Controller extends TAINACAN_UnitApiTestCase {
$request = new \WP_REST_Request(
'GET',
$this->namespace . '/item/' . $item->get_id() . '/field'
$this->namespace . '/item/' . $item->get_id() . '/metadata'
);
$response = $this->server->dispatch($request);
@ -164,7 +164,7 @@ class TAINACAN_REST_Metadata_Controller extends TAINACAN_UnitApiTestCase {
$request = new \WP_REST_Request(
'PATCH',
$this->namespace . '/item/' . $item->get_id() . '/field/' . $field->get_id()
$this->namespace . '/item/' . $item->get_id() . '/metadata/' . $field->get_id()
);
$request->set_body($meta_values);
@ -192,7 +192,7 @@ class TAINACAN_REST_Metadata_Controller extends TAINACAN_UnitApiTestCase {
$request = new \WP_REST_Request(
'PATCH',
$this->namespace . '/collection/' . $collection->get_id() . '/field/' . $field->get_id()
$this->namespace . '/collection/' . $collection->get_id() . '/fields/' . $field->get_id()
);
$request->set_body($values);