Lines 44-50
sub list {
Link Here
|
44 |
|
44 |
|
45 |
return try { |
45 |
return try { |
46 |
my $patrons_set = Koha::Patrons->new; |
46 |
my $patrons_set = Koha::Patrons->new; |
47 |
my @patrons = $c->objects->search( $patrons_set )->as_list; |
47 |
my @patrons = $c->objects->search( $patrons_set, \&_to_model )->as_list; |
|
|
48 |
@patrons = map { _to_api($_->TO_JSON) } @patrons; |
48 |
return $c->render( status => 200, openapi => \@patrons ); |
49 |
return $c->render( status => 200, openapi => \@patrons ); |
49 |
} |
50 |
} |
50 |
catch { |
51 |
catch { |
Lines 70-83
Controller function that handles retrieving a single Koha::Patron object
Link Here
|
70 |
sub get { |
71 |
sub get { |
71 |
my $c = shift->openapi->valid_input or return; |
72 |
my $c = shift->openapi->valid_input or return; |
72 |
|
73 |
|
73 |
my $borrowernumber = $c->validation->param('borrowernumber'); |
74 |
my $patron_id = $c->validation->param('patron_id'); |
74 |
my $patron = Koha::Patrons->find($borrowernumber); |
75 |
my $patron = Koha::Patrons->find($patron_id); |
75 |
|
76 |
|
76 |
unless ($patron) { |
77 |
unless ($patron) { |
77 |
return $c->render(status => 404, openapi => { error => "Patron not found." }); |
78 |
return $c->render( status => 404, openapi => { error => "Patron not found." } ); |
78 |
} |
79 |
} |
79 |
|
80 |
|
80 |
return $c->render(status => 200, openapi => $patron); |
81 |
return $c->render( status => 200, openapi => _to_api($patron->TO_JSON) ); |
81 |
} |
82 |
} |
82 |
|
83 |
|
83 |
=head3 add |
84 |
=head3 add |
Lines 92-102
sub add {
Link Here
|
92 |
return try { |
93 |
return try { |
93 |
my $body = $c->validation->param('body'); |
94 |
my $body = $c->validation->param('body'); |
94 |
|
95 |
|
95 |
Koha::Patron->new($body)->_validate; |
96 |
Koha::Patron->new(_to_model($body))->_validate; |
96 |
|
97 |
|
97 |
# TODO: Use AddMember until it has been moved to Koha-namespace |
98 |
# TODO: Use AddMember until it has been moved to Koha-namespace |
98 |
my $borrowernumber = AddMember(%$body); |
99 |
my $patron_id = AddMember( %{ _to_model($body) } ); |
99 |
my $patron = Koha::Patrons->find($borrowernumber); |
100 |
my $patron = _to_api(Koha::Patrons->find( $patron_id )->TO_JSON); |
100 |
|
101 |
|
101 |
return $c->render( status => 201, openapi => $patron ); |
102 |
return $c->render( status => 201, openapi => $patron ); |
102 |
} |
103 |
} |
Lines 118-130
sub add {
Link Here
|
118 |
elsif ( $_->isa('Koha::Exceptions::Library::BranchcodeNotFound') ) { |
119 |
elsif ( $_->isa('Koha::Exceptions::Library::BranchcodeNotFound') ) { |
119 |
return $c->render( |
120 |
return $c->render( |
120 |
status => 400, |
121 |
status => 400, |
121 |
openapi => { error => "Given branchcode does not exist" } |
122 |
openapi => { error => "Given library_id does not exist" } |
122 |
); |
123 |
); |
123 |
} |
124 |
} |
124 |
elsif ( $_->isa('Koha::Exceptions::Category::CategorycodeNotFound') ) { |
125 |
elsif ( $_->isa('Koha::Exceptions::Category::CategorycodeNotFound') ) { |
125 |
return $c->render( |
126 |
return $c->render( |
126 |
status => 400, |
127 |
status => 400, |
127 |
openapi => { error => "Given categorycode does not exist" } |
128 |
openapi => { error => "Given category_id does not exist" } |
128 |
); |
129 |
); |
129 |
} |
130 |
} |
130 |
else { |
131 |
else { |
Lines 147-153
Controller function that handles updating a Koha::Patron object
Link Here
|
147 |
sub update { |
148 |
sub update { |
148 |
my $c = shift->openapi->valid_input or return; |
149 |
my $c = shift->openapi->valid_input or return; |
149 |
|
150 |
|
150 |
my $patron = Koha::Patrons->find( $c->validation->param('borrowernumber') ); |
151 |
my $patron = Koha::Patrons->find( $c->validation->param('patron_id') ); |
151 |
|
152 |
|
152 |
return try { |
153 |
return try { |
153 |
my $body = $c->validation->param('body'); |
154 |
my $body = $c->validation->param('body'); |
Lines 158-164
sub update {
Link Here
|
158 |
# Add borrowernumber to $body, as required by ModMember |
159 |
# Add borrowernumber to $body, as required by ModMember |
159 |
$body->{borrowernumber} = $patron->borrowernumber; |
160 |
$body->{borrowernumber} = $patron->borrowernumber; |
160 |
|
161 |
|
161 |
if ( ModMember(%$body) ) { |
162 |
if ( ModMember( %{ _to_model($body) } ) ) { |
162 |
return $c->render( status => 200, openapi => $patron ); |
163 |
return $c->render( status => 200, openapi => $patron ); |
163 |
} |
164 |
} |
164 |
else { |
165 |
else { |
Lines 194-206
sub update {
Link Here
|
194 |
elsif ( $_->isa('Koha::Exceptions::Library::BranchcodeNotFound') ) { |
195 |
elsif ( $_->isa('Koha::Exceptions::Library::BranchcodeNotFound') ) { |
195 |
return $c->render( |
196 |
return $c->render( |
196 |
status => 400, |
197 |
status => 400, |
197 |
openapi => { error => "Given branchcode does not exist" } |
198 |
openapi => { error => "Given library_id does not exist" } |
198 |
); |
199 |
); |
199 |
} |
200 |
} |
200 |
elsif ( $_->isa('Koha::Exceptions::Category::CategorycodeNotFound') ) { |
201 |
elsif ( $_->isa('Koha::Exceptions::Category::CategorycodeNotFound') ) { |
201 |
return $c->render( |
202 |
return $c->render( |
202 |
status => 400, |
203 |
status => 400, |
203 |
openapi => { error => "Given categorycode does not exist" } |
204 |
openapi => { error => "Given category_id does not exist" } |
204 |
); |
205 |
); |
205 |
} |
206 |
} |
206 |
elsif ( $_->isa('Koha::Exceptions::MissingParameter') ) { |
207 |
elsif ( $_->isa('Koha::Exceptions::MissingParameter') ) { |
Lines 251-257
sub delete {
Link Here
|
251 |
my $patron; |
252 |
my $patron; |
252 |
|
253 |
|
253 |
return try { |
254 |
return try { |
254 |
$patron = Koha::Patrons->find( $c->validation->param('borrowernumber') ); |
255 |
$patron = Koha::Patrons->find( $c->validation->param('patron_id') ); |
255 |
|
256 |
|
256 |
# check if loans, reservations, debarrment, etc. before deletion! |
257 |
# check if loans, reservations, debarrment, etc. before deletion! |
257 |
my $res = $patron->delete; |
258 |
my $res = $patron->delete; |
Lines 276-281
sub delete {
Link Here
|
276 |
}; |
277 |
}; |
277 |
} |
278 |
} |
278 |
|
279 |
|
|
|
280 |
=head3 _to_api |
281 |
|
282 |
Helper function that maps Koha::Patron objects into REST api |
283 |
attribute names. |
284 |
|
285 |
=cut |
286 |
|
287 |
sub _to_api { |
288 |
my $patron = shift; |
289 |
|
290 |
my $mapping = { |
291 |
borrowernumber => 'patron_id', |
292 |
branchcode => 'library_id', |
293 |
categorycode => 'category_id' |
294 |
}; |
295 |
|
296 |
foreach my $key ( keys %{ $mapping } ) { |
297 |
if ( exists $patron->{ $key } ) { |
298 |
$patron->{ $mapping->{$key} } = delete $patron->{ $key }; |
299 |
} |
300 |
} |
301 |
|
302 |
return $patron; |
303 |
} |
304 |
|
279 |
=head3 _to_model |
305 |
=head3 _to_model |
280 |
|
306 |
|
281 |
Helper function that maps REST api objects into Koha::Patron |
307 |
Helper function that maps REST api objects into Koha::Patron |
Lines 284-295
attribute names.
Link Here
|
284 |
=cut |
310 |
=cut |
285 |
|
311 |
|
286 |
sub _to_model { |
312 |
sub _to_model { |
287 |
my $params = shift; |
313 |
my $patron = shift; |
|
|
314 |
|
315 |
my $mapping = { |
316 |
category_id => 'categorycode', |
317 |
library_id => 'branchcode', |
318 |
patron_id => 'borrowernumber' |
319 |
}; |
320 |
|
321 |
foreach my $key ( keys %{ $mapping } ) { |
322 |
if ( exists $patron->{ $key } ) { |
323 |
$patron->{ $mapping->{$key} } = delete $patron->{ $key }; |
324 |
} |
325 |
} |
288 |
|
326 |
|
289 |
$params->{lost} = ($params->{lost}) ? 1 : 0; |
327 |
$patron->{lost} = ($patron->{lost}) ? 1 : 0; |
290 |
$params->{gonenoaddress} = ($params->{gonenoaddress}) ? 1 : 0; |
328 |
$patron->{gonenoaddress} = ($patron->{gonenoaddress}) ? 1 : 0; |
291 |
|
329 |
|
292 |
return $params; |
330 |
return $patron; |
293 |
} |
331 |
} |
294 |
|
332 |
|
295 |
1; |
333 |
1; |