Api updates

Created taxonomies Controller;
Now the entities have the method __toArray();
Created test for taxonomies Controller;
Added more exceptions treatments;
terms Controller was initiated;
And other improvements.
This commit is contained in:
weryques 2017-12-19 14:24:30 -02:00
parent 6eb70b515d
commit 167867620e
13 changed files with 380 additions and 68 deletions

View File

@ -89,7 +89,7 @@ class TAINACAN_REST_Collections_Controller extends WP_REST_Controller {
$collection_id = $request['collection_id']; $collection_id = $request['collection_id'];
$collection = $this->collections_repository->fetch($collection_id); $collection = $this->collections_repository->fetch($collection_id);
$response = $this->prepare_item_for_response( $collection, $request ); $response = $this->prepare_item_for_response($collection, $request );
return new WP_REST_Response($response, 200); return new WP_REST_Response($response, 200);
} }
@ -111,16 +111,16 @@ class TAINACAN_REST_Collections_Controller extends WP_REST_Controller {
while ( $item->have_posts() ) { while ( $item->have_posts() ) {
$item->the_post(); $item->the_post();
$collection = new Entities\Collection($item->post); $collection = new Entities\Collection($item->post);
array_push($collections_as_json, $collection->__toJSON()); array_push($collections_as_json, $collection->__toArray());
} }
wp_reset_postdata(); wp_reset_postdata();
} }
return json_encode($collections_as_json); return $collections_as_json;
} }
elseif(!empty($item)){ elseif(!empty($item)){
return $item->__toJSON(); return $item->__toArray();
} }
return $item; return $item;
@ -165,6 +165,13 @@ class TAINACAN_REST_Collections_Controller extends WP_REST_Controller {
public function create_item( $request ) { public function create_item( $request ) {
$request = json_decode($request->get_body(), true); $request = json_decode($request->get_body(), true);
if(empty($request)){
return new WP_REST_Response([
'error_message' => __('Body can not be empty.', 'tainacan'),
'collection' => $request
], 400);
}
try { try {
$prepared_post = $this->prepare_item_for_database( $request ); $prepared_post = $this->prepare_item_for_database( $request );
} catch (\Error $exception){ } catch (\Error $exception){
@ -174,13 +181,13 @@ class TAINACAN_REST_Collections_Controller extends WP_REST_Controller {
if($prepared_post->validate()) { if($prepared_post->validate()) {
$collection = $this->collections_repository->insert( $prepared_post ); $collection = $this->collections_repository->insert( $prepared_post );
return new WP_REST_Response($collection->__toJSON(), 201); return new WP_REST_Response($collection->__toArray(), 201);
} }
return new WP_REST_Response([ return new WP_REST_Response([
'error_message' => __('One or more values are invalid.', 'tainacan'), 'error_message' => __('One or more values are invalid.', 'tainacan'),
'errors' => $prepared_post->get_errors(), 'errors' => $prepared_post->get_errors(),
'collection' => $prepared_post->__toJSON() 'collection' => $prepared_post->__toArray()
], 400); ], 400);
} }

View File

@ -37,7 +37,7 @@ class TAINACAN_REST_Items_Controller extends WP_REST_Controller {
array( array(
'methods' => WP_REST_Server::READABLE, 'methods' => WP_REST_Server::READABLE,
'callback' => array($this, 'get_items'), 'callback' => array($this, 'get_items'),
//'permission_callback' => array($this, 'get_items_permissions_check'), 'permission_callback' => array($this, 'get_items_permissions_check'),
'args' => $this->get_collection_params(), 'args' => $this->get_collection_params(),
), ),
array( array(
@ -83,15 +83,15 @@ class TAINACAN_REST_Items_Controller extends WP_REST_Controller {
while ( $item->have_posts() ) { while ( $item->have_posts() ) {
$item->the_post(); $item->the_post();
$ite = new Entities\Item($item->post); $ite = new Entities\Item($item->post);
array_push($items_as_array, $ite->__toJSON()); array_push($items_as_array, $ite->__toArray());
} }
wp_reset_postdata(); wp_reset_postdata();
} }
return json_encode($items_as_array); return $items_as_array;
} elseif(!empty($item)){ } elseif(!empty($item)){
return $item->__toJSON(); return $item->__toArray();
} }
return $item; return $item;
@ -139,6 +139,14 @@ class TAINACAN_REST_Items_Controller extends WP_REST_Controller {
return false; return false;
} }
public function get_items_permissions_check( $request ) {
if(current_user_can('read')){
return true;
}
return false;
}
/** /**
* @param WP_REST_Request $request * @param WP_REST_Request $request
* *
@ -185,6 +193,13 @@ class TAINACAN_REST_Items_Controller extends WP_REST_Controller {
$collection_id = $request['collection_id']; $collection_id = $request['collection_id'];
$item = json_decode($request->get_body(), true); $item = json_decode($request->get_body(), true);
if(empty($item)){
return new WP_REST_Response([
'error_message' => __('Body can not be empty.', 'tainacan'),
'item' => $item
], 400);
}
try { try {
$metadata = $this->prepare_item_for_database( [ $item, $collection_id ] ); $metadata = $this->prepare_item_for_database( [ $item, $collection_id ] );
} catch (\Error $exception){ } catch (\Error $exception){
@ -197,14 +212,14 @@ class TAINACAN_REST_Items_Controller extends WP_REST_Controller {
$item_metadata = new Entities\Item_Metadata_Entity($item, $metadata ); $item_metadata = new Entities\Item_Metadata_Entity($item, $metadata );
$metadata_added = $this->item_metadata->insert( $item_metadata ); $metadata_added = $this->item_metadata->insert( $item_metadata );
return new WP_REST_Response($metadata_added->get_item()->__toJSON(), 201 ); return new WP_REST_Response($metadata_added->get_item()->__toArray(), 201 );
} }
return new WP_REST_Response([ return new WP_REST_Response([
'error_message' => __('One or more values are invalid.', 'tainacan'), 'error_message' => __('One or more values are invalid.', 'tainacan'),
'errors' => $this->item->get_errors(), 'errors' => $this->item->get_errors(),
'item' => $this->item->__toJSON() 'item' => $this->item->__toArray()
], 400); ], 400);
} }

View File

@ -118,16 +118,16 @@ class TAINACAN_REST_Metadata_Controller extends WP_REST_Controller {
$metadata_added = $this->item_metadata_repository->insert($item_meta); $metadata_added = $this->item_metadata_repository->insert($item_meta);
} }
return new WP_REST_Response($metadata_added->get_metadata()->__toJSON(), 201); return new WP_REST_Response($metadata_added->get_metadata()->__toArray(), 201);
} }
else { else {
return new WP_REST_Response($this->metadata->__toJSON(), 201); return new WP_REST_Response($this->metadata->__toArray(), 201);
} }
} else { } else {
return new WP_REST_Response([ return new WP_REST_Response([
'error_message' => __('One or more values are invalid.', 'tainacan'), 'error_message' => __('One or more values are invalid.', 'tainacan'),
'errors' => $this->metadata->get_errors(), 'errors' => $this->metadata->get_errors(),
'metadata' => $this->metadata->__toJSON(), 'metadata' => $this->metadata->__toArray(),
], 400); ], 400);
} }
} elseif (!empty($request['item_id']) && !empty($request->get_body())){ } elseif (!empty($request['item_id']) && !empty($request->get_body())){
@ -146,16 +146,19 @@ class TAINACAN_REST_Metadata_Controller extends WP_REST_Controller {
if($item_metadata->validate()) { if($item_metadata->validate()) {
$metadata_updated = $this->item_metadata_repository->insert( $item_metadata ); $metadata_updated = $this->item_metadata_repository->insert( $item_metadata );
return new WP_REST_Response( $metadata_updated->__toJSON(), 201 ); return new WP_REST_Response( $metadata_updated->__toArray(), 201 );
} else { } else {
return new WP_REST_Response([ return new WP_REST_Response([
'error_message' => __('One or more values are invalid.', 'tainacan'), 'error_message' => __('One or more values are invalid.', 'tainacan'),
'errors' => $item_metadata->get_errors(), 'errors' => $item_metadata->get_errors(),
'item_metadata' => $item_metadata->__toJSON(), 'item_metadata' => $item_metadata->__toArray(),
], 400); ], 400);
} }
} else { } else {
return new WP_REST_Response($request->get_body(), 400); return new WP_REST_Response([
'error_message' => __('Body can not be empty.', 'tainacan'),
'item' => $request->get_body()
], 400);
} }
} }
@ -168,19 +171,21 @@ class TAINACAN_REST_Metadata_Controller extends WP_REST_Controller {
} }
public function prepare_item_for_response( $item, $request ) { public function prepare_item_for_response( $item, $request ) {
$metadata_as_json = []; $metadata_as = [];
if($request['item_id']) { if($request['item_id']) {
foreach ( $item as $metadata ) { foreach ( $item as $metadata ) {
$metadata_as_json[] = $metadata->__toJSON(); $metadata_as[] = $metadata->__toArray();
} }
} else { } else if($request['collection_id']){
foreach ( $item as $metadata ) { foreach ( $item as $metadata ) {
$metadata_as_json[] = $metadata->__toJSON(); $metadata_as[] = $metadata->__toArray();
} }
} }
return $metadata_as_json; //var_dump($metadata_as);
return $metadata_as;
} }
public function get_items( $request ) { public function get_items( $request ) {

View File

@ -0,0 +1,154 @@
<?php
use Tainacan\Entities;
use Tainacan\Repositories;
class TAINACAN_REST_Taxonomies_Controller extends WP_REST_Controller {
private $taxonomy;
private $taxonomy_repository;
public function __construct() {
$this->namespace = 'tainacan/v2';
$this->rest_base = 'taxonomies';
$this->taxonomy = new Entities\Taxonomy();
$this->taxonomy_repository = new Repositories\Taxonomies();
add_action('rest_api_init', array($this, 'register_routes'));
}
public function register_routes() {
register_rest_route(
$this->namespace, '/' . $this->rest_base,
array(
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array($this, 'get_items'),
'permission_callback' => array($this, 'get_items_permissions_check'),
),
array(
'methods' => WP_REST_Server::CREATABLE,
'callback' => array($this, 'create_item'),
'permission_callback' => array($this, 'create_item_permissions_check'),
)
)
);
register_rest_route(
$this->namespace, '/' . $this->rest_base . '/(?P<taxonomy_id>[\d]+)',
array(
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array($this, 'get_item'),
'permission_callback' => array($this, 'get_item_permissions_check'),
),
array(
'methods' => WP_REST_Server::DELETABLE,
'callback' => array($this, 'delete_item'),
'permission_callback' => array($this, 'delete_item_permissions_check'),
)
)
);
}
public function prepare_item_for_response( $item, $request ) {
$taxonomies = [];
if($request['taxonomy_id']){
return $item->__toArray();
}
foreach ( $item as $it ) {
$taxonomies[] = $it->__toArray();
}
return $taxonomies;
}
public function prepare_item_for_database( $request ) {
foreach ($request as $key => $value){
$set_ = 'set_' . $key;
$this->taxonomy->$set_($value);
}
}
public function get_item( $request ) {
$taxonomy_id = $request['taxonomy_id'];
$taxonomy = $this->taxonomy_repository->fetch($taxonomy_id);
$taxonomy_prepared = $this->prepare_item_for_response($taxonomy, $request);
return new WP_REST_Response($taxonomy_prepared, 200);
}
public function get_item_permissions_check( $request ) {
if(current_user_can('read')){
return true;
}
return false;
}
public function delete_item( $request ) {
return parent::delete_item( $request ); // TODO: Change the autogenerated stub
}
public function delete_item_permissions_check( $request ) {
if (current_user_can('delete_posts')){
return true;
}
return false;
}
public function get_items( $request ) {
$taxonomies = $this->taxonomy_repository->fetch([], 'OBJECT');
$taxonomies_prepared = $this->prepare_item_for_response($taxonomies, $request);
return new WP_REST_Response($taxonomies_prepared, 200);
}
public function get_items_permissions_check( $request ) {
if (current_user_can('read')){
return true;
}
return false;
}
public function create_item( $request ) {
$body = json_decode($request->get_body(), true);
if(!empty($body)){
$this->prepare_item_for_database($body);
if($this->taxonomy->validate()){
$taxonomy = $this->taxonomy_repository->insert($this->taxonomy);
return new WP_REST_Response($taxonomy->__toArray(), 201);
} else {
return new WP_REST_Response([
'error_message' => __('One or more values are invalid.', 'tainacan'),
'errors' => $this->taxonomy->get_errors(),
'item_metadata' => $this->taxonomy->__toArray(),
], 400);
}
} else {
return new WP_REST_Response([
'error_message' => __('Body can not be empty.', 'tainacan'),
], 400);
}
}
public function create_item_permissions_check( $request ) {
if(current_user_can('edit_posts')){
return true;
}
return false;
}
}
?>

View File

@ -0,0 +1,49 @@
<?php
use Tainacan\Entities;
use Tainacan\Repositories;
class TAINACAN_REST_Terms_Controller extends WP_REST_Controller {
private $term;
private $terms_repository;
private $taxonomy;
private $taxonomy_repository;
public function __construct() {
$this->namespace = 'tainacan/v2';
$this->rest_base = 'terms';
$this->term = new Entities\Term();
$this->terms_repository = new Repositories\Terms();
$this->taxonomy = new Entities\Taxonomy();
$this->taxonomy_repository = new Repositories\Taxonomies();
add_action('rest_api_init', array($this, 'register_routes'));
}
public function register_routes() {
register_rest_route($this->namespace, '/' . $this->rest_base . '/(?P<taxonomy_id>[\d]+)',
array(
array(
'methods' => WP_REST_Server::CREATABLE,
'callback' => array($this, 'create_item'),
'permission_callback' => array($this, 'create_item_permissions_check')
)
)
);
}
public function create_item( $request ) {
return parent::create_item( $request ); // TODO: Change the autogenerated stub
}
public function create_item_permissions_check( $request ) {
if(current_user_can('edit_posts')){
return true;
}
return false;
}
}
?>

View File

@ -3,6 +3,7 @@
$rest_collections_controller = new TAINACAN_REST_Collections_Controller(); $rest_collections_controller = new TAINACAN_REST_Collections_Controller();
$rest_items_controller = new TAINACAN_REST_Items_Controller(); $rest_items_controller = new TAINACAN_REST_Items_Controller();
$rest_metadata_controller = new TAINACAN_REST_Metadata_Controller(); $rest_metadata_controller = new TAINACAN_REST_Metadata_Controller();
$rest_taxonomies_controller = new TAINACAN_REST_Taxonomies_Controller();
// Add here other endpoints imports // Add here other endpoints imports
?> ?>

View File

@ -242,16 +242,19 @@ class Entity {
return true; return true;
} }
public function __toArray(){
global ${$this->repository};
$map = ${$this->repository}->get_map();
$attributes = [];
foreach($map as $prop => $content) {
$attributes[$prop] = $this->get_mapped_property($prop);
}
return $attributes;
}
public function __toJSON(){ public function __toJSON(){
global ${$this->repository}; return json_encode($this->__toArray(), JSON_NUMERIC_CHECK);
$map = ${$this->repository}->get_map();
$attributes = [];
foreach($map as $prop => $content) {
$attributes[$prop] = $this->get_mapped_property($prop);
}
return json_encode($attributes, JSON_NUMERIC_CHECK);
} }
} }

View File

@ -26,12 +26,12 @@ class Item_Metadata_Entity extends Entity {
return 'Hello, I\'m the Item Metadata Entity'; return 'Hello, I\'m the Item Metadata Entity';
} }
public function __toJSON(){ public function __toArray(){
$json['value'] = $this->get_value(); $as_array['value'] = $this->get_value();
$json['item'] = $this->get_item()->__toJSON(); $as_array['item'] = $this->get_item()->__toArray();
$json['metadata'] = $this->get_metadata()->__toJSON(); $as_array['metadata'] = $this->get_metadata()->__toArray();
return json_encode($json); return $as_array;
} }
/** /**

View File

@ -17,7 +17,7 @@ class Item extends Entity {
protected $repository = 'Tainacan_Items'; protected $repository = 'Tainacan_Items';
public function __toString(){ public function __toString(){
return 'Hello, my name is '. $this->get_name(); return 'Hello, my name is '. $this->get_title();
} }
/** /**

View File

@ -41,15 +41,15 @@ class TAINACAN_REST_Collections_Controller extends TAINACAN_UnitApiTestCase {
$response = $this->server->dispatch( $request ); $response = $this->server->dispatch( $request );
$this->assertEquals( 201, $response->get_status() ); $this->assertEquals( 201, $response->get_status() );
$collection = json_decode($response->get_data()); $collection = $response->get_data();
$id = $collection->id; $id = $collection['id'];
$requestGet = new \WP_REST_Request( 'GET', $this->namespace . '/collections/' . $id ); $requestGet = new \WP_REST_Request( 'GET', $this->namespace . '/collections/' . $id );
$responseGet = $this->server->dispatch( $requestGet ); $responseGet = $this->server->dispatch( $requestGet );
$this->assertEquals( 200, $responseGet->get_status() ); $this->assertEquals( 200, $responseGet->get_status() );
$data = json_decode($responseGet->get_data(), true); $data = $responseGet->get_data();
$this->assertEquals('TesteJsonAdd', $data['name']); $this->assertEquals('TesteJsonAdd', $data['name']);
} }
@ -72,13 +72,11 @@ class TAINACAN_REST_Collections_Controller extends TAINACAN_UnitApiTestCase {
$this->assertEquals( 200, $response->get_status() ); $this->assertEquals( 200, $response->get_status() );
$data = json_decode($response->get_data()); $data = $response->get_data();
//$data is a valid json? //$data is a valid json?
$this->assertTrue(json_last_error() === JSON_ERROR_NONE); //$this->assertTrue(json_last_error() === JSON_ERROR_NONE);
$this->assertContainsOnly('string', $data); $one_collection = $data[0];
$one_collection = json_decode($data[0], true);
$this->assertEquals('testeApi', $one_collection['name']); $this->assertEquals('testeApi', $one_collection['name']);
} }
@ -98,7 +96,7 @@ class TAINACAN_REST_Collections_Controller extends TAINACAN_UnitApiTestCase {
$this->assertEquals(200, $response->get_status()); $this->assertEquals(200, $response->get_status());
$data = json_decode($response->get_data(), true); $data = $response->get_data();
$this->assertEquals($collection1->get_id(), $data['id']); $this->assertEquals($collection1->get_id(), $data['id']);
@ -123,7 +121,7 @@ class TAINACAN_REST_Collections_Controller extends TAINACAN_UnitApiTestCase {
$this->assertEquals(200, $response->get_status()); $this->assertEquals(200, $response->get_status());
$data = json_decode($response->get_data(), true); $data = $response->get_data();
$this->assertEquals($collection2->get_id(), $data['id']); $this->assertEquals($collection2->get_id(), $data['id']);

View File

@ -26,7 +26,7 @@ class TAINACAN_REST_Items_Controller extends TAINACAN_UnitApiTestCase {
$this->assertEquals(201, $response->get_status()); $this->assertEquals(201, $response->get_status());
$data = json_decode($response->get_data(), true); $data = $response->get_data();
$this->assertEquals('Vue JS 2', $data['title']); $this->assertEquals('Vue JS 2', $data['title']);
} }
@ -66,12 +66,10 @@ class TAINACAN_REST_Items_Controller extends TAINACAN_UnitApiTestCase {
$this->assertEquals(200, $response->get_status()); $this->assertEquals(200, $response->get_status());
$data = json_decode($response->get_data(), true); $data = $response->get_data();
$this->assertContainsOnly('string', $data); $first_item = $data[0];
$second_item = $data[1];
$first_item = json_decode($data[0], true);
$second_item = json_decode($data[1], true);
$this->assertEquals($item2->get_title(), $first_item['title']); $this->assertEquals($item2->get_title(), $first_item['title']);
$this->assertEquals($item1->get_title(), $second_item['title']); $this->assertEquals($item1->get_title(), $second_item['title']);
@ -103,7 +101,7 @@ class TAINACAN_REST_Items_Controller extends TAINACAN_UnitApiTestCase {
$this->assertEquals(200, $response->get_status()); $this->assertEquals(200, $response->get_status());
$data = json_decode($response->get_data(), true); $data = $response->get_data();
$this->assertEquals($item1->get_title(), $data['title']); $this->assertEquals($item1->get_title(), $data['title']);
@ -136,7 +134,7 @@ class TAINACAN_REST_Items_Controller extends TAINACAN_UnitApiTestCase {
$this->assertEquals(200, $response->get_status()); $this->assertEquals(200, $response->get_status());
$data = json_decode($response->get_data(), true); $data = $response->get_data();
$this->assertEquals($item2->get_title(), $data['title']); $this->assertEquals($item2->get_title(), $data['title']);

View File

@ -37,7 +37,7 @@ class TAINACAN_REST_Metadata_Controller extends TAINACAN_UnitApiTestCase {
$response = $this->server->dispatch($request); $response = $this->server->dispatch($request);
$metadata_added = json_decode($response->get_data(), true); $metadata_added = $response->get_data();
$this->assertEquals('Moeda', $metadata_added['name']); $this->assertEquals('Moeda', $metadata_added['name']);
@ -58,8 +58,9 @@ class TAINACAN_REST_Metadata_Controller extends TAINACAN_UnitApiTestCase {
$response = $this->server->dispatch($request); $response = $this->server->dispatch($request);
$item_metadata_updated = json_decode($response->get_data(), true); $item_metadata_updated = $response->get_data();
$metadata = json_decode($item_metadata_updated['metadata'], true);
$metadata = $item_metadata_updated['metadata'];
$this->assertEquals($metadata_added['id'], $metadata['id']); $this->assertEquals($metadata_added['id'], $metadata['id']);
@ -122,9 +123,7 @@ class TAINACAN_REST_Metadata_Controller extends TAINACAN_UnitApiTestCase {
$data = $response->get_data(); $data = $response->get_data();
$this->assertContainsOnly('string', $data); $metadata = $data[0];
$metadata = json_decode($data[0], true);
$this->assertEquals('Data', $metadata['name']); $this->assertEquals('Data', $metadata['name']);
@ -139,10 +138,8 @@ class TAINACAN_REST_Metadata_Controller extends TAINACAN_UnitApiTestCase {
$data = $response->get_data(); $data = $response->get_data();
$this->assertContainsOnly('string', $data); $item_metadata = $data[0];
$metadata = $item_metadata['metadata'];
$item_metadata = json_decode($data[0], true);
$metadata = json_decode($item_metadata['metadata'], true);
$this->assertEquals('Data', $metadata['name']); $this->assertEquals('Data', $metadata['name']);
$this->assertEquals('12/12/2017', $item_metadata['value']); $this->assertEquals('12/12/2017', $item_metadata['value']);

View File

@ -0,0 +1,85 @@
<?php
namespace Tainacan\Tests;
class TAINACAN_REST_Taxonomies_Controller extends TAINACAN_UnitApiTestCase {
public function test_create_taxonomy(){
$taxonomy_json = json_encode([
'name' => 'Nome',
'description' => 'desc',
'allow_insert' => 'yes',
'status' => 'publish'
]);
$request = new \WP_REST_Request(
'POST', $this->namespace . '/taxonomies'
);
$request->set_body($taxonomy_json);
$response = $this->server->dispatch($request);
$data = $response->get_data();
$this->assertEquals('Nome', $data['name']);
}
public function test_get_taxonomy_by_id(){
$taxonomy = $this->tainacan_entity_factory->create_entity(
'taxonomy',
array(
'name' => '1genero',
'description' => 'tipos',
'allow_insert' => 'yes'
),
true
);
$request = new \WP_REST_Request(
'GET', $this->namespace . '/taxonomies/' . $taxonomy->get_id()
);
$response = $this->server->dispatch($request);
$data = $response->get_data();
$this->assertEquals($taxonomy->get_name(), $data['name']);
}
public function test_get_all_taxonomies(){
$taxonomy1 = $this->tainacan_entity_factory->create_entity(
'taxonomy',
array(
'name' => '1genero',
'description' => 'tipos de musica',
'allow_insert' => 'yes',
'status' => 'publish'
),
true
);
$taxonomy2 = $this->tainacan_entity_factory->create_entity(
'taxonomy',
array(
'name' => '2genero',
'description' => 'tipos',
'allow_insert' => 'yes',
'status' => 'publish'
),
true
);
$request = new \WP_REST_Request(
'GET', $this->namespace . '/taxonomies'
);
$response = $this->server->dispatch($request);
$data = $response->get_data();
$this->assertEquals($taxonomy1->get_name(), $data[1]['name']);
$this->assertEquals($taxonomy2->get_name(), $data[0]['name']);
}
}
?>