Bug 17600: Standardize our EXPORT_OK
[srvgit] / Koha / REST / V1 / Patrons / Attributes.pm
1 package Koha::REST::V1::Patrons::Attributes;
2
3 # This file is part of Koha.
4 #
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18 use Modern::Perl;
19
20 use Mojo::Base 'Mojolicious::Controller';
21
22 use Koha::Patron::Attributes;
23 use Koha::Patrons;
24
25 use Scalar::Util qw( blessed );
26 use Try::Tiny qw( catch try );
27
28 =head1 NAME
29
30 Koha::REST::V1::Patrons::Attributes
31
32 =head1 API
33
34 =head2 Methods
35
36 =head3 list_patron_attributes
37
38 Controller method that handles listing the Koha::Patron::Attribute objects that belong
39 to a given patron.
40
41 =cut
42
43 sub list_patron_attributes {
44     my $c = shift->openapi->valid_input or return;
45
46     my $patron = Koha::Patrons->find( $c->validation->param('patron_id') );
47
48     unless ($patron) {
49         return $c->render(
50             status  => 404,
51             openapi => {
52                 error => 'Patron not found'
53             }
54         );
55     }
56
57     return try {
58
59         my $attributes_rs = $patron->extended_attributes;
60         my $attributes    = $c->objects->search($attributes_rs);
61
62         return $c->render(
63             status  => 200,
64             openapi => $attributes
65         );
66     }
67     catch {
68         $c->unhandled_exception($_);
69     };
70 }
71
72 =head3 add
73
74 Controller method that handles adding a Koha::Patron::Attribute to a given patron.
75
76 =cut
77
78 sub add {
79     my $c = shift->openapi->valid_input or return;
80
81     my $patron = Koha::Patrons->find( $c->validation->param('patron_id') );
82
83     unless ($patron) {
84         return $c->render(
85             status  => 404,
86             openapi => {
87                 error => 'Patron not found'
88             }
89         );
90     }
91
92     return try {
93
94         my $attribute = $patron->add_extended_attribute(
95             Koha::Patron::Attribute->new_from_api( # new_from_api takes care of mapping attributes
96                 $c->validation->param('body')
97             )->unblessed
98         );
99
100         $c->res->headers->location( $c->req->url->to_string . '/' . $attribute->id );
101         return $c->render(
102             status  => 201,
103             openapi => $attribute->to_api
104         );
105     }
106     catch {
107         if ( blessed $_ ) {
108             if (
109                 $_->isa(
110                     'Koha::Exceptions::Patron::Attribute::UniqueIDConstraint')
111               )
112             {
113                 return $c->render(
114                     status  => 409,
115                     openapi => {
116                         error => "$_"
117                     }
118                 );
119             }
120             elsif (
121                 $_->isa('Koha::Exceptions::Patron::Attribute::NonRepeatable') )
122             {
123                 return $c->render(
124                     status  => 409,
125                     openapi => {
126                         error => "$_"
127                     }
128                 );
129             }
130             elsif (
131                 $_->isa('Koha::Exceptions::Patron::Attribute::InvalidType') )
132             {
133                 return $c->render(
134                     status  => 400,
135                     openapi => { error => "$_" }
136                 );
137             }
138         }
139
140         $c->unhandled_exception($_);
141     };
142 }
143
144 =head3 overwrite
145
146 Controller method that handles overwriting extended attributes for a given patron.
147
148 =cut
149
150 sub overwrite {
151     my $c = shift->openapi->valid_input or return;
152
153     my $patron = Koha::Patrons->find( $c->validation->param('patron_id') );
154
155     unless ($patron) {
156         return $c->render(
157             status  => 404,
158             openapi => {
159                 error => 'Patron not found'
160             }
161         );
162     }
163
164     return try {
165
166         my $body = $c->validation->every_param('body');
167
168         my @attrs;
169
170         foreach my $attr ( @{$body} ) {
171             push @attrs, { code => $attr->{type}, attribute => $attr->{value} };
172         }
173
174         # Fetch the attributes, sorted by id
175         my $attributes = $patron->extended_attributes( \@attrs )->search( undef, { order_by => 'id' });
176
177         return $c->render(
178             status  => 200,
179             openapi => $attributes->to_api
180         );
181     }
182     catch {
183         if ( blessed $_ ) {
184             if ( $_->isa('Koha::Exceptions::Patron::Attribute::InvalidType') ) {
185                 return $c->render(
186                     status  => 400,
187                     openapi => { error => "$_" }
188                 );
189             }
190             elsif (
191                 $_->isa(
192                     'Koha::Exceptions::Patron::Attribute::UniqueIDConstraint')
193               )
194             {
195                 return $c->render(
196                     status  => 409,
197                     openapi => { error => "$_" }
198                 );
199             }
200             elsif (
201                 $_->isa('Koha::Exceptions::Patron::Attribute::NonRepeatable') )
202             {
203                 return $c->render(
204                     status  => 409,
205                     openapi => { error => "$_" }
206                 );
207             }
208             elsif ( $_->isa('Koha::Exceptions::Patron::MissingMandatoryExtendedAttribute') ) {
209                 return $c->render(
210                     status  => 400,
211                     openapi => { error => "$_" }
212                 );
213
214             }
215         }
216
217         $c->unhandled_exception($_);
218     };
219 }
220
221
222 =head3 update
223
224 Controller method that handles updating a single extended patron attribute.
225
226 =cut
227
228 sub update {
229     my $c = shift->openapi->valid_input or return;
230
231     my $patron = Koha::Patrons->find( $c->validation->param('patron_id') );
232
233     unless ($patron) {
234         return $c->render(
235             status  => 404,
236             openapi => {
237                 error => 'Patron not found'
238             }
239         );
240     }
241
242     return try {
243         my $attribute = $patron->extended_attributes->find(
244             $c->validation->param('extended_attribute_id') );
245
246         unless ($attribute) {
247             return $c->render(
248                 status  => 404,
249                 openapi => {
250                     error => 'Attribute not found'
251                 }
252             );
253         }
254
255         $attribute->set_from_api( $c->validation->param('body') )->store;
256         $attribute->discard_changes;
257
258         return $c->render(
259             status  => 200,
260             openapi => $attribute->to_api
261         );
262     }
263     catch {
264         if ( blessed $_ ) {
265             if ( $_->isa('Koha::Exceptions::Patron::Attribute::InvalidType') ) {
266                 return $c->render(
267                     status  => 400,
268                     openapi => { error => "$_" }
269                 );
270             }
271             elsif (
272                 $_->isa(
273                     'Koha::Exceptions::Patron::Attribute::UniqueIDConstraint')
274               )
275             {
276                 return $c->render(
277                     status  => 409,
278                     openapi => { error => "$_" }
279                 );
280             }
281             elsif (
282                 $_->isa('Koha::Exceptions::Patron::Attribute::NonRepeatable') )
283             {
284                 return $c->render(
285                     status  => 409,
286                     openapi => { error => "$_" }
287                 );
288             }
289         }
290
291         $c->unhandled_exception($_);
292     };
293
294 }
295
296 =head3 delete
297
298 Controller method that handles removing an extended patron attribute.
299
300 =cut
301
302 sub delete {
303     my $c = shift->openapi->valid_input or return;
304
305     my $patron = Koha::Patrons->find( $c->validation->param('patron_id') );
306
307     unless ($patron) {
308         return $c->render(
309             status  => 404,
310             openapi => {
311                 error => 'Patron not found'
312             }
313         );
314     }
315
316     return try {
317
318         my $attribute = $patron->extended_attributes->find(
319             $c->validation->param('extended_attribute_id') );
320
321         unless ($attribute) {
322             return $c->render(
323                 status  => 404,
324                 openapi => {
325                     error => 'Attribute not found'
326                 }
327             );
328         }
329
330         $attribute->delete;
331         return $c->render(
332             status  => 204,
333             openapi => q{}
334         );
335     }
336     catch {
337         $c->unhandled_exception($_);
338     };
339 }
340
341 1;