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;
94 my $body = _to_model($c->validation->param('body'));
96 # TODO: Use AddMember until it has been moved to Koha-namespace
97 my $borrowernumber = AddMember(%$body);
98 my $patron = Koha::Patrons->find($borrowernumber);
100 return $c->render( status => 201, openapi => $patron );
103 unless ( blessed $_ && $_->can('rethrow') ) {
107 error => "Something went wrong, check Koha logs for details."
111 if ( $_->isa('Koha::Exceptions::Object::DuplicateID') ) {
114 openapi => { error => $_->error, conflict => $_->duplicate_id }
117 elsif ( $_->isa('Koha::Exceptions::Object::FKConstraint') ) {
120 openapi => { error => "Given " . $_->broken_fk . " does not exist" }
123 elsif ( $_->isa('Koha::Exceptions::BadParameter') ) {
126 openapi => { error => "Given " . $_->parameter . " does not exist" }
133 error => "Something went wrong, check Koha logs for details."
142 Controller function that handles updating a Koha::Patron object
147 my $c = shift->openapi->valid_input or return;
149 my $patron_id = $c->validation->param('borrowernumber');
150 my $patron = Koha::Patrons->find( $patron_id );
155 openapi => { error => "Patron not found" }
160 my $body = _to_model($c->validation->param('body'));
162 ## TODO: Use ModMember until it has been moved to Koha-namespace
163 # Add borrowernumber to $body, as required by ModMember
164 $body->{borrowernumber} = $patron_id;
166 if ( ModMember(%$body) ) {
167 # Fetch the updated Koha::Patron object
168 $patron->discard_changes;
169 return $c->render( status => 200, openapi => $patron );
175 error => 'Something went wrong, check Koha logs for details.'
181 unless ( blessed $_ && $_->can('rethrow') ) {
185 error => "Something went wrong, check Koha logs for details."
189 if ( $_->isa('Koha::Exceptions::Object::DuplicateID') ) {
192 openapi => { error => $_->error, conflict => $_->duplicate_id }
195 elsif ( $_->isa('Koha::Exceptions::Object::FKConstraint') ) {
198 openapi => { error => "Given " . $_->broken_fk . " does not exist" }
201 elsif ( $_->isa('Koha::Exceptions::MissingParameter') ) {
205 error => "Missing mandatory parameter(s)",
206 parameters => $_->parameter
210 elsif ( $_->isa('Koha::Exceptions::BadParameter') ) {
214 error => "Invalid parameter(s)",
215 parameters => $_->parameter
219 elsif ( $_->isa('Koha::Exceptions::NoChanges') ) {
222 openapi => { error => "No changes have been made" }
230 "Something went wrong, check Koha logs for details."
239 Controller function that handles deleting a Koha::Patron object
244 my $c = shift->openapi->valid_input or return;
249 $patron = Koha::Patrons->find( $c->validation->param('borrowernumber') );
251 # check if loans, reservations, debarrment, etc. before deletion!
252 my $res = $patron->delete;
253 return $c->render( status => 200, openapi => {} );
259 openapi => { error => "Patron not found" }
267 "Something went wrong, check Koha logs for details."
276 Helper function that maps REST api objects into Koha::Patron
284 $params->{lost} = ($params->{lost}) ? 1 : 0;
285 $params->{gonenoaddress} = ($params->{gonenoaddress}) ? 1 : 0;