diff --git a/src/api/endpoints/class-tainacan-rest-filters-controller.php b/src/api/endpoints/class-tainacan-rest-filters-controller.php index ba8332674..56bf27ebc 100644 --- a/src/api/endpoints/class-tainacan-rest-filters-controller.php +++ b/src/api/endpoints/class-tainacan-rest-filters-controller.php @@ -38,6 +38,11 @@ class TAINACAN_REST_Filters_Controller extends WP_REST_Controller { 'methods' => WP_REST_Server::CREATABLE, 'callback' => array($this, 'create_item'), 'permission_callback' => array($this, 'create_item_permissions_check') + ), + array( + 'methods' => WP_REST_Server::READABLE, + 'callback' => array($this, 'get_items'), + 'permission_callback' => array($this, 'get_items_permissions_check') ) )); register_rest_route($this->namespace, '/' . $this->rest_base . '/(?P[\d]+)', array( @@ -50,6 +55,11 @@ class TAINACAN_REST_Filters_Controller extends WP_REST_Controller { 'methods' => WP_REST_Server::EDITABLE, 'callback' => array($this, 'update_item'), 'permission_callback' => array($this, 'update_item_permissions_check') + ), + array( + 'methods' => WP_REST_Server::READABLE, + 'callback' => array($this, 'get_item'), + 'permission_callback' => array($this, 'get_item_permissions_check') ) )); } @@ -132,6 +142,11 @@ class TAINACAN_REST_Filters_Controller extends WP_REST_Controller { return $this->filter_repository->can_edit($this->filter); } + /** + * @param WP_REST_Request $request + * + * @return WP_Error|WP_REST_Response + */ public function delete_item( $request ) { $filter_id = $request['filter_id']; @@ -151,11 +166,21 @@ class TAINACAN_REST_Filters_Controller extends WP_REST_Controller { ], 400); } + /** + * @param WP_REST_Request $request + * + * @return bool|WP_Error + */ public function delete_item_permissions_check( $request ) { $filter = $this->filter_repository->fetch($request['filter_id']); return $this->filter_repository->can_delete($filter); } + /** + * @param $request + * + * @return WP_Error|WP_REST_Response + */ public function update_item( $request ) { $filter_id = $request['filter_id']; @@ -180,9 +205,80 @@ class TAINACAN_REST_Filters_Controller extends WP_REST_Controller { } + /** + * @param WP_REST_Request $request + * + * @return bool|WP_Error + */ public function update_item_permissions_check( $request ) { $filter = $this->filter_repository->fetch($request['filter_id']); return $this->filter_repository->can_edit($filter); } + + /** + * @param mixed $object + * @param WP_REST_Request $request + * + * @return array|mixed|WP_Error|WP_REST_Response + */ + public function prepare_item_for_response( $object, $request ) { + + if(is_array($object)){ + $filters = []; + + foreach ($object as $item){ + $filters[] = $item->__toArray(); + } + + return $filters; + } + + return $object; + } + + /** + * @param WP_REST_Request $request + * + * @return WP_Error|WP_REST_Response + */ + public function get_items( $request ) { + $filters = $this->filter_repository->fetch([], 'OBJECT'); + + $response = $this->prepare_item_for_response($filters, $request); + + return new WP_REST_Response($response, 200); + } + + /** + * @param WP_REST_Request $request + * + * @return bool|WP_Error + */ + public function get_items_permissions_check( $request ) { + return $this->filter_repository->can_read($this->filter); + } + + /** + * @param WP_REST_Request $request + * + * @return WP_Error|WP_REST_Response + */ + public function get_item( $request ) { + $filter_id = $request['filter_id']; + + $filter = $this->filter_repository->fetch($filter_id); + + return new WP_REST_Response($filter->__toArray(), 200); + } + + /** + * @param WP_REST_Request $request + * + * @return bool|WP_Error + */ + public function get_item_permissions_check( $request ) { + $filter = $this->filter_repository->fetch($request['filter_id']); + return $this->filter_repository->can_read($filter); + } } ?> \ No newline at end of file diff --git a/src/api/endpoints/class-tainacan-rest-taxonomies-controller.php b/src/api/endpoints/class-tainacan-rest-taxonomies-controller.php index 4cbf7026b..240f1a67b 100644 --- a/src/api/endpoints/class-tainacan-rest-taxonomies-controller.php +++ b/src/api/endpoints/class-tainacan-rest-taxonomies-controller.php @@ -48,6 +48,11 @@ class TAINACAN_REST_Taxonomies_Controller extends WP_REST_Controller { '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') ) ) ); @@ -227,6 +232,43 @@ class TAINACAN_REST_Taxonomies_Controller extends WP_REST_Controller { return $this->taxonomy_repository->can_edit($this->taxonomy); } + /** + * @param WP_REST_Request $request + * + * @return WP_Error|WP_REST_Response + */ + public function update_item( $request ) { + $taxonomy_id = $request['taxonomy_id']; + + $body = json_decode($request->get_body(), true); + + if(!empty($body)){ + $attributes = ['ID' => $taxonomy_id]; + + foreach ($body as $att => $value){ + $attributes[$att] = $value; + } + + $updated_taxonomy = $this->taxonomy_repository->update($attributes); + + return new WP_REST_Response($updated_taxonomy->__toArray(), 200); + } + + 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 + */ + public function update_item_permissions_check( $request ) { + $taxonomy = $this->taxonomy_repository->fetch($request['taxonomy_id']); + return $this->taxonomy_repository->can_edit($taxonomy); + } } ?> \ No newline at end of file diff --git a/src/api/endpoints/class-tainacan-rest-terms-controller.php b/src/api/endpoints/class-tainacan-rest-terms-controller.php index 7e16de469..e7edfc1bd 100644 --- a/src/api/endpoints/class-tainacan-rest-terms-controller.php +++ b/src/api/endpoints/class-tainacan-rest-terms-controller.php @@ -31,6 +31,11 @@ class TAINACAN_REST_Terms_Controller extends WP_REST_Controller { 'methods' => WP_REST_Server::CREATABLE, 'callback' => array($this, 'create_item'), 'permission_callback' => array($this, 'create_item_permissions_check') + ), + array( + 'methods' => WP_REST_Server::READABLE, + 'callback' => array($this, 'get_items'), + 'permission_callback' => array($this, 'get_items_permissions_check') ) ) ); @@ -39,7 +44,17 @@ class TAINACAN_REST_Terms_Controller extends WP_REST_Controller { array( 'methods' => WP_REST_Server::DELETABLE, 'callback' => array($this, 'delete_item'), - 'permission_callbacl' => array($this, 'delete_item_permissions_check') + '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') + ), + array( + 'methods' => WP_REST_Server::READABLE, + 'callback' => array($this, 'get_item'), + 'permission_callback' => array($this, 'get_item_permissions_check') ) ) ); @@ -112,6 +127,11 @@ class TAINACAN_REST_Terms_Controller extends WP_REST_Controller { return $this->terms_repository->can_edit($this->term); } + /** + * @param WP_REST_Request $request + * + * @return WP_Error|WP_REST_Response + */ public function delete_item( $request ) { $term_id = $request['term_id']; $taxonomy_id = $request['taxonomy_id']; @@ -131,10 +151,137 @@ class TAINACAN_REST_Terms_Controller extends WP_REST_Controller { return new WP_REST_Response($is_deleted, 200); } + /** + * @param WP_REST_Request $request + * + * @return bool|WP_Error + */ public function delete_item_permissions_check( $request ) { - $term = $this->terms_repository->fetch($request['term_id']); + $term = new Entities\Term($this->terms_repository->fetch($request['term_id'])); return $this->terms_repository->can_delete($term); } + + /** + * @param WP_REST_Request $request + * + * @return WP_Error|WP_REST_Response + */ + public function update_item( $request ) { + $term_id = $request['term_id']; + $taxonomy_id = $request['taxonomy_id']; + + $body = json_decode($request->get_body(), true); + + if(!empty($body)){ + $taxonomy_name = $this->taxonomy_repository->fetch($taxonomy_id)->get_db_identifier(); + + $identifiers = [ + 'term_id' => $term_id, + 'tax_name' => $taxonomy_name + ]; + + $attributes = []; + + foreach ($body as $att => $value){ + $attributes[$att] = $value; + } + + $updated_term = $this->terms_repository->update([$attributes, $identifiers]); + + return new WP_REST_Response($updated_term->__toArray(), 200); + } + + 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 + */ + public function update_item_permissions_check( $request ) { + $term = new Entities\Term($this->terms_repository->fetch($request['term_id'])); + return $this->terms_repository->can_edit($term); + } + + /** + * @param mixed $item + * @param WP_REST_Request $request + * + * @return array|mixed|WP_Error|WP_REST_Response + */ + public function prepare_item_for_response( $item, $request ) { + + if(is_array($item)){ + $prepared = []; + + foreach ($item as $term){ + $prepared[] = $term->__toArray(); + } + + return $prepared; + } + + return $item; + } + + /** + * @param WP_REST_Request $request + * + * @return WP_Error|WP_REST_Response + */ + public function get_items( $request ) { + $taxonomy_id = $request['taxonomy_id']; + + $taxonomy = $this->taxonomy_repository->fetch($taxonomy_id); + + $args = json_decode($request->get_body(), true); + + $terms = $this->terms_repository->fetch($args, $taxonomy); + + $prepared_terms = $this->prepare_item_for_response($terms, $request); + + return new WP_REST_Response($prepared_terms, 200); + } + + /** + * @param WP_REST_Request $request + * + * @return bool|WP_Error + */ + public function get_items_permissions_check( $request ) { + $taxonomy = $this->taxonomy_repository->fetch($request['taxonomy_id']); + return $this->taxonomy_repository->can_read($taxonomy); + } + + /** + * @param WP_REST_Request $request + * + * @return WP_Error|WP_REST_Response + */ + public function get_item( $request ) { + $term_id = $request['term_id']; + $tax_id = $request['taxonomy_id']; + + $taxonomy = $this->taxonomy_repository->fetch($tax_id); + + $term = $this->terms_repository->fetch($term_id, $taxonomy); + + return new WP_REST_Response($term->__toArray(), 200); + } + + /** + * @param WP_REST_Request $request + * + * @return bool|WP_Error + */ + public function get_item_permissions_check( $request ) { + $taxonomy = $this->taxonomy_repository->fetch($request['taxonomy_id']); + return $this->taxonomy_repository->can_read($taxonomy); + } } ?> \ No newline at end of file diff --git a/src/classes/repositories/class-tainacan-taxonomies.php b/src/classes/repositories/class-tainacan-taxonomies.php index c2b106d0b..ff6ca6fc4 100644 --- a/src/classes/repositories/class-tainacan-taxonomies.php +++ b/src/classes/repositories/class-tainacan-taxonomies.php @@ -180,7 +180,22 @@ class Taxonomies extends Repository { } public function update($object){ + $map = $this->get_map(); + $entity = []; + + foreach ($object as $key => $value) { + if($key != 'ID') { + $entity[$map[$key]['map']] = $value ; + } elseif ($key == 'ID'){ + $entity[$key] = (int) $value; + } + } + + $updated_taxonomy = new Entities\Taxonomy(wp_update_post($entity)); + $updated_taxonomy->register_taxonomy(); + + return $updated_taxonomy; } public function delete($args){ diff --git a/src/classes/repositories/class-tainacan-terms.php b/src/classes/repositories/class-tainacan-terms.php index 3852a5819..d65f482f7 100644 --- a/src/classes/repositories/class-tainacan-terms.php +++ b/src/classes/repositories/class-tainacan-terms.php @@ -156,7 +156,20 @@ class Terms extends Repository { } public function update($object){ + $map = $this->get_map(); + $entity = []; + + foreach ($object[0] as $key => $value) { + if($key != 'ID') { + $entity[$map[$key]['map']] = $value ; + } + } + + $term_tax_ids = wp_update_term($object[1]['term_id'], $object[1]['tax_name'], $entity); + $term_id = (int) $term_tax_ids['term_id']; + + return new Entities\Term($term_id, $object[1]['tax_name']); } public function delete($args){ diff --git a/tests/test-api-filters.php b/tests/test-api-filters.php index 81fe8717c..8028a660e 100644 --- a/tests/test-api-filters.php +++ b/tests/test-api-filters.php @@ -131,6 +131,7 @@ class TAINACAN_REST_Terms_Controller extends TAINACAN_UnitApiTestCase { array( 'name' => 'Metadata filtered', 'description' => 'Is filtered', + 'collection_id' => $collection->get_id() ) ); @@ -165,6 +166,87 @@ class TAINACAN_REST_Terms_Controller extends TAINACAN_UnitApiTestCase { $this->assertNotEquals('filtro', $data['name']); $this->assertEquals('Faceta', $data['name']); } + + public function test_fetch_filters(){ + $collection = $this->tainacan_entity_factory->create_entity( + 'collection', + array( + 'name' => 'Collection filtered', + 'description' => 'Is filtered', + ), + true + ); + + $metadata = $this->tainacan_entity_factory->create_entity( + 'metadata', + array( + 'name' => 'Metadata filtered', + 'description' => 'Is filtered', + 'collection_id' => $collection->get_id() + ) + ); + + $metadata2 = $this->tainacan_entity_factory->create_entity( + 'metadata', + array( + 'name' => 'Other filtered', + 'description' => 'Is filtered', + 'collection_id' => $collection->get_id() + ) + ); + + $filter_type = $this->tainacan_filter_factory->create_filter('range'); + + $filter = $this->tainacan_entity_factory->create_entity( + 'filter', + array( + 'name' => 'filtro', + 'collection' => $collection, + 'description' => 'descricao', + 'metadata' => $metadata, + 'filter_type' => $filter_type, + 'status' => 'publish' + ), + true + ); + + $filter2 = $this->tainacan_entity_factory->create_entity( + 'filter', + array( + 'name' => 'filtro2', + 'collection' => $collection, + 'description' => 'descricao', + 'metadata' => $metadata2, + 'filter_type' => $filter_type, + 'status' => 'publish' + ), + true + ); + + $request = new \WP_REST_Request('GET', $this->namespace . '/filters'); + + $response = $this->server->dispatch($request); + + $data = $response->get_data(); + + $first_filter = $data[0]; + $second_filter = $data[1]; + + $this->assertEquals($filter2->get_name(), $first_filter['name']); + $this->assertEquals($filter->get_name(), $second_filter['name']); + + #### FETCH A FILTER #### + + $request = new \WP_REST_Request( + 'GET', $this->namespace . '/filters/' . $filter->get_id() + ); + + $response = $this->server->dispatch($request); + + $data = $response->get_data(); + + $this->assertEquals('filtro', $data['name']); + } } ?> \ No newline at end of file diff --git a/tests/test-api-taxonomies.php b/tests/test-api-taxonomies.php index d7898b89f..55d7a2cf9 100644 --- a/tests/test-api-taxonomies.php +++ b/tests/test-api-taxonomies.php @@ -122,6 +122,44 @@ class TAINACAN_REST_Taxonomies_Controller extends TAINACAN_UnitApiTestCase { $this->assertEquals($taxonomy1->get_name(), $data[1]['name']); $this->assertEquals($taxonomy2->get_name(), $data[0]['name']); } + + public function test_update_taxonomy(){ + $taxonomy = $this->tainacan_entity_factory->create_entity( + 'taxonomy', + array( + 'name' => 'Gender', + 'description' => 'Music types', + 'allow_insert' => 'yes', + 'status' => 'publish' + ), + true + ); + + $new_attributes = json_encode([ + 'name' => 'People', + 'description' => 'Male or Female' + ]); + + $request = new \WP_REST_Request('PATCH', $this->namespace . '/taxonomies/' . $taxonomy->get_id()); + + $request->set_body($new_attributes); + + $response = $this->server->dispatch($request); + + $data = $response->get_data(); + + $args=array( + 'name' => $taxonomy->get_db_identifier() + ); + $output = 'objects'; + + $tax = get_taxonomies($args, $output); + + $this->assertNotEquals($taxonomy->get_name(), $data['name']); + $this->assertEquals('People', $data['name']); + + $this->assertEquals('People', $tax[$taxonomy->get_db_identifier()]->label); + } } ?> \ No newline at end of file diff --git a/tests/test-api-terms.php b/tests/test-api-terms.php index b67d3fae3..bdc1f89dd 100644 --- a/tests/test-api-terms.php +++ b/tests/test-api-terms.php @@ -70,6 +70,113 @@ class TAINACAN_REST_Terms extends TAINACAN_UnitApiTestCase { $this->assertNull($term); } + + public function test_update_term(){ + $taxonomy = $this->tainacan_entity_factory->create_entity( + 'taxonomy', + array( + 'name' => '1genero', + 'description' => 'tipos de musica', + 'allow_insert' => 'yes', + 'status' => 'publish' + ), + true + ); + + $term = $this->tainacan_entity_factory->create_entity( + 'term', + array( + 'taxonomy' => $taxonomy->get_db_identifier(), + 'name' => 'Rock', + 'user' => get_current_user_id(), + ), + true + ); + + $new_attributes = json_encode([ + 'name' => 'Trap' + ]); + + $request = new \WP_REST_Request( + 'PATCH', $this->namespace . '/terms/' . $term . '/taxonomy/' . $taxonomy->get_id() + ); + + $request->set_body($new_attributes); + + $response = $this->server->dispatch($request); + + $data = $response->get_data(); + + $this->assertNotEquals('Rock', $data['name']); + $this->assertEquals('Trap', $data['name']); + + $this->assertEquals($taxonomy->get_db_identifier(), $data['taxonomy']); + } + + public function test_fetch_terms(){ + $taxonomy = $this->tainacan_entity_factory->create_entity( + 'taxonomy', + array( + 'name' => 'Gender', + 'description' => 'Music types', + 'allow_insert' => 'yes', + 'status' => 'publish' + ), + true + ); + + $this->tainacan_entity_factory->create_entity( + 'term', + array( + 'taxonomy' => $taxonomy->get_db_identifier(), + 'name' => 'Rock', + 'user' => get_current_user_id(), + ), + true + ); + + $term2 = $this->tainacan_entity_factory->create_entity( + 'term', + array( + 'taxonomy' => $taxonomy->get_db_identifier(), + 'name' => 'Trap', + 'user' => get_current_user_id(), + ), + true + ); + + $show_empty = json_encode([ + 'hide_empty' => false + ]); + + $request = new \WP_REST_Request( + 'GET', $this->namespace . '/terms/taxonomy/' . $taxonomy->get_id() + ); + + $request->set_body($show_empty); + + $response = $this->server->dispatch($request); + + $data = $response->get_data(); + + $termA = $data[0]; + $termB = $data[1]; + + $this->assertEquals('Trap', $termB['name']); + $this->assertEquals('Rock', $termA['name']); + + #### FETCH A TERM #### + + $request = new \WP_REST_Request( + 'GET', $this->namespace . '/terms/' . $term2 . '/taxonomy/' . $taxonomy->get_id() + ); + + $response = $this->server->dispatch($request); + + $data = $response->get_data(); + + $this->assertEquals('Trap', $data['name']); + } } ?> \ No newline at end of file