View | Details | Raw Unified | Return to bug 23666
Collapse All | Expand All

(-)a/Koha/REST/V1/Patrons/Attributes.pm (-1 / +270 lines)
Line 0 Link Here
0
- 
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;
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
            }
191
            elsif (
192
                $_->isa(
193
                    'Koha::Exceptions::Patron::Attribute::UniqueIDConstraint')
194
              )
195
            {
196
                return $c->render(
197
                    status  => 409,
198
                    openapi => { error => "$_" }
199
                );
200
            }
201
            elsif (
202
                $_->isa('Koha::Exceptions::Patron::Attribute::NonRepeatable') )
203
            {
204
                return $c->render(
205
                    status  => 409,
206
                    openapi => { error => "$_" }
207
                );
208
            }
209
            elsif ( $_->isa('Koha::Exceptions::Object::FKConstraint') ) {
210
                return $c->render(
211
                    status  => 400,
212
                    openapi => {
213
                        error => 'Missing mandatory attribute of type "'
214
                          . $_->value . '".'
215
                    }
216
                );
217
218
            }
219
        }
220
221
        $c->unhandled_exception($_);
222
    };
223
}
224
225
=head3 delete
226
227
Controller method that handling removing an extended patron attribute
228
229
=cut
230
231
sub delete {
232
    my $c = shift->openapi->valid_input or return;
233
234
    my $patron = Koha::Patrons->find( $c->validation->param('patron_id') );
235
236
    unless ($patron) {
237
        return $c->render(
238
            status  => 404,
239
            openapi => {
240
                error => 'Patron not found'
241
            }
242
        );
243
    }
244
245
    return try {
246
247
        my $attribute = $patron->extended_attributes->find(
248
            $c->validation->param('extended_attribute_id') );
249
250
        unless ($attribute) {
251
            return $c->render(
252
                status  => 404,
253
                openapi => {
254
                    error => 'Attribute not found'
255
                }
256
            );
257
        }
258
259
        $attribute->delete;
260
        return $c->render(
261
            status  => 204,
262
            openapi => q{}
263
        );
264
    }
265
    catch {
266
        $c->unhandled_exception($_);
267
    };
268
}
269
270
1;

Return to bug 23666