42a2b15adfa6e1682f7d7aa4852d93c37f3b4f15
[koha.git] / Koha / REST / V1 / Patrons.pm
1 package Koha::REST::V1::Patrons;
2
3 # This file is part of Koha.
4 #
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
8 # version.
9 #
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.
13 #
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.
17
18 use Modern::Perl;
19
20 use Mojo::Base 'Mojolicious::Controller';
21
22 use C4::Members qw( AddMember ModMember );
23 use Koha::Patrons;
24
25 use Scalar::Util qw(blessed);
26 use Try::Tiny;
27
28 =head1 NAME
29
30 Koha::REST::V1::Patrons
31
32 =head1 API
33
34 =head2 Methods
35
36 =head3 list
37
38 Controller function that handles listing Koha::Patron objects
39
40 =cut
41
42 sub list {
43     my $c = shift->openapi->valid_input or return;
44
45     return try {
46         my $patrons_set = Koha::Patrons->new;
47         my $patrons = $c->objects->search( $patrons_set );
48         return $c->render( status => 200, openapi => $patrons );
49     }
50     catch {
51         if ( $_->isa('DBIx::Class::Exception') ) {
52             return $c->render( status => 500,
53                 openapi => { error => $_->{msg} } );
54         }
55         else {
56             return $c->render(
57                 status  => 500,
58                 openapi => { error => "Something went wrong, check the logs." }
59             );
60         }
61     };
62 }
63
64 =head3 get
65
66 Controller function that handles retrieving a single Koha::Patron object
67
68 =cut
69
70 sub get {
71     my $c = shift->openapi->valid_input or return;
72
73     my $borrowernumber = $c->validation->param('borrowernumber');
74     my $patron = Koha::Patrons->find($borrowernumber);
75
76     unless ($patron) {
77         return $c->render(status => 404, openapi => { error => "Patron not found." });
78     }
79
80     return $c->render(status => 200, openapi => $patron);
81 }
82
83 =head3 add
84
85 Controller function that handles adding a new Koha::Patron object
86
87 =cut
88
89 sub add {
90     my $c = shift->openapi->valid_input or return;
91
92     return try {
93
94         my $body = _to_model($c->validation->param('body'));
95
96         # TODO: Use AddMember until it has been moved to Koha-namespace
97         my $borrowernumber = AddMember(%$body);
98         my $patron         = Koha::Patrons->find($borrowernumber);
99
100         return $c->render( status => 201, openapi => $patron );
101     }
102     catch {
103         unless ( blessed $_ && $_->can('rethrow') ) {
104             return $c->render(
105                 status  => 500,
106                 openapi => {
107                     error => "Something went wrong, check Koha logs for details."
108                 }
109             );
110         }
111         if ( $_->isa('Koha::Exceptions::Object::DuplicateID') ) {
112             return $c->render(
113                 status  => 409,
114                 openapi => { error => $_->error, conflict => $_->duplicate_id }
115             );
116         }
117         elsif ( $_->isa('Koha::Exceptions::Object::FKConstraint') ) {
118             return $c->render(
119                 status  => 400,
120                 openapi => { error => "Given " . $_->broken_fk . " does not exist" }
121             );
122         }
123         elsif ( $_->isa('Koha::Exceptions::BadParameter') ) {
124             return $c->render(
125                 status  => 400,
126                 openapi => { error => "Given " . $_->parameter . " does not exist" }
127             );
128         }
129         else {
130             return $c->render(
131                 status  => 500,
132                 openapi => {
133                     error => "Something went wrong, check Koha logs for details."
134                 }
135             );
136         }
137     };
138 }
139
140 =head3 update
141
142 Controller function that handles updating a Koha::Patron object
143
144 =cut
145
146 sub update {
147     my $c = shift->openapi->valid_input or return;
148
149     my $patron_id = $c->validation->param('borrowernumber');
150     my $patron    = Koha::Patrons->find( $patron_id );
151
152     unless ($patron) {
153          return $c->render(
154              status  => 404,
155              openapi => { error => "Patron not found" }
156          );
157      }
158
159     return try {
160         my $body = _to_model($c->validation->param('body'));
161
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;
165
166         if ( ModMember(%$body) ) {
167             # Fetch the updated Koha::Patron object
168             $patron->discard_changes;
169             return $c->render( status => 200, openapi => $patron );
170         }
171         else {
172             return $c->render(
173                 status  => 500,
174                 openapi => {
175                     error => 'Something went wrong, check Koha logs for details.'
176                 }
177             );
178         }
179     }
180     catch {
181         unless ( blessed $_ && $_->can('rethrow') ) {
182             return $c->render(
183                 status  => 500,
184                 openapi => {
185                     error => "Something went wrong, check Koha logs for details."
186                 }
187             );
188         }
189         if ( $_->isa('Koha::Exceptions::Object::DuplicateID') ) {
190             return $c->render(
191                 status  => 409,
192                 openapi => { error => $_->error, conflict => $_->duplicate_id }
193             );
194         }
195         elsif ( $_->isa('Koha::Exceptions::Object::FKConstraint') ) {
196             return $c->render(
197                 status  => 400,
198                 openapi => { error => "Given " . $_->broken_fk . " does not exist" }
199             );
200         }
201         elsif ( $_->isa('Koha::Exceptions::MissingParameter') ) {
202             return $c->render(
203                 status  => 400,
204                 openapi => {
205                     error      => "Missing mandatory parameter(s)",
206                     parameters => $_->parameter
207                 }
208             );
209         }
210         elsif ( $_->isa('Koha::Exceptions::BadParameter') ) {
211             return $c->render(
212                 status  => 400,
213                 openapi => {
214                     error      => "Invalid parameter(s)",
215                     parameters => $_->parameter
216                 }
217             );
218         }
219         elsif ( $_->isa('Koha::Exceptions::NoChanges') ) {
220             return $c->render(
221                 status  => 204,
222                 openapi => { error => "No changes have been made" }
223             );
224         }
225         else {
226             return $c->render(
227                 status  => 500,
228                 openapi => {
229                     error =>
230                       "Something went wrong, check Koha logs for details."
231                 }
232             );
233         }
234     };
235 }
236
237 =head3 delete
238
239 Controller function that handles deleting a Koha::Patron object
240
241 =cut
242
243 sub delete {
244     my $c = shift->openapi->valid_input or return;
245
246     my $patron;
247
248     return try {
249         $patron = Koha::Patrons->find( $c->validation->param('borrowernumber') );
250
251         # check if loans, reservations, debarrment, etc. before deletion!
252         my $res = $patron->delete;
253         return $c->render( status => 200, openapi => {} );
254     }
255     catch {
256         unless ($patron) {
257             return $c->render(
258                 status  => 404,
259                 openapi => { error => "Patron not found" }
260             );
261         }
262         else {
263             return $c->render(
264                 status  => 500,
265                 openapi => {
266                     error =>
267                       "Something went wrong, check Koha logs for details."
268                 }
269             );
270         }
271     };
272 }
273
274 =head3 _to_model
275
276 Helper function that maps REST api objects into Koha::Patron
277 attribute names.
278
279 =cut
280
281 sub _to_model {
282     my $params = shift;
283
284     $params->{lost} = ($params->{lost}) ? 1 : 0;
285     $params->{gonenoaddress} = ($params->{gonenoaddress}) ? 1 : 0;
286
287     return $params;
288 }
289
290 1;