@@ -, +, @@ - 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/definitions/index.json | 2 + api/v1/definitions/libraries.json | 4 ++ api/v1/definitions/library.json | 77 +++++++++++++++++++++++++++++++++++++++ api/v1/swagger.json | 50 +++++++++++++++++++++++++ t/db_dependent/api/v1/libraries.t | 63 ++++++++++++++++++++++++++++++++ 6 files changed, 238 insertions(+) create mode 100644 Koha/REST/V1/Library.pm create mode 100644 api/v1/definitions/libraries.json create mode 100644 api/v1/definitions/library.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 $brancocode = $c->param('branchcode'); + my $library = Koha::Libraries->find({branchcode => $brancocode}); + unless ($library) { + return $c->$cb({error => "Library with branchcode \"$brancocode\" not found"}, 404); + } + + return $c->$cb($library->unblessed, 200); +} + +1; --- a/api/v1/definitions/index.json +++ a/api/v1/definitions/index.json @@ -2,5 +2,7 @@ "patron": { "$ref": "patron.json" }, "holds": { "$ref": "holds.json" }, "hold": { "$ref": "hold.json" }, + "libraries": { "$ref": "libraries.json" }, + "library": { "$ref": "library.json" }, "error": { "$ref": "error.json" } } --- a/api/v1/definitions/libraries.json +++ a/api/v1/definitions/libraries.json @@ -0,0 +1,4 @@ +{ + "type": "array", + "items": { "$ref": "library.json" } +} --- a/api/v1/definitions/library.json +++ a/api/v1/definitions/library.json @@ -0,0 +1,77 @@ +{ + "type": "object", + "properties": { + "branchcode": { + "type": "string", + "description": "Internal library identifier" + }, + "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.json +++ a/api/v1/swagger.json @@ -332,6 +332,49 @@ } } } + }, + "/libraries": { + "get": { + "operationId": "listLibrary", + "tags": ["libraries"], + "produces": [ + "application/json" + ], + "responses": { + "200": { + "description": "A list of libraries", + "schema": { + "$ref": "#/definitions/libraries" + } + } + } + } + }, + "/libraries/{branchcode}": { + "get": { + "operationId": "getLibrary", + "tags": ["libraries"], + "parameters": [ + { "$ref": "#/parameters/branchcodePathParam" } + ], + "produces": [ + "application/json" + ], + "responses": { + "200": { + "description": "A library", + "schema": { + "$ref": "#/definitions/library" + } + }, + "404": { + "description": "Library not found", + "schema": { + "$ref": "#/definitions/error" + } + } + } + } } }, "definitions": { @@ -351,6 +394,13 @@ "description": "Internal hold identifier", "required": true, "type": "integer" + }, + "branchcodePathParam": { + "name": "branchcode", + "in": "path", + "description": "Internal library identifier", + "required": true, + "type": "string" } } } --- 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; --