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 155-161
sub update {
Link Here
|
155 |
$patron->set( _to_model($body) )->_validate; |
156 |
$patron->set( _to_model($body) )->_validate; |
156 |
|
157 |
|
157 |
# TODO: Use ModMember until it has been moved to Koha-namespace |
158 |
# TODO: Use ModMember until it has been moved to Koha-namespace |
158 |
if ( ModMember(%$body) ) { |
159 |
if ( ModMember( %{ _to_model($body) } ) ) { |
159 |
return $c->render( status => 200, openapi => $patron ); |
160 |
return $c->render( status => 200, openapi => $patron ); |
160 |
} |
161 |
} |
161 |
else { |
162 |
else { |
Lines 191-203
sub update {
Link Here
|
191 |
elsif ( $_->isa('Koha::Exceptions::Library::BranchcodeNotFound') ) { |
192 |
elsif ( $_->isa('Koha::Exceptions::Library::BranchcodeNotFound') ) { |
192 |
return $c->render( |
193 |
return $c->render( |
193 |
status => 400, |
194 |
status => 400, |
194 |
openapi => { error => "Given branchcode does not exist" } |
195 |
openapi => { error => "Given library_id does not exist" } |
195 |
); |
196 |
); |
196 |
} |
197 |
} |
197 |
elsif ( $_->isa('Koha::Exceptions::Category::CategorycodeNotFound') ) { |
198 |
elsif ( $_->isa('Koha::Exceptions::Category::CategorycodeNotFound') ) { |
198 |
return $c->render( |
199 |
return $c->render( |
199 |
status => 400, |
200 |
status => 400, |
200 |
openapi => { error => "Given categorycode does not exist" } |
201 |
openapi => { error => "Given category_id does not exist" } |
201 |
); |
202 |
); |
202 |
} |
203 |
} |
203 |
elsif ( $_->isa('Koha::Exceptions::MissingParameter') ) { |
204 |
elsif ( $_->isa('Koha::Exceptions::MissingParameter') ) { |
Lines 248-254
sub delete {
Link Here
|
248 |
my $patron; |
249 |
my $patron; |
249 |
|
250 |
|
250 |
return try { |
251 |
return try { |
251 |
$patron = Koha::Patrons->find( $c->validation->param('borrowernumber') ); |
252 |
$patron = Koha::Patrons->find( $c->validation->param('patron_id') ); |
252 |
|
253 |
|
253 |
# check if loans, reservations, debarrment, etc. before deletion! |
254 |
# check if loans, reservations, debarrment, etc. before deletion! |
254 |
my $res = $patron->delete; |
255 |
my $res = $patron->delete; |
Lines 273-278
sub delete {
Link Here
|
273 |
}; |
274 |
}; |
274 |
} |
275 |
} |
275 |
|
276 |
|
|
|
277 |
=head3 _to_api |
278 |
|
279 |
Helper function that maps Koha::Patron objects into REST api |
280 |
attribute names. |
281 |
|
282 |
=cut |
283 |
|
284 |
sub _to_api { |
285 |
my $patron = shift; |
286 |
|
287 |
my $mapping = { |
288 |
borrowernumber => 'patron_id', |
289 |
branchcode => 'library_id', |
290 |
categorycode => 'category_id' |
291 |
}; |
292 |
|
293 |
foreach my $key ( keys %{ $mapping } ) { |
294 |
if ( exists $patron->{ $key } ) { |
295 |
$patron->{ $mapping->{$key} } = delete $patron->{ $key }; |
296 |
} |
297 |
} |
298 |
|
299 |
return $patron; |
300 |
} |
301 |
|
276 |
=head3 _to_model |
302 |
=head3 _to_model |
277 |
|
303 |
|
278 |
Helper function that maps REST api objects into Koha::Patron |
304 |
Helper function that maps REST api objects into Koha::Patron |
Lines 281-292
attribute names.
Link Here
|
281 |
=cut |
307 |
=cut |
282 |
|
308 |
|
283 |
sub _to_model { |
309 |
sub _to_model { |
284 |
my $params = shift; |
310 |
my $patron = shift; |
|
|
311 |
|
312 |
my $mapping = { |
313 |
category_id => 'categorycode', |
314 |
library_id => 'branchcode', |
315 |
patron_id => 'borrowernumber' |
316 |
}; |
317 |
|
318 |
foreach my $key ( keys %{ $mapping } ) { |
319 |
if ( exists $patron->{ $key } ) { |
320 |
$patron->{ $mapping->{$key} } = delete $patron->{ $key }; |
321 |
} |
322 |
} |
285 |
|
323 |
|
286 |
$params->{lost} = ($params->{lost}) ? 1 : 0; |
324 |
$patron->{lost} = ($patron->{lost}) ? 1 : 0; |
287 |
$params->{gonenoaddress} = ($params->{gonenoaddress}) ? 1 : 0; |
325 |
$patron->{gonenoaddress} = ($patron->{gonenoaddress}) ? 1 : 0; |
288 |
|
326 |
|
289 |
return $params; |
327 |
return $patron; |
290 |
} |
328 |
} |
291 |
|
329 |
|
292 |
1; |
330 |
1; |