From 975be0aef051d1131c39a02cbf7b6efe2f67d113 Mon Sep 17 00:00:00 2001 From: Julian Maurice Date: Fri, 15 Apr 2016 10:49:29 +0200 Subject: [PATCH] Bug 16271: Allow more filters on /api/v1/holds borrowernumber parameter is no longer required and you can filter on every column of reserves table Some example requests - GET /api/v1/holds - GET /api/v1/holds?biblionumber=123 - GET /api/v1/holds?borrowernumber=456 - GET /api/v1/holds?priority=0&found=W Signed-off-by: Benjamin Rokseth Signed-off-by: Kyle M Hall Signed-off-by: Kyle M Hall --- Koha/REST/V1/Hold.pm | 14 ++--- api/v1/swagger.json | 103 +++++++++++++++++++++++++++++++++- t/db_dependent/api/v1/holds.t | 19 ++++++- 3 files changed, 125 insertions(+), 11 deletions(-) diff --git a/Koha/REST/V1/Hold.pm b/Koha/REST/V1/Hold.pm index 208488d2cb..7f0b1ff269 100644 --- a/Koha/REST/V1/Hold.pm +++ b/Koha/REST/V1/Hold.pm @@ -23,20 +23,20 @@ use C4::Biblio; use C4::Reserves; use Koha::Patrons; +use Koha::Holds; use Koha::DateUtils; sub list { my ($c, $args, $cb) = @_; - my $borrowernumber = $c->param('borrowernumber'); - my $borrower = Koha::Patrons->find($borrowernumber); - unless ($borrower) { - return $c->$cb({error => "Borrower not found"}, 404); + my $params = $c->req->query_params->to_hash; + my @valid_params = Koha::Holds->_resultset->result_source->columns; + foreach my $key (keys %$params) { + delete $params->{$key} unless grep { $key eq $_ } @valid_params; } + my $holds = Koha::Holds->search($params)->unblessed; - my @reserves = C4::Reserves::GetReservesFromBorrowernumber($borrowernumber); - - return $c->$cb(\@reserves, 200); + return $c->$cb($holds, 200); } sub add { diff --git a/api/v1/swagger.json b/api/v1/swagger.json index 1488aba17a..b3f30f8d60 100644 --- a/api/v1/swagger.json +++ b/api/v1/swagger.json @@ -79,12 +79,113 @@ "operationId": "listHolds", "tags": ["borrowers", "holds"], "parameters": [ + { + "name": "reserve_id", + "in": "query", + "description": "Internal reserve identifier", + "type": "integer" + }, { "name": "borrowernumber", "in": "query", "description": "Internal borrower identifier", - "required": true, "type": "integer" + }, + { + "name": "reservedate", + "in": "query", + "description": "Reserve date", + "type": "string" + }, + { + "name": "biblionumber", + "in": "query", + "description": "Internal biblio identifier", + "type": "integer" + }, + { + "name": "branchcode", + "in": "query", + "description": "Branch code", + "type": "string" + }, + { + "name": "notificationdate", + "in": "query", + "description": "Notification date", + "type": "string" + }, + { + "name": "reminderdate", + "in": "query", + "description": "Reminder date", + "type": "string" + }, + { + "name": "cancellationdate", + "in": "query", + "description": "Cancellation date", + "type": "string" + }, + { + "name": "reservenotes", + "in": "query", + "description": "Reserve notes", + "type": "string" + }, + { + "name": "priority", + "in": "query", + "description": "Priority", + "type": "integer" + }, + { + "name": "found", + "in": "query", + "description": "Found status", + "type": "string" + }, + { + "name": "timestamp", + "in": "query", + "description": "Time of latest update", + "type": "string" + }, + { + "name": "itemnumber", + "in": "query", + "description": "Internal item identifier", + "type": "integer" + }, + { + "name": "waitingdate", + "in": "query", + "description": "Date the item was marked as waiting for the patron", + "type": "string" + }, + { + "name": "expirationdate", + "in": "query", + "description": "Date the hold expires", + "type": "string" + }, + { + "name": "lowestPriority", + "in": "query", + "description": "Lowest priority", + "type": "integer" + }, + { + "name": "suspend", + "in": "query", + "description": "Suspended", + "type": "integer" + }, + { + "name": "suspend_until", + "in": "query", + "description": "Suspended until", + "type": "string" } ], "produces": ["application/json"], diff --git a/t/db_dependent/api/v1/holds.t b/t/db_dependent/api/v1/holds.t index 0ce67d5b48..52177bf3bb 100644 --- a/t/db_dependent/api/v1/holds.t +++ b/t/db_dependent/api/v1/holds.t @@ -17,7 +17,7 @@ use Modern::Perl; -use Test::More tests => 30; +use Test::More tests => 39; use Test::Mojo; use DateTime; @@ -56,6 +56,8 @@ my $borrowernumber2 = $borrower2->borrowernumber; my $biblionumber = create_biblio('RESTful Web APIs'); my $itemnumber = create_item($biblionumber, 'TEST000001'); +$dbh->do('DELETE FROM reserves'); + my $reserve_id = C4::Reserves::AddReserve($branchcode, $borrowernumber, $biblionumber, undef, 1, undef, undef, undef, '', $itemnumber); @@ -63,6 +65,17 @@ my $reserve_id = C4::Reserves::AddReserve($branchcode, $borrowernumber, C4::Reserves::AddReserve($branchcode, $borrowernumber2, $biblionumber, undef, 2, undef, undef, undef, '', $itemnumber); +$t->get_ok('/api/v1/holds') + ->status_is(200) + ->json_has('/0') + ->json_has('/1') + ->json_hasnt('/2'); + +$t->get_ok('/api/v1/holds?priority=2') + ->status_is(200) + ->json_is('/0/borrowernumber', $borrowernumber2) + ->json_hasnt('/1'); + my $suspend_until = DateTime->now->add(days => 10)->ymd; my $put_data = { priority => 2, @@ -92,8 +105,8 @@ $t->get_ok("/api/v1/holds?borrowernumber=$borrowernumber") my $inexisting_borrowernumber = $borrowernumber2 + 1; $t->get_ok("/api/v1/holds?borrowernumber=$inexisting_borrowernumber") - ->status_is(404) - ->json_has('/error'); + ->status_is(200) + ->json_is([]); $dbh->do('DELETE FROM issuingrules'); $dbh->do(q{ -- 2.39.5