1 package Koha
::REST
::V1
::Patron
;
3 # This file is part of Koha.
5 # Koha is free software; you can redistribute it and/or modify it under the
6 # terms of the GNU General Public License as published by the Free Software
7 # Foundation; either version 3 of the License, or (at your option) any later
10 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
11 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License along
15 # with Koha; if not, write to the Free Software Foundation, Inc.,
16 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 use Mojo
::Base
'Mojolicious::Controller';
22 use C4
::Members
qw( AddMember ModMember );
25 use Scalar
::Util
qw(blessed);
30 Koha::REST::V1::Patron
38 Controller function that handles listing Koha::Patron objects
43 my $c = shift->openapi->valid_input or return;
46 my $patrons_set = Koha
::Patrons
->new;
47 my @patrons = $c->objects->search( $patrons_set )->as_list;
48 return $c->render( status
=> 200, openapi
=> \
@patrons );
51 if ( $_->isa('DBIx::Class::Exception') ) {
52 return $c->render( status
=> 500,
53 openapi
=> { error
=> $_->{msg
} } );
58 openapi
=> { error
=> "Something went wrong, check the logs." }
66 Controller function that handles retrieving a single Koha::Patron object
71 my $c = shift->openapi->valid_input or return;
73 my $borrowernumber = $c->validation->param('borrowernumber');
74 my $patron = Koha
::Patrons
->find($borrowernumber);
77 return $c->render(status
=> 404, openapi
=> { error
=> "Patron not found." });
80 return $c->render(status
=> 200, openapi
=> $patron);
85 Controller function that handles adding a new Koha::Patron object
90 my $c = shift->openapi->valid_input or return;
93 my $body = $c->validation->param('body');
95 Koha
::Patron
->new($body)->_validate;
97 # TODO: Use AddMember until it has been moved to Koha-namespace
98 my $borrowernumber = AddMember
(%$body);
99 my $patron = Koha
::Patrons
->find($borrowernumber);
101 return $c->render( status
=> 201, openapi
=> $patron );
104 unless ( blessed
$_ && $_->can('rethrow') ) {
108 error
=> "Something went wrong, check Koha logs for details."
112 if ( $_->isa('Koha::Exceptions::Patron::DuplicateObject') ) {
115 openapi
=> { error
=> $_->error, conflict
=> $_->conflict }
118 elsif ( $_->isa('Koha::Exceptions::Library::BranchcodeNotFound') ) {
121 openapi
=> { error
=> "Given branchcode does not exist" }
124 elsif ( $_->isa('Koha::Exceptions::Category::CategorycodeNotFound') ) {
127 openapi
=> { error
=> "Given categorycode does not exist" }
134 error
=> "Something went wrong, check Koha logs for details."
143 Controller function that handles updating a Koha::Patron object
148 my $c = shift->openapi->valid_input or return;
150 my $patron = Koha
::Patrons
->find( $c->validation->param('borrowernumber') );
153 my $body = $c->validation->param('body');
155 $patron->set( _to_model
($body) )->_validate;
157 # TODO: Use ModMember until it has been moved to Koha-namespace
158 if ( ModMember
(%$body) ) {
159 return $c->render( status
=> 200, openapi
=> $patron );
165 error
=> 'Something went wrong, check Koha logs for details.'
174 openapi
=> { error
=> "Patron not found" }
177 unless ( blessed
$_ && $_->can('rethrow') ) {
181 error
=> "Something went wrong, check Koha logs for details."
185 if ( $_->isa('Koha::Exceptions::Patron::DuplicateObject') ) {
188 openapi
=> { error
=> $_->error, conflict
=> $_->conflict }
191 elsif ( $_->isa('Koha::Exceptions::Library::BranchcodeNotFound') ) {
194 openapi
=> { error
=> "Given branchcode does not exist" }
197 elsif ( $_->isa('Koha::Exceptions::Category::CategorycodeNotFound') ) {
200 openapi
=> { error
=> "Given categorycode does not exist" }
203 elsif ( $_->isa('Koha::Exceptions::MissingParameter') ) {
207 error
=> "Missing mandatory parameter(s)",
208 parameters
=> $_->parameter
212 elsif ( $_->isa('Koha::Exceptions::BadParameter') ) {
216 error
=> "Invalid parameter(s)",
217 parameters
=> $_->parameter
221 elsif ( $_->isa('Koha::Exceptions::NoChanges') ) {
224 openapi
=> { error
=> "No changes have been made" }
232 "Something went wrong, check Koha logs for details."
241 Controller function that handles deleting a Koha::Patron object
246 my $c = shift->openapi->valid_input or return;
251 $patron = Koha
::Patrons
->find( $c->validation->param('borrowernumber') );
253 # check if loans, reservations, debarrment, etc. before deletion!
254 my $res = $patron->delete;
255 return $c->render( status
=> 200, openapi
=> {} );
261 openapi
=> { error
=> "Patron not found" }
269 "Something went wrong, check Koha logs for details."
278 Helper function that maps REST api objects into Koha::Patron
286 $params->{lost
} = ($params->{lost
}) ?
1 : 0;
287 $params->{gonenoaddress
} = ($params->{gonenoaddress
}) ?
1 : 0;