Skip to content

Bug 38050 - /lists - #136

Open
mrenvoize wants to merge 6 commits into
mainfrom
bug_38050
Open

Bug 38050 - /lists#136
mrenvoize wants to merge 6 commits into
mainfrom
bug_38050

Conversation

@mrenvoize

Copy link
Copy Markdown
Member

No description provided.

@mrenvoize mrenvoize left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We'll also need unit tests.. examples in t/db_dependent/api

Comment thread Koha/REST/V1/Lists.pm Outdated

=cut

sub list {

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This whole routine is rather custom without any real need for it to be.. please follow the patterns set in all the other API controllers... i.e. using the API helpers so we get all the search and query handling from there.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You'll want to follow Tomas's example to some extent here and use:

my $lists_set = Koha::Virtualshelves->new;
$lists_set = $lists_set->filter_by_readable( { patron_id => $user->id } );

return $c->render(
    status  => 200,
    openapi => $c->objects->search($lists_set),
);

He's also got error handling in a wrapper.. you'll likely want that too for various failure cases the object searches can throw.

Comment thread Koha/REST/V1/Lists.pm Outdated
return $c->render(status => 201, json => { id => $list->id });
}

=head3 read

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please call this method 'get'.. 'read' is a homonym for a Perl builtin so can sometimes be confused... also, that would make this consistent with other controllers.

Comment thread Koha/REST/V1/Lists.pm Outdated
return $c->render(json => \@lists);
}

=head3 create

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please call this 'add' rather than 'create' for consistency with other API controllers.

Comment thread Koha/REST/V1/Lists.pm Outdated

=cut

sub create {

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is another case where you've not used the foundations that have been laid out before you.

See the 'Cities' controller for reference as it's generally the most basic 'best practices' example... I should have pointed you to that.. assuming I didn't already.

sub add {
    my $c = shift->openapi->valid_input or return;

    return try {
        my $city = Koha::City->new_from_api( $c->req->json );
        $city->store;
        $c->res->headers->location( $c->req->url->to_string . '/' . $city->cityid );
        return $c->render(
            status  => 201,
            openapi => $c->objects->to_api($city),
        );
    }
    catch {
        $c->unhandled_exception($_);
    };
}

Comment thread Koha/REST/V1/Lists.pm Outdated
}
}

return $c->render(status => 201, json => { id => $list->id });

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As above.. by using 'json' as your output directly, you're skipping all validation... please use the 'openapi' output handler as that include validation against the specification files.

Comment thread Koha/REST/V1/Lists.pm Outdated

=cut

sub read {

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a pattern for this one too:

sub get {
    my $c = shift->openapi->valid_input or return;

    return try {
        my $city = Koha::Cities->find( $c->param('city_id') );
        return $c->render_resource_not_found("City")
            unless $city;

        return $c->render( status => 200, openapi => $c->objects->to_api($city), );
    } catch {
        $c->unhandled_exception($_);
    };
}

I think you'll likely need to use the above mentioned filter_by_readable method however to ensure only those with appropriate access permissions can 'get' the specific list.

Comment thread Koha/REST/V1/Lists.pm

=cut

sub update {

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's another template for this:

sub update {
    my $c = shift->openapi->valid_input or return;

    my $city = Koha::Cities->find( $c->param('city_id') );

    return $c->render_resource_not_found("City")
        unless $city;

    return try {
        $city->set_from_api( $c->req->json );
        $city->store();
        return $c->render( status => 200, openapi => $c->objects->to_api($city), );
    }
    catch {
        $c->unhandled_exception($_);
    };
}

We'll want to think about how we handle adding and removing biblios to/from lists.. lets keep the basic crud at the list level only however.

Comment thread Koha/REST/V1/Lists.pm

=cut

sub delete {

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again.. there's a template:

sub delete {
    my $c = shift->openapi->valid_input or return;

    my $city = Koha::Cities->find( $c->param('city_id') );

    return $c->render_resource_not_found("City")
        unless $city;

    return try {
        $city->delete;
        return $c->render_resource_deleted;
    }
    catch {
        $c->unhandled_exception($_);
    };
}

This patch creates the yaml definitions for the CRUD enpoints for virtualshelves/lists
This patch implements the methods for the admin CRUD endpoints for lists/virtualshelves
This patch creates the yaml definitions for the public CRUD enpoints for lists/virtualshelves
This patch creates the methods for the public CRUD enpoints for lists/virtualshelves
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants