[Koha-patches] [PATCH] bug_7520 Added Meta handler to API v1 for metadata lists
Srdjan
srdjan at catalyst.net.nz
Tue Aug 2 03:45:01 CEST 2016
Useful for building drop-downs etc.
Kick off with /meta/advanced-search-types
---
Koha/REST/V1/Meta.pm | 52 ++++++++++++++++++++++++++++
api/v1/swagger.json | 49 ++++++++++++++++++++++++++
t/db_dependent/api/v1/meta.t | 82 ++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 183 insertions(+)
create mode 100644 Koha/REST/V1/Meta.pm
create mode 100644 t/db_dependent/api/v1/meta.t
diff --git a/Koha/REST/V1/Meta.pm b/Koha/REST/V1/Meta.pm
new file mode 100644
index 0000000..2ad54d8
--- /dev/null
+++ b/Koha/REST/V1/Meta.pm
@@ -0,0 +1,52 @@
+package Koha::REST::V1::Meta;
+
+# 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::ItemTypes;
+use C4::Koha;
+
+sub advanced_search_types {
+ my ($self, $args, $cb) = @_;
+
+ my $advanced_search_types = $self->syspref("AdvancedSearchTypes");
+ my ($ccl, @typesloop);
+ if (!$advanced_search_types or $advanced_search_types eq 'itemtypes') {
+ my $itemtypes = Koha::ItemTypes->unblessed;
+ my $itype_or_itemtype = ($self->syspref("item-level_itypes"))?'itype':'itemtype';
+ @typesloop = map {
+ code => $_->{itemtype},
+ description => $_->{description},
+ imageurl => getitemtypeimagelocation( 'opac', $_->{imageurl} ),
+ }, sort {$a->{description} cmp $b->{description} } @$itemtypes;
+ $ccl = "$itype_or_itemtype,phr";
+ } else {
+ my $advsearchtypes = GetAuthorisedValues($advanced_search_types, 'OPAC');
+ @typesloop = map {
+ code => $_->{authorised_value},
+ description => $_->{lib},
+ imageurl => getitemtypeimagelocation( 'opac', $_->{imageurl} ),
+ }, @$advsearchtypes;
+ $ccl = $advanced_search_types;
+ }
+
+ return $self->$cb({ ccl => $ccl, types => \@typesloop }, 200);
+}
+
+1;
diff --git a/api/v1/swagger.json b/api/v1/swagger.json
index b3f30f8..94933a3 100644
--- a/api/v1/swagger.json
+++ b/api/v1/swagger.json
@@ -332,6 +332,55 @@
}
}
}
+ },
+ "/meta/advanced-search-types": {
+ "get": {
+ "operationId": "advanced_search_typesMeta",
+ "tags": ["meta"],
+ "produces": [
+ "application/json"
+ ],
+ "responses": {
+ "200": {
+ "description": "Advanced search types list",
+ "schema": {
+ "type": "object",
+ "required": [
+ "ccl",
+ "types"
+ ],
+ "properties": {
+ "ccl": {
+ "type": "string"
+ },
+ "types": {
+ "type": "array",
+ "items": {
+ "type": "object",
+ "required": [
+ "code",
+ "description",
+ "imageurl"
+ ],
+ "properties": {
+ "code": {
+ "type": "string"
+ },
+ "description": {
+ "type": "string"
+ },
+ "imageurl": {
+ "type": "string",
+ "format": "uri"
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+ }
}
},
"definitions": {
diff --git a/t/db_dependent/api/v1/meta.t b/t/db_dependent/api/v1/meta.t
new file mode 100644
index 0000000..7c8f905
--- /dev/null
+++ b/t/db_dependent/api/v1/meta.t
@@ -0,0 +1,82 @@
+#!/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 Test::More;
+use Test::Mojo;
+use t::lib::TestBuilder;
+use t::lib::Mocks;
+
+use C4::Auth;
+use C4::Context;
+
+use Koha::Database;
+use Koha::Patron;
+use Koha::AuthorisedValues;
+
+my $builder = t::lib::TestBuilder->new();
+
+my $dbh = C4::Context->dbh;
+$dbh->{AutoCommit} = 0;
+$dbh->{RaiseError} = 1;
+
+my $t = Test::Mojo->new('Koha::REST::V1');
+
+my $categorycode = Koha::Database->new()->schema()->resultset('Category')->first()->categorycode();
+my $branchcode = Koha::Database->new()->schema()->resultset('Branch')->first()->branchcode();
+
+my $borrower = $builder->build({ source => 'Borrower' });
+
+my $session = C4::Auth::get_session('');
+$session->param('number', $borrower->{borrowernumber});
+$session->param('id', $borrower->{userid});
+$session->param('ip', '127.0.0.1');
+$session->param('lasttime', time());
+$session->flush;
+
+my $advanced_search_types = C4::Context->preference("AdvancedSearchTypes");
+
+my %cat = map { $_ => 1 } Koha::AuthorisedValues->categories;
+foreach (qw/CCODE LOC/) {
+ next unless $cat{$_};
+
+ t::lib::Mocks::mock_preference('AdvancedSearchTypes', $_);
+ my $tx = $t->ua->build_tx(GET => '/api/v1/meta/advanced-search-types');
+ $tx->req->cookies({name => 'CGISESSID', value => $session->id});
+ $tx->req->env({REMOTE_ADDR => '127.0.0.1'});
+ $t->request_ok($tx)
+ ->status_is(200)
+ ->json_is('/ccl' => $_)
+ ->json_has('/types/0/code')
+ ->json_has('/types/0/description')
+ ->json_has('/types/0/imageurl');
+}
+
+t::lib::Mocks::mock_preference('AdvancedSearchTypes', 'itemtypes');
+my $tx = $t->ua->build_tx(GET => '/api/v1/meta/advanced-search-types');
+$tx->req->cookies({name => 'CGISESSID', value => $session->id});
+$tx->req->env({REMOTE_ADDR => '127.0.0.1'});
+$t->request_ok($tx)
+ ->status_is(200)
+ ->json_like('/ccl' => qr/\w+/)
+ ->json_has('/types/0/code')
+ ->json_has('/types/0/description')
+ ->json_has('/types/0/imageurl');
+
+$dbh->rollback;
+done_testing();
--
2.7.4
More information about the Koha-patches
mailing list