@@ -, +, @@ - apply patch - run tests: t/db_dependent/api/v1/libraries.t - test API with some API tool or simple curl --- Koha/REST/V1/Library.pm | 42 +++++++++++++++++ api/v1/swagger/definitions.json | 6 +++ api/v1/swagger/definitions/libraries.json | 6 +++ api/v1/swagger/definitions/library.json | 76 +++++++++++++++++++++++++++++++ api/v1/swagger/parameters.json | 3 ++ api/v1/swagger/parameters/library.json | 9 ++++ api/v1/swagger/paths.json | 6 +++ api/v1/swagger/paths/libraries.json | 47 +++++++++++++++++++ t/db_dependent/api/v1/libraries.t | 63 +++++++++++++++++++++++++ 9 files changed, 258 insertions(+) create mode 100644 Koha/REST/V1/Library.pm create mode 100644 api/v1/swagger/definitions/libraries.json create mode 100644 api/v1/swagger/definitions/library.json create mode 100644 api/v1/swagger/parameters/library.json create mode 100644 api/v1/swagger/paths/libraries.json create mode 100644 t/db_dependent/api/v1/libraries.t --- a/Koha/REST/V1/Library.pm +++ a/Koha/REST/V1/Library.pm @@ -0,0 +1,42 @@ +package Koha::REST::V1::Library; + +# This file is part of Koha. +# +# Koha is free software; you can redistribute it and/or modify it under the +# terms of the GNU General Public License as published by the Free Software +# Foundation; either version 3 of the License, or (at your option) any later +# version. +# +# Koha is distributed in the hope that it will be useful, but WITHOUT ANY +# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR +# A PARTICULAR PURPOSE. See the GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with Koha; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +use Modern::Perl; + +use Mojo::Base 'Mojolicious::Controller'; +use Koha::Libraries; + +sub list { + my ($c, $args, $cb) = @_; + + my $libraries = Koha::Libraries->search; + return $c->$cb($libraries->unblessed, 200); +} + +sub get { + my ($c, $args, $cb) = @_; + + my $branchcode = $c->param('branchcode'); + my $library = Koha::Libraries->find({branchcode => $branchcode}); + unless ($library) { + return $c->$cb({error => "Library with branchcode \"$branchcode\" not found"}, 404); + } + + return $c->$cb($library->unblessed, 200); +} + +1; --- a/api/v1/swagger/definitions.json +++ a/api/v1/swagger/definitions.json @@ -8,6 +8,12 @@ "hold": { "$ref": "definitions/hold.json" }, + "libraries": { + "$ref": "definitions/libraries.json" + }, + "library": { + "$ref": "definitions/library.json" + }, "error": { "$ref": "definitions/error.json" } --- a/api/v1/swagger/definitions/libraries.json +++ a/api/v1/swagger/definitions/libraries.json @@ -0,0 +1,6 @@ +{ + "type": "array", + "items": { + "$ref": "library.json" + } +} --- a/api/v1/swagger/definitions/library.json +++ a/api/v1/swagger/definitions/library.json @@ -0,0 +1,76 @@ +{ + "type": "object", + "properties": { + "branchcode": { + "$ref": "../x-primitives.json#/branchcode" + }, + "branchname": { + "type": "string", + "description": "Printable name of library" + }, + "branchaddress1": { + "type": [ "string", "null" ], + "description": "the first address line of the library" + }, + "branchaddress2": { + "type": [ "string", "null" ], + "description": "the second address line of the library" + }, + "branchaddress3": { + "type": [ "string", "null" ], + "description": "the third address line of the library" + }, + "branchzip": { + "type": [ "string", "null" ], + "description": "the zip or postal code of the library" + }, + "branchcity": { + "type": [ "string", "null" ], + "description": "the city or province of the library" + }, + "branchstate": { + "type": [ "string", "null" ], + "description": "the reqional state of the library" + }, + "branchcountry": { + "type": [ "string", "null" ], + "description": "the county of the library" + }, + "branchphone": { + "type": [ "string", "null" ], + "description": "the primary phone of the library" + }, + "branchfax": { + "type": [ "string", "null" ], + "description": "the fax number of the library" + }, + "branchemail": { + "type": [ "string", "null" ], + "description": "the primary email address of the library" + }, + "branchreplyto": { + "type": [ "string", "null" ], + "description": "the email to be used as a Reply-To" + }, + "branchreturnpath": { + "type": [ "string", "null" ], + "description": "the email to be used as Return-Path" + }, + "branchurl": { + "type": [ "string", "null" ], + "description": "the URL for your library or branch's website" + }, + "branchip": { + "type": [ "string", "null" ], + "description": "the IP address for your library or branch" + }, + "branchnotes": { + "type": [ "string", "null" ], + "description": "notes related to your library or branch" + }, + "opac_info": { + "type": [ "string", "null" ], + "description": "HTML that displays in OPAC" + } + } +} --- a/api/v1/swagger/parameters.json +++ a/api/v1/swagger/parameters.json @@ -7,5 +7,8 @@ }, "holdIdPathParam": { "$ref": "parameters/hold.json#/holdIdPathParam" + }, + "branchcodePathParam": { + "$ref": "parameters/library.json#/branchcodePathParam" } } --- a/api/v1/swagger/parameters/library.json +++ a/api/v1/swagger/parameters/library.json @@ -0,0 +1,9 @@ +{ + "branchcodePathParam": { + "name": "branchcode", + "in": "path", + "description": "Branch identifier code", + "required": true, + "type": "string" + } +} --- a/api/v1/swagger/paths.json +++ a/api/v1/swagger/paths.json @@ -10,5 +10,11 @@ }, "/patrons/{borrowernumber}": { "$ref": "paths/patrons.json#/~1patrons~1{borrowernumber}" + }, + "/libraries": { + "$ref": "paths/libraries.json#/~1libraries" + }, + "/libraries/{branchcode}": { + "$ref": "paths/libraries.json#/~1libraries~1{branchcode}" } } --- a/api/v1/swagger/paths/libraries.json +++ a/api/v1/swagger/paths/libraries.json @@ -0,0 +1,47 @@ +{ + "/libraries": { + "get": { + "operationId": "listLibrary", + "tags": ["libraries"], + "produces": [ + "application/json" + ], + "responses": { + "200": { + "description": "A list of libraries", + "schema": { + "$ref": "../definitions.json#/libraries" + } + } + } + } + }, + "/libraries/{branchcode}": { + "get": { + "operationId": "getLibrary", + "tags": ["libraries"], + "parameters": [ + { + "$ref": "../parameters.json#/branchcodePathParam" + } + ], + "produces": [ + "application/json" + ], + "responses": { + "200": { + "description": "A library", + "schema": { + "$ref": "../definitions.json#/library" + } + }, + "404": { + "description": "Library not found", + "schema": { + "$ref": "../definitions.json#/error" + } + } + } + } + } +} --- a/t/db_dependent/api/v1/libraries.t +++ a/t/db_dependent/api/v1/libraries.t @@ -0,0 +1,63 @@ +#!/usr/bin/env perl + +# This file is part of Koha. +# +# Koha is free software; you can redistribute it and/or modify it under the +# terms of the GNU General Public License as published by the Free Software +# Foundation; either version 3 of the License, or (at your option) any later +# version. +# +# Koha is distributed in the hope that it will be useful, but WITHOUT ANY +# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR +# A PARTICULAR PURPOSE. See the GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with Koha; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +use Modern::Perl; + +use t::lib::TestBuilder; + +use Test::More tests => 11; +use Test::Mojo; + +use C4::Auth; +use C4::Context; +use Koha::Database; + +BEGIN { + use_ok('Koha::Object'); + use_ok('Koha::Libraries'); +} + +my $schema = Koha::Database->schema; +my $dbh = C4::Context->dbh; +my $builder = t::lib::TestBuilder->new; + +$ENV{REMOTE_ADDR} = '127.0.0.1'; +my $t = Test::Mojo->new('Koha::REST::V1'); + +$schema->storage->txn_begin; + +my $branch = $builder->build({ source => 'Branch' }); + +my $tx = $t->ua->build_tx(GET => '/api/v1/libraries'); +#$tx->req->cookies({name => 'CGISESSID', value => $session->id}); +$tx->req->env({REMOTE_ADDR => '127.0.0.1'}); +$t->request_ok($tx) + ->status_is(200); + +$tx = $t->ua->build_tx(GET => "/api/v1/libraries/" . $branch->{ branchcode }); +#$tx->req->cookies({name => 'CGISESSID', value => $session->id}); +$t->request_ok($tx) + ->status_is(200) + ->json_is('/branchcode' => $branch->{ branchcode }) + ->json_is('/branchname' => $branch->{ branchname }); + +$tx = $t->ua->build_tx(GET => "/api/v1/libraries/" . "nonexistent"); +$t->request_ok($tx) + ->status_is(404) + ->json_is('/error' => "Library with branchcode \"nonexistent\" not found"); + +$schema->storage->txn_rollback; --