1 package Koha::REST::V1::Patrons::Attributes;
3 # This file is part of Koha.
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.
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.
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>.
20 use Mojo::Base 'Mojolicious::Controller';
22 use Koha::Patron::Attributes;
25 use Scalar::Util qw(blessed);
30 Koha::REST::V1::Patrons::Attributes
36 =head3 list_patron_attributes
38 Controller method that handles listing the Koha::Patron::Attribute objects that belong
43 sub list_patron_attributes {
44 my $c = shift->openapi->valid_input or return;
46 my $patron = Koha::Patrons->find( $c->validation->param('patron_id') );
52 error => 'Patron not found'
59 my $attributes_rs = $patron->extended_attributes;
60 my $attributes = $c->objects->search($attributes_rs);
64 openapi => $attributes
68 $c->unhandled_exception($_);
74 Controller method that handles adding a Koha::Patron::Attribute to a given patron.
79 my $c = shift->openapi->valid_input or return;
81 my $patron = Koha::Patrons->find( $c->validation->param('patron_id') );
87 error => 'Patron not found'
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')
100 $c->res->headers->location( $c->req->url->to_string . '/' . $attribute->id );
103 openapi => $attribute->to_api
110 'Koha::Exceptions::Patron::Attribute::UniqueIDConstraint')
121 $_->isa('Koha::Exceptions::Patron::Attribute::NonRepeatable') )
131 $_->isa('Koha::Exceptions::Patron::Attribute::InvalidType') )
135 openapi => { error => "$_" }
140 $c->unhandled_exception($_);
146 Controller method that handles overwriting extended attributes for a given patron.
151 my $c = shift->openapi->valid_input or return;
153 my $patron = Koha::Patrons->find( $c->validation->param('patron_id') );
159 error => 'Patron not found'
166 my $body = $c->validation->every_param('body');
170 foreach my $attr ( @{$body} ) {
171 push @attrs, { code => $attr->{type}, attribute => $attr->{value} };
174 # Fetch the attributes, sorted by id
175 my $attributes = $patron->extended_attributes( \@attrs )->search( undef, { order_by => 'id' });
179 openapi => $attributes->to_api
184 if ( $_->isa('Koha::Exceptions::Patron::Attribute::InvalidType') ) {
187 openapi => { error => "$_" }
192 'Koha::Exceptions::Patron::Attribute::UniqueIDConstraint')
197 openapi => { error => "$_" }
201 $_->isa('Koha::Exceptions::Patron::Attribute::NonRepeatable') )
205 openapi => { error => "$_" }
208 elsif ( $_->isa('Koha::Exceptions::Patron::MissingMandatoryExtendedAttribute') ) {
211 openapi => { error => "$_" }
217 $c->unhandled_exception($_);
224 Controller method that handles updating a single extended patron attribute.
229 my $c = shift->openapi->valid_input or return;
231 my $patron = Koha::Patrons->find( $c->validation->param('patron_id') );
237 error => 'Patron not found'
243 my $attribute = $patron->extended_attributes->find(
244 $c->validation->param('extended_attribute_id') );
246 unless ($attribute) {
250 error => 'Attribute not found'
255 $attribute->set_from_api( $c->validation->param('body') )->store;
256 $attribute->discard_changes;
260 openapi => $attribute->to_api
265 if ( $_->isa('Koha::Exceptions::Patron::Attribute::InvalidType') ) {
268 openapi => { error => "$_" }
273 'Koha::Exceptions::Patron::Attribute::UniqueIDConstraint')
278 openapi => { error => "$_" }
282 $_->isa('Koha::Exceptions::Patron::Attribute::NonRepeatable') )
286 openapi => { error => "$_" }
291 $c->unhandled_exception($_);
298 Controller method that handles removing an extended patron attribute.
303 my $c = shift->openapi->valid_input or return;
305 my $patron = Koha::Patrons->find( $c->validation->param('patron_id') );
311 error => 'Patron not found'
318 my $attribute = $patron->extended_attributes->find(
319 $c->validation->param('extended_attribute_id') );
321 unless ($attribute) {
325 error => 'Attribute not found'
337 $c->unhandled_exception($_);