1 package Koha::REST::V1::Patrons;
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::Patrons
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 # Add borrowernumber to $body, as required by ModMember
159 $body->{borrowernumber} = $patron->borrowernumber;
161 if ( ModMember(%$body) ) {
162 return $c->render( status => 200, openapi => $patron );
168 error => 'Something went wrong, check Koha logs for details.'
177 openapi => { error => "Patron not found" }
180 unless ( blessed $_ && $_->can('rethrow') ) {
184 error => "Something went wrong, check Koha logs for details."
188 if ( $_->isa('Koha::Exceptions::Patron::DuplicateObject') ) {
191 openapi => { error => $_->error, conflict => $_->conflict }
194 elsif ( $_->isa('Koha::Exceptions::Library::BranchcodeNotFound') ) {
197 openapi => { error => "Given branchcode does not exist" }
200 elsif ( $_->isa('Koha::Exceptions::Category::CategorycodeNotFound') ) {
203 openapi => { error => "Given categorycode does not exist" }
206 elsif ( $_->isa('Koha::Exceptions::MissingParameter') ) {
210 error => "Missing mandatory parameter(s)",
211 parameters => $_->parameter
215 elsif ( $_->isa('Koha::Exceptions::BadParameter') ) {
219 error => "Invalid parameter(s)",
220 parameters => $_->parameter
224 elsif ( $_->isa('Koha::Exceptions::NoChanges') ) {
227 openapi => { error => "No changes have been made" }
235 "Something went wrong, check Koha logs for details."
244 Controller function that handles deleting a Koha::Patron object
249 my $c = shift->openapi->valid_input or return;
254 $patron = Koha::Patrons->find( $c->validation->param('borrowernumber') );
256 # check if loans, reservations, debarrment, etc. before deletion!
257 my $res = $patron->delete;
258 return $c->render( status => 200, openapi => {} );
264 openapi => { error => "Patron not found" }
272 "Something went wrong, check Koha logs for details."
281 Helper function that maps REST api objects into Koha::Patron
289 $params->{lost} = ($params->{lost}) ? 1 : 0;
290 $params->{gonenoaddress} = ($params->{gonenoaddress}) ? 1 : 0;