Bug 16330: Move patches to OpenAPI
[koha.git] / Koha / REST / V1 / Patron.pm
1 package Koha::REST::V1::Patron;
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::Patron
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 )->as_list;
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         my $body = $c->validation->param('body');
94
95         Koha::Patron->new($body)->_validate;
96
97         # TODO: Use AddMember until it has been moved to Koha-namespace
98         my $borrowernumber = AddMember(%$body);
99         my $patron         = Koha::Patrons->find($borrowernumber);
100
101         return $c->render( status => 201, openapi => $patron );
102     }
103     catch {
104         unless ( blessed $_ && $_->can('rethrow') ) {
105             return $c->render(
106                 status  => 500,
107                 openapi => {
108                     error => "Something went wrong, check Koha logs for details."
109                 }
110             );
111         }
112         if ( $_->isa('Koha::Exceptions::Patron::DuplicateObject') ) {
113             return $c->render(
114                 status  => 409,
115                 openapi => { error => $_->error, conflict => $_->conflict }
116             );
117         }
118         elsif ( $_->isa('Koha::Exceptions::Library::BranchcodeNotFound') ) {
119             return $c->render(
120                 status  => 400,
121                 openapi => { error => "Given branchcode does not exist" }
122             );
123         }
124         elsif ( $_->isa('Koha::Exceptions::Category::CategorycodeNotFound') ) {
125             return $c->render(
126                 status  => 400,
127                 openapi => { error => "Given categorycode does not exist" }
128             );
129         }
130         else {
131             return $c->render(
132                 status  => 500,
133                 openapi => {
134                     error => "Something went wrong, check Koha logs for details."
135                 }
136             );
137         }
138     };
139 }
140
141 =head3 update
142
143 Controller function that handles updating a Koha::Patron object
144
145 =cut
146
147 sub update {
148     my $c = shift->openapi->valid_input or return;
149
150     my $patron = Koha::Patrons->find( $c->validation->param('borrowernumber') );
151
152     return try {
153         my $body = $c->validation->param('body');
154
155         $patron->set( _to_model($body) )->_validate;
156
157         # TODO: Use ModMember until it has been moved to Koha-namespace
158         if ( ModMember(%$body) ) {
159             return $c->render( status => 200, openapi => $patron );
160         }
161         else {
162             return $c->render(
163                 status  => 500,
164                 openapi => {
165                     error => 'Something went wrong, check Koha logs for details.'
166                 }
167             );
168         }
169     }
170     catch {
171         unless ($patron) {
172             return $c->render(
173                 status  => 404,
174                 openapi => { error => "Patron not found" }
175             );
176         }
177         unless ( blessed $_ && $_->can('rethrow') ) {
178             return $c->render(
179                 status  => 500,
180                 openapi => {
181                     error => "Something went wrong, check Koha logs for details."
182                 }
183             );
184         }
185         if ( $_->isa('Koha::Exceptions::Patron::DuplicateObject') ) {
186             return $c->render(
187                 status  => 409,
188                 openapi => { error => $_->error, conflict => $_->conflict }
189             );
190         }
191         elsif ( $_->isa('Koha::Exceptions::Library::BranchcodeNotFound') ) {
192             return $c->render(
193                 status  => 400,
194                 openapi => { error => "Given branchcode does not exist" }
195             );
196         }
197         elsif ( $_->isa('Koha::Exceptions::Category::CategorycodeNotFound') ) {
198             return $c->render(
199                 status  => 400,
200                 openapi => { error => "Given categorycode does not exist" }
201             );
202         }
203         elsif ( $_->isa('Koha::Exceptions::MissingParameter') ) {
204             return $c->render(
205                 status  => 400,
206                 openapi => {
207                     error      => "Missing mandatory parameter(s)",
208                     parameters => $_->parameter
209                 }
210             );
211         }
212         elsif ( $_->isa('Koha::Exceptions::BadParameter') ) {
213             return $c->render(
214                 status  => 400,
215                 openapi => {
216                     error      => "Invalid parameter(s)",
217                     parameters => $_->parameter
218                 }
219             );
220         }
221         elsif ( $_->isa('Koha::Exceptions::NoChanges') ) {
222             return $c->render(
223                 status  => 204,
224                 openapi => { error => "No changes have been made" }
225             );
226         }
227         else {
228             return $c->render(
229                 status  => 500,
230                 openapi => {
231                     error =>
232                       "Something went wrong, check Koha logs for details."
233                 }
234             );
235         }
236     };
237 }
238
239 =head3 delete
240
241 Controller function that handles deleting a Koha::Patron object
242
243 =cut
244
245 sub delete {
246     my $c = shift->openapi->valid_input or return;
247
248     my $patron;
249
250     return try {
251         $patron = Koha::Patrons->find( $c->validation->param('borrowernumber') );
252
253         # check if loans, reservations, debarrment, etc. before deletion!
254         my $res = $patron->delete;
255         return $c->render( status => 200, openapi => {} );
256     }
257     catch {
258         unless ($patron) {
259             return $c->render(
260                 status  => 404,
261                 openapi => { error => "Patron not found" }
262             );
263         }
264         else {
265             return $c->render(
266                 status  => 500,
267                 openapi => {
268                     error =>
269                       "Something went wrong, check Koha logs for details."
270                 }
271             );
272         }
273     };
274 }
275
276 =head3 _to_model
277
278 Helper function that maps REST api objects into Koha::Patron
279 attribute names.
280
281 =cut
282
283 sub _to_model {
284     my $params = shift;
285
286     $params->{lost} = ($params->{lost}) ? 1 : 0;
287     $params->{gonenoaddress} = ($params->{gonenoaddress}) ? 1 : 0;
288
289     return $params;
290 }
291
292 1;