diff --git a/src/api/endpoints/class-tainacan-rest-items-controller.php b/src/api/endpoints/class-tainacan-rest-items-controller.php index c3dc6b373..799934d2e 100644 --- a/src/api/endpoints/class-tainacan-rest-items-controller.php +++ b/src/api/endpoints/class-tainacan-rest-items-controller.php @@ -11,6 +11,7 @@ use Tainacan\Entities; class TAINACAN_REST_Items_Controller extends WP_REST_Controller { private $items_repository; private $item; + private $item_metadata; /** * TAINACAN_REST_Items_Controller constructor. @@ -21,6 +22,7 @@ class TAINACAN_REST_Items_Controller extends WP_REST_Controller { $this->rest_base = 'items'; $this->items_repository = new Repositories\Items(); $this->item = new Entities\Item(); + $this->item_metadata = new Repositories\Item_Metadata(); add_action('rest_api_init', array($this, 'register_routes')); } @@ -142,12 +144,27 @@ class TAINACAN_REST_Items_Controller extends WP_REST_Controller { $this->item->set_title($request[0]['title']); $this->item->set_description($request[0]['description']); - $collection_wp_post = get_post($request[1]); - $collection = new Entities\Collection($collection_wp_post); + $collection = new Entities\Collection($request[1]); $this->item->set_collection($collection); - return $this->item; + $metadata = get_post_meta($collection->get_id()); + + if(!empty($metadata)) { + foreach ($metadata as $key => $value){ + $new_metadata = new Entities\Metadata(); + + try { + $set_ = 'set_' . $key; + $new_metadata->$set_( $value ); + } catch (\Error $exception){ + //echo $exception->getMessage(); + } + } + + } + + return $new_metadata; } /** @@ -159,15 +176,18 @@ class TAINACAN_REST_Items_Controller extends WP_REST_Controller { $collection_id = $request['collection_id']; $item = json_decode($request->get_body(), true); - $prepared_item = $this->prepare_item_for_database([$item, $collection_id]); + $metadata = $this->prepare_item_for_database([$item, $collection_id]); - if($prepared_item->validate()){ - $item = $this->items_repository->insert($prepared_item); + if($this->item->validate()) { + $item = $this->items_repository->insert($this->item ); - return new WP_REST_Response($item->__toJSON(), 201); + $item_metadata = new Entities\Item_Metadata_Entity($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($prepared_item->get_errors(), 400); + return new WP_REST_Response($item->get_errors(), 400); } /** diff --git a/src/api/endpoints/class-tainacan-rest-metadata-controller.php b/src/api/endpoints/class-tainacan-rest-metadata-controller.php new file mode 100644 index 000000000..a0098561c --- /dev/null +++ b/src/api/endpoints/class-tainacan-rest-metadata-controller.php @@ -0,0 +1,152 @@ +namespace = 'tainacan/v2'; + $this->rest_base = 'metadata'; + + $this->metadata = new Entities\Metadata(); + $this->metadata_repository = new Repositories\Metadatas(); + $this->item_metadata_repository = new Repositories\Item_Metadata(); + $this->item_repository = new Repositories\Items(); + + add_action('rest_api_init', array($this, 'register_routes')); + } + + + /** + * If POST on metadata/collection/, then + * a metadata will be created in matched collection and all your item will receive this metadata + * + * If POST on metadata/item/, then a value will be added in a field and metadata passed + * id body of requisition + * + * Both of GETs return the metadata of matched objects + */ + public function register_routes() { + register_rest_route($this->namespace, '/' . $this->rest_base . '/collection/(?P[\d]+)', + 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') + ), + ) + ); + register_rest_route($this->namespace, '/' . $this->rest_base . '/item/(?P[\d]+)', + 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') + ) + ) + ); + } + + public function prepare_item_for_database( $request ) { + $meta = json_decode($request[0]->get_body(), true); + + $this->metadata->set_name($meta['name']); + $this->metadata->set_description($meta['description']); + $this->metadata->set_field_type($meta['field_type']); + + $collection = new Entities\Collection($request[1]); + + $this->metadata->set_collection($collection); + } + + public function create_item( $request ) { + if(!empty($request['collection_id'])){ + $collection_id = $request['collection_id']; + + $this->prepare_item_for_database([$request, $collection_id]); + + if($this->metadata->validate()) { + $this->metadata_repository->insert( $this->metadata ); + + $items = $this->item_repository->fetch([], $collection_id, 'WP_Query'); + + $metadata_added = ''; + if($items->have_posts()){ + while ($items->have_posts()){ + $items->the_post(); + + $item = new Entities\Item($items->post); + $item_meta = new Entities\Item_Metadata_Entity($item, $this->metadata); + + $metadata_added = $this->item_metadata_repository->insert($item_meta); + } + } + + return new WP_REST_Response($metadata_added->get_metadata()->__toJSON(), 201); + } + } elseif (!empty($request['item_id']) && !empty($request->get_body())){ + $body = json_decode($request->get_body(), true); + + $item_id = $request['item_id']; + $metadata_id = $body['metadata_id']; + $value = $body['values']; + + $item = $this->item_repository->fetch($item_id); + $metadata = $this->metadata_repository->fetch($metadata_id); + + $item_metadata = new Entities\Item_Metadata_Entity($item, $metadata); + $item_metadata->set_value($value); + + if($item_metadata->validate()) { + $metadata_updated = $this->item_metadata_repository->insert( $item_metadata ); + + return new WP_REST_Response( $metadata_updated->get_metadata()->__toJSON(), 201 ); + } + } else { + return new WP_REST_Response($request->get_body(), 400); + } + } + + public function create_item_permissions_check( $request ) { + return true; + } + + public function prepare_item_for_response( $item, $request ) { + + } + + public function get_items( $request ) { + $id = !empty($request['collection_id']) ? $request['collection_id'] : (!empty($request['item_id']) ? $request['item_id'] : ''); + + $this->metadata_repository; + } + + public function get_item_permissions_check( $request ) { + return true; + } + + public function get_collection_params() { + return parent::get_collection_params(); // TODO: Change the autogenerated stub + } + +} + +?> \ No newline at end of file diff --git a/src/api/tainacan-rest-creator.php b/src/api/tainacan-rest-creator.php index 6537cdae9..3a5203e50 100644 --- a/src/api/tainacan-rest-creator.php +++ b/src/api/tainacan-rest-creator.php @@ -2,6 +2,7 @@ $rest_collections_controller = new TAINACAN_REST_Collections_Controller(); $rest_items_controller = new TAINACAN_REST_Items_Controller(); +$rest_metadata_controller = new TAINACAN_REST_Metadata_Controller(); // Add here other endpoints imports ?> \ No newline at end of file diff --git a/tests/test-api-metadata.php b/tests/test-api-metadata.php new file mode 100644 index 000000000..11d06c231 --- /dev/null +++ b/tests/test-api-metadata.php @@ -0,0 +1,73 @@ +tainacan_entity_factory->create_entity('collection', '', true); + + $item = $this->tainacan_entity_factory->create_entity( + 'item', + array( + 'title' => 'No name', + 'description' => 'No description', + 'collection' => $collection + ), + true + ); + + $field = $this->tainacan_field_factory->create_field('text', '', true); + + $metadata = json_encode( + array( + 'name' => 'Moeda', + 'description' => 'Descreve campo moeda.', + 'field_type' => $field->get_primitive_type(), + ) + ); + + $request = new \WP_REST_Request( + 'POST', + $this->namespaced_route . '/metadata/collection/' . $collection->get_id() + ); + $request->set_body($metadata); + + $response = $this->server->dispatch($request); + + $metadata_added = json_decode($response->get_data(), true); + + $this->assertEquals('Moeda', $metadata_added['name']); + + + #################### + + $meta_values = json_encode( + array( + 'metadata_id' => $metadata_added['id'], + 'values' => 'Valorado' + ) + ); + + $request = new \WP_REST_Request( + 'POST', + $this->namespaced_route . '/metadata/item/' . $item->get_id() + ); + $request->set_body($meta_values); + + $response = $this->server->dispatch($request); + + $metadata_updated = json_decode($response->get_data(), true); + + $this->assertEquals($metadata_added['id'], $metadata_updated['id']); + + $metav = get_post_meta($item->get_id(), $metadata_updated['id'], true); + + $this->assertEquals('Valorado', $metav); + } + +} + +?> \ No newline at end of file