The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Yancy::Controller::Yancy::API - An OpenAPI REST controller for the Yancy editor

VERSION

version 1.024

DESCRIPTION

This module contains the routes that Yancy uses to work with the backend data. This API is used by the Yancy editor.

METHODS

list_items

List the items in a collection. The collection name should be in the stash key collection.

Each returned item will be filtered by filters conforming with "yancy.filter.add" in Mojolicious::Plugin::Yancy that are passed in the array-ref in stash key filters_out.

$limit, $offset, and $order_by may be provided as query parameters.

add_item

Add a new item to the collection. The collection name should be in the stash key collection.

The new item is extracted from the OpenAPI input, under parameter name newItem, and must be a hash/JSON "object". It will be filtered by filters conforming with "yancy.filter.add" in Mojolicious::Plugin::Yancy that are passed in the array-ref in stash key filters, after the collection and property filters have been applied.

The return value is filtered like each result is in "list_items".

get_item

Get a single item from a collection. The collection should be in the stash key collection.

The item's ID field-name is in the stash key id_field. The ID itself is extracted from the OpenAPI input, under a parameter of that name.

The return value is filtered like each result is in "list_items".

set_item

Update an item in a collection. The collection should be in the stash key collection.

The item to be updated is determined as with "get_item", and what to update it with is determined as with "add_item".

The return value is filtered like each result is in "list_items".

delete_item

Delete an item from a collection. The collection name should be in the stash key collection.

The item to be deleted is determined as with "get_item".

SUBCLASSING

To change how the API provides access to the data in your database, you can create a custom controller. To do so, you should extend this class and override the desired methods to provide the desired functionality.

    package MyApp::Controller::CustomYancyAPI;
    use Mojo::Base 'Yancy::Controller::Yancy::API';
    sub list_items {
        my ( $c ) = @_;
        return unless $c->openapi->valid_input;
        my $items = $c->yancy->backend->list( $c->stash( 'collection' ) );
        return $c->render(
            status => 200,
            openapi => $items,
        );
    }

    package main;
    use Mojolicious::Lite;
    push @{ app->routes->namespaces }, 'MyApp::Controller';
    plugin Yancy => {
        api_controller => 'CustomYancyAPI',
    };

For an example, you could extend this class to add authorization based on your own requirements.

SEE ALSO

Yancy, Mojolicious::Controller

AUTHOR

Doug Bell <preaction@cpan.org>

COPYRIGHT AND LICENSE

This software is copyright (c) 2018 by Doug Bell.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.