Adds schema for metadata sections. #792.

This commit is contained in:
mateuswetah 2023-07-04 09:46:36 -03:00
parent 3f474c8d97
commit a118165836
4 changed files with 98 additions and 39 deletions

File diff suppressed because one or more lines are too long

View File

@ -1565,12 +1565,6 @@ class REST_Items_Controller extends REST_Controller {
}
function get_attachments_schema() {
$schema = [
'$schema' => 'http://json-schema.org/draft-04/schema#',
'title' => 'attachments',
'type' => 'object',
'tags' => ['attachments', $this->rest_base],
];
$properties = [
'title' => [
@ -1616,13 +1610,21 @@ class REST_Items_Controller extends REST_Controller {
],
];
$schema['properties'] = array_merge(
parent::get_base_properties_schema(),
$properties
);
$schema = [
'$schema' => 'http://json-schema.org/draft-04/schema#',
'title' => 'attachments',
'type' => 'array',
'tags' => ['attachments', $this->rest_base],
'items' => array(
'type' => 'object',
'properties' => array_merge(
parent::get_base_properties_schema(),
$properties
)
)
];
return $schema;
}
function get_schema() {

View File

@ -7,8 +7,6 @@ use Tainacan\Entities;
use Tainacan\Repositories;
class REST_Metadata_Controller extends REST_Controller {
private $item_metadata_repository;
private $item_repository;
private $collection_repository;
private $metadatum_repository;
@ -25,8 +23,6 @@ class REST_Metadata_Controller extends REST_Controller {
*/
public function init_objects() {
$this->metadatum_repository = Repositories\Metadata::get_instance();
$this->item_metadata_repository = Repositories\Item_Metadata::get_instance();
$this->item_repository = Repositories\Items::get_instance();
$this->collection_repository = Repositories\Collections::get_instance();
}
@ -57,6 +53,15 @@ class REST_Metadata_Controller extends REST_Controller {
'description' => __( 'Metadatum ID', 'tainacan' ),
'required' => true,
],
'context' => array(
'type' => 'string',
'default' => 'view',
'description' => 'The context in which the request is made.',
'enum' => array(
'view',
'edit'
)
),
],
),
array(

View File

@ -47,7 +47,15 @@ class REST_Metadata_Sections_Controller extends REST_Controller {
'methods' => \WP_REST_Server::READABLE,
'callback' => array($this, 'get_item'),
'permission_callback' => array($this, 'get_item_permissions_check'),
'args' => array(
'args' => [
'collection_id' => [
'description' => __( 'Collection ID', 'tainacan' ),
'required' => true,
],
'metadata_section_id' => [
'description' => __( 'Metadata Section ID', 'tainacan' ),
'required' => true,
],
'context' => array(
'type' => 'string',
'default' => 'view',
@ -57,7 +65,7 @@ class REST_Metadata_Sections_Controller extends REST_Controller {
'edit'
)
),
),
],
),
array(
'methods' => \WP_REST_Server::EDITABLE,
@ -69,6 +77,7 @@ class REST_Metadata_Sections_Controller extends REST_Controller {
'methods' => \WP_REST_Server::DELETABLE,
'callback' => array($this, 'delete_item'),
'permission_callback' => array($this, 'delete_item_permissions_check'),
'args' => $this->get_endpoint_args_for_item_schema(\WP_REST_Server::DELETABLE)
),
'schema' => [$this, 'get_schema']
)
@ -79,7 +88,15 @@ class REST_Metadata_Sections_Controller extends REST_Controller {
'methods' => \WP_REST_Server::READABLE,
'callback' => array($this, 'get_items'),
'permission_callback' => array($this, 'get_items_permissions_check'),
'args' => $this->get_wp_query_params(),
'args' => array_merge(
array(
'collection_id' => [
'description' => __( 'Collection ID', 'tainacan' ),
'required' => true,
],
),
$this->get_wp_query_params()
),
),
array(
'methods' => \WP_REST_Server::CREATABLE,
@ -87,7 +104,7 @@ class REST_Metadata_Sections_Controller extends REST_Controller {
'permission_callback' => array($this, 'create_item_permissions_check'),
'args' => $this->get_endpoint_args_for_item_schema(\WP_REST_Server::CREATABLE),
),
'schema' => [$this, 'get_schema']
'schema' => [$this, 'get_list_schema']
)
);
register_rest_route($this->namespace, '/collection/(?P<collection_id>[\d]+)/' . $this->rest_base . '/(?P<metadata_section_id>[\d|default_section]+)/metadata',
@ -107,10 +124,10 @@ class REST_Metadata_Sections_Controller extends REST_Controller {
array(
'methods' => \WP_REST_Server::DELETABLE,
'callback' => array($this, 'delete_metadata'),
'permission_callback' => array($this, 'update_item_permissions_check'),
'args' => $this->get_endpoint_args_for_item_schema(\WP_REST_Server::EDITABLE),
'permission_callback' => array($this, 'delete_item_permissions_check'),
'args' => $this->get_endpoint_args_for_item_schema(\WP_REST_Server::DELETABLE),
),
'schema' => [$this, 'get_schema']
'schema' => [$this, 'get_metadata_list_schema']
)
);
}
@ -599,7 +616,7 @@ class REST_Metadata_Sections_Controller extends REST_Controller {
$query_params = array_merge($query_params, parent::get_wp_query_params());
$query_params['name'] = array(
'description' => __('Limits the result set to metadata with a specific name'),
'description' => __('Limits the result set to metadata sections with a specific name'),
'type' => 'string',
);
@ -615,30 +632,65 @@ class REST_Metadata_Sections_Controller extends REST_Controller {
* @throws \Exception
*/
public function get_endpoint_args_for_item_schema( $method = null ) {
$endpoint_args = [];
if($method === \WP_REST_Server::READABLE) {
$endpoint_args = array_merge(
$endpoint_args,
parent::get_wp_query_params()
);
} elseif ($method === \WP_REST_Server::CREATABLE || $method === \WP_REST_Server::EDITABLE) {
$map = $this->metadata_sections_repository->get_map();
$endpoint_args = [
'metadata_section_id' => [
'description' => __( 'Metadata Section ID', 'tainacan' ),
'required' => true,
],
'collection_id' => [
'description' => __( 'Collection ID', 'tainacan' ),
'required' => true,
]
];
foreach ($map as $mapped => $value){
$set_ = 'set_'. $mapped;
switch ( $method ) {
case \WP_REST_Server::READABLE:
$endpoint_args = array_merge(
$endpoint_args,
parent::get_wp_query_params()
);
break;
case \WP_REST_Server::CREATABLE:
case \WP_REST_Server::EDITABLE:
$map = $this->metadata_sections_repository->get_map();
// Show only args that has a method set
if( !method_exists(new Entities\Metadatum(), "$set_") ){
unset($map[$mapped]);
foreach ($map as $mapped => $value){
$set_ = 'set_'. $mapped;
// Show only args that has a method set
if( !method_exists(new Entities\Metadatum(), "$set_") ){
unset($map[$mapped]);
}
}
}
$endpoint_args = array_merge(
$endpoint_args,
$map
);
if ( $method === \WP_REST_Server::CREATABLE )
unset($endpoint_args['metadata_section_id']);
$endpoint_args = $map;
break;
}
return $endpoint_args;
}
function get_metadata_list_schema() {
$metadatum_schema = parent::get_repository_schema($this->metadata_repository);
return [
'$schema' => 'http://json-schema.org/draft-04/schema#',
'title' => 'metadata',
'tags' => [$this->rest_base, 'metadata'],
'type' => 'array',
'items' => array(
'type' => 'object',
'properties' => $metadatum_schema,
)
];
}
function get_schema() {
$schema = [
'$schema' => 'http://json-schema.org/draft-04/schema#',