From f23a6a67bcc8364d7d97a7d2dff83186887e8c75 Mon Sep 17 00:00:00 2001 From: Lari Taskula Date: Mon, 19 Dec 2016 14:11:45 +0200 Subject: [PATCH] Bug 16826: Swaggerize Koha::Availability objects Koha::Biblio::Availability->swaggerize Koha::Item::Availability->swaggerize Constructs a HASHref that contains all availability data to be returned in a JSON object. Numifies numbers to be numbers instead of strings. E.g. biblio swaggerize { "biblionumber": 1234, "availability": { "available": true, "notes": { "Patron::SomethingToNote": { "cool": 1 } } }, "item_availabilities": [ { "itemnumber": 5678, "availability": { "available": false, "unavailabilities": { "Item::Withdrawn": {} } } } ] } --- Koha/Availability.pm | 16 ++++++++++++ Koha/Biblio/Availability.pm | 46 ++++++++++++++++++++++++++++++++++ Koha/Item/Availability.pm | 45 +++++++++++++++++++++++++++++++++ api/v1/swagger/paths/availability.json | 9 ++++++- 4 files changed, 115 insertions(+), 1 deletion(-) diff --git a/Koha/Availability.pm b/Koha/Availability.pm index a2e72a2..f9008ce 100644 --- a/Koha/Availability.pm +++ b/Koha/Availability.pm @@ -213,4 +213,20 @@ sub unavailable { } } + +sub _swaggerize_exception { + my ($self, $exceptions) = @_; + + my $ret = {}; + foreach my $ex (keys %{$exceptions}) { + my $name = $ex; + $name =~ s/Koha::Exceptions:://; + $ret->{$name} = {}; + foreach my $field ($exceptions->{$ex}->Fields) { + $ret->{$name}->{$field} = $exceptions->{$ex}->$field; + } + } + return $ret; +} + 1; diff --git a/Koha/Biblio/Availability.pm b/Koha/Biblio/Availability.pm index 654457b..8cc875f 100644 --- a/Koha/Biblio/Availability.pm +++ b/Koha/Biblio/Availability.pm @@ -19,6 +19,7 @@ package Koha::Biblio::Availability; use Modern::Perl; use Scalar::Util qw(looks_like_number); +use Mojo::JSON; use base qw(Koha::Availability); @@ -129,4 +130,49 @@ sub new { return $self; } +=head3 swaggerize + +Returns a HASHref that contains biblio availability information as well as +availability information for each item of this biblio. + +Numifies numbers for Swagger to be numbers instead of strings. + +=cut + +sub swaggerize { + my ($self) = @_; + + my $item_availabilities = []; + foreach my $item_availability (@{$self->item_availabilities}) { + push $item_availabilities, $item_availability->swaggerize; + } + foreach my $item_availability (@{$self->item_unavailabilities}) { + push $item_availabilities, $item_availability->swaggerize; + } + my $confirmations = $self->SUPER::_swaggerize_exception($self->confirmations); + my $notes = $self->SUPER::_swaggerize_exception($self->notes); + my $unavailabilities = $self->SUPER::_swaggerize_exception($self->unavailabilities); + my $availability = { + available => $self->available + ? Mojo::JSON->true + : Mojo::JSON->false, + }; + if (keys %{$confirmations} > 0) { + $availability->{'confirmations'} = $confirmations; + } + if (keys %{$notes} > 0) { + $availability->{'notes'} = $notes; + } + if (keys %{$unavailabilities} > 0) { + $availability->{'unavailabilities'} = $unavailabilities; + } + + my $hash = { + biblionumber => 0+$self->biblio->biblionumber, + availability => $availability, + item_availabilities => $item_availabilities, + }; + return $hash; +} + 1; diff --git a/Koha/Item/Availability.pm b/Koha/Item/Availability.pm index c26d79b..7b4ef2d 100644 --- a/Koha/Item/Availability.pm +++ b/Koha/Item/Availability.pm @@ -123,4 +123,49 @@ sub new { return $self; } +=head3 swaggerize + +Returns a HASHref that contains item availability information. + +Numifies numbers for Swagger to be numbers instead of strings. + +=cut + +sub swaggerize { + my ($self) = @_; + + my $confirmations = $self->SUPER::_swaggerize_exception($self->confirmations); + my $notes = $self->SUPER::_swaggerize_exception($self->notes); + my $unavailabilities = $self->SUPER::_swaggerize_exception($self->unavailabilities); + my $item = $self->item; + my $availability = { + available => $self->available + ? Mojo::JSON->true + : Mojo::JSON->false, + }; + if (keys %{$confirmations} > 0) { + $availability->{'confirmations'} = $confirmations; + } + if (keys %{$notes} > 0) { + $availability->{'notes'} = $notes; + } + if (keys %{$unavailabilities} > 0) { + $availability->{'unavailabilities'} = $unavailabilities; + } + + return { + itemnumber => 0+$item->itemnumber, + biblionumber => 0+$item->biblionumber, + biblioitemnumber => 0+$item->biblioitemnumber, + availability => $availability, + barcode => $item->barcode, + enumchron => $item->enumchron, + holdingbranch => $item->holdingbranch, + homebranch => $item->homebranch, + itemcallnumber => $item->itemcallnumber, + itemnotes => $item->itemnotes, + location => $item->location, + }; +} + 1; diff --git a/api/v1/swagger/paths/availability.json b/api/v1/swagger/paths/availability.json index d7ab4c6..2dbe03e 100644 --- a/api/v1/swagger/paths/availability.json +++ b/api/v1/swagger/paths/availability.json @@ -6,7 +6,14 @@ "parameters": [ { "$ref": "../parameters.json#/biblionumbersQueryParam" }, { "$ref": "../parameters.json#/borrowernumberQueryParam" }, - { "$ref": "../parameters.json#/branchcodeQueryParam"} + { "$ref": "../parameters.json#/branchcodeQueryParam"}, + { + "name": "limit_items", + "in": "query", + "description": "Check only first n available items.", + "required": false, + "type": "integer" + } ], "consumes": ["application/json"], "produces": ["application/json"], -- 1.9.1