Bug 22823: Add unit tests for get_effective_email
[koha.git] / opac / svc / recordedbooks
blobd55a15c75917ba76a97cbef81b0a02d59468fbc9
1 #!/usr/bin/perl
3 # Copyright 2016 Catalyst IT
4 # This file is part of Koha.
6 # Koha is free software; you can redistribute it and/or modify it
7 # under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 3 of the License, or
9 # (at your option) any later version.
11 # Koha is distributed in the hope that it will be useful, but
12 # WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with Koha; if not, see <http://www.gnu.org/licenses>.
19 use Modern::Perl;
20 use CGI qw ( -utf8 );
21 use JSON qw(encode_json);
22 use C4::Auth qw(checkauth);
23 use C4::Output;
24 use Koha::Logger;
25 use Koha::ExternalContent::RecordedBooks;
27 our $cgi = new CGI;
28 my $logger = Koha::Logger->get({ interface => 'opac' });
29 my $page_url = $cgi->referer();
31 my ( $user, $cookie, $sessionID, $flags ) = checkauth( $cgi, 1, {}, 'opac' );
33 my $action = $cgi->param('action') or response_bad_request("No 'action' specified");
35 ($user && $sessionID) || $action eq 'search' or response_bad_request("User not logged in");
37 local $@;
38 my $rb = eval { Koha::ExternalContent::RecordedBooks->new({ koha_session_id => $sessionID }) };
39 unless ($rb) {
40 $logger->error($@) if $@;
41 response({
42 error => $@,
43 is_identified => JSON::false,
44 });
47 my $is_identified = $rb->is_identified;
48 my %data = (
49 is_identified => $is_identified ? JSON::true : JSON::false,
51 response(\%data) unless $is_identified || $action eq 'search';
53 eval {
55 $action eq 'search' && do {
56 my $query = $cgi->param('q');
57 my $page = $cgi->param('page');
58 my $page_size = $cgi->param('page_size');
59 my $sort = $cgi->param('sort');
60 my $res = $rb->search({
61 query => $query,
62 page => $page,
63 page_size => $page_size,
64 sort => $sort,
65 });
66 $data{results} = $res;
67 last;
70 $action eq 'account' && do {
71 eval {
72 $data{account} = $rb->patron;
73 $data{checkouts} = $rb->checkouts;
74 $data{holds} = $rb->holds;
76 response_bad_request($rb->error_message($@)) if $@;
77 last;
80 $action eq 'checkout' && do {
81 my $isbn = $cgi->param('isbn')
82 or response_bad_request("No 'isbn' specified");
83 my $format = $cgi->param('format');
84 $data{action} = eval { $rb->checkout($isbn, $format) };
85 response_bad_request($rb->error_message($@)) if $@;
87 eval {
88 $data{checkouts} = $rb->checkouts;
89 $data{holds} = $rb->holds;
91 $data{error} = $rb->error_message($@) if $@;
92 last;
95 $action eq 'return' && do {
96 my $isbn = $cgi->param('isbn')
97 or response_bad_request("No 'isbn' specified");
98 $data{action} = eval { $rb->return($isbn) };
99 response_bad_request($rb->error_message($@)) if $@;
101 $data{checkouts} = eval { $rb->checkouts };
102 $data{error} = $rb->error_message($@) if $@;
103 last;
106 $action eq 'place_hold' && do {
107 my $isbn = $cgi->param('isbn')
108 or response_bad_request("No 'isbn' specified");
109 $data{action} = eval { $rb->place_hold($isbn) };
110 response_bad_request($rb->error_message($@)) if $@;
112 $data{holds} = eval { $rb->holds };
113 $data{error} = $rb->error_message($@) if $@;
114 last;
117 $action eq 'remove_hold' && do {
118 my $isbn = $cgi->param('isbn')
119 or response_bad_request("No 'isbn' specified");
120 $data{action} = eval { $rb->remove_hold($isbn) };
121 response_bad_request($rb->error_message($@)) if $@;
123 $data{holds} = eval { $rb->holds };
124 $data{error} = $rb->error_message($@) if $@;
125 last;
128 response_bad_request("Invalid 'action': $action");
131 if ($@) {
132 $logger->error($@);
133 $data{error} = $rb->error_message("$@");
136 response(\%data);
139 sub response_bad_request {
140 my ($error) = @_;
141 response({error => $error}, "400 $error");
143 sub response {
144 my ($data, $status_line) = @_;
145 $status_line ||= "200 OK";
146 output_with_http_headers $cgi, undef, encode_json($data), 'json', $status_line;
147 exit;