From 923e410e7b347445814eb532c85a60d3be2f227e Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Thu, 23 Nov 2017 16:44:54 -0300 Subject: [PATCH] Bug 19250: Add /acquisitions/vendors pagination This patch makes the /acquisitions/vendors endpoint paginated and handle the new special params introduced by 19410: _match, _order_by_, _page and _per_page. To do so, the OpenAPI spec is updated with the new parameters, and _to_model is rewritten to avoid autovivification, as required by the extension of the objects.search helper, introduced by bug 19686. --- Koha/REST/V1/Acquisitions/Vendors.pm | 31 +++++++++++++------------- api/v1/swagger/paths/acquisitions_vendors.json | 8 +++++++ 2 files changed, 24 insertions(+), 15 deletions(-) diff --git a/Koha/REST/V1/Acquisitions/Vendors.pm b/Koha/REST/V1/Acquisitions/Vendors.pm index 5f86ac82d4..7c55cf3589 100644 --- a/Koha/REST/V1/Acquisitions/Vendors.pm +++ b/Koha/REST/V1/Acquisitions/Vendors.pm @@ -38,20 +38,14 @@ Controller function that handles listing Koha::Acquisition::Bookseller objects =cut sub list_vendors { - my $c = shift->openapi->valid_input or return; - - my $args = _to_model($c->req->params->to_hash); - my $filter; - for my $filter_param ( keys %$args ) { - $filter->{$filter_param} = { LIKE => $args->{$filter_param} . "%" } - if $args->{$filter_param}; - } + my $c = shift->openapi->valid_input or return; my @vendors; return try { - @vendors = Koha::Acquisition::Booksellers->search($filter); + my $vendors_set = Koha::Acquisition::Booksellers->new; + @vendors = $c->objects->search( $vendors_set, \&_to_model )->as_list; @vendors = map { _to_api($_->TO_JSON) } @vendors; return $c->render( status => 200, openapi => \@vendors ); @@ -221,12 +215,19 @@ attribute names. sub _to_model { my $vendor = shift; - # Rename back - $vendor->{listprice} = delete $vendor->{list_currency}; - $vendor->{invoiceprice} = delete $vendor->{invoice_currency}; - $vendor->{gstreg} = delete $vendor->{gst}; - $vendor->{listincgst} = delete $vendor->{list_includes_gst}; - $vendor->{invoiceincgst} = delete $vendor->{invoice_includes_gst}; + my $mapping = { + list_currency => 'listprice', + invoice_currency => 'invoiceprice', + gst => 'gstreg', + list_includes_gst => 'listincgst', + invoice_includes_gst => 'invoiceincgst' + }; + + foreach my $key ( keys %{ $mapping } ) { + if ( exists $vendor->{ $key } ) { + $vendor->{ $mapping->{$key} } = delete $vendor->{ $key }; + } + } return $vendor; } diff --git a/api/v1/swagger/paths/acquisitions_vendors.json b/api/v1/swagger/paths/acquisitions_vendors.json index 4c86a8de87..64fd8dfe69 100644 --- a/api/v1/swagger/paths/acquisitions_vendors.json +++ b/api/v1/swagger/paths/acquisitions_vendors.json @@ -19,6 +19,14 @@ "description": "Case insensitive search on vendor's account number", "required": false, "type": "string" + }, { + "$ref": "../parameters.json#/match" + }, { + "$ref": "../parameters.json#/order_by" + }, { + "$ref": "../parameters.json#/page" + }, { + "$ref": "../parameters.json#/per_page" }], "responses": { "200": { -- 2.15.0