|
Lines 21-31
use Koha::AdvancedEditorMacros;
Link Here
|
| 21 |
|
21 |
|
| 22 |
use Try::Tiny; |
22 |
use Try::Tiny; |
| 23 |
|
23 |
|
| 24 |
=head1 API |
24 |
=head1 Name |
| 25 |
|
25 |
|
| 26 |
=head2 Class Methods |
26 |
Koha::REST::V1::AdvancedEditorMacro |
| 27 |
|
27 |
|
| 28 |
=cut |
28 |
=head1 API |
|
|
29 |
|
| 30 |
=head2 Methods |
| 29 |
|
31 |
|
| 30 |
=head3 list |
32 |
=head3 list |
| 31 |
|
33 |
|
|
Lines 37-55
sub list {
Link Here
|
| 37 |
my $c = shift->openapi->valid_input or return; |
39 |
my $c = shift->openapi->valid_input or return; |
| 38 |
my $patron = $c->stash('koha.user'); |
40 |
my $patron = $c->stash('koha.user'); |
| 39 |
return try { |
41 |
return try { |
| 40 |
my $macros_set = Koha::AdvancedEditorMacros->search({ -or => { shared => 1, borrowernumber => $patron->borrowernumber } }); |
42 |
my $macros_set = Koha::AdvancedEditorMacros->search( |
| 41 |
my $macros = $c->objects->search( $macros_set, \&_to_model, \&_to_api ); |
43 |
{ |
| 42 |
return $c->render( status => 200, openapi => $macros ); |
44 |
-or => |
|
|
45 |
{ shared => 1, borrowernumber => $patron->borrowernumber } |
| 46 |
} |
| 47 |
); |
| 48 |
my $macros = $c->objects->search( $macros_set ); |
| 49 |
return $c->render( |
| 50 |
status => 200, |
| 51 |
openapi => $macros |
| 52 |
); |
| 43 |
} |
53 |
} |
| 44 |
catch { |
54 |
catch { |
| 45 |
if ( $_->isa('DBIx::Class::Exception') ) { |
55 |
$c->unhandled_exception($_); |
| 46 |
return $c->render( status => 500, |
|
|
| 47 |
openapi => { error => $_->{msg} } ); |
| 48 |
} |
| 49 |
else { |
| 50 |
return $c->render( status => 500, |
| 51 |
openapi => { error => "Something went wrong, check the logs. $_"} ); |
| 52 |
} |
| 53 |
}; |
56 |
}; |
| 54 |
|
57 |
|
| 55 |
} |
58 |
} |
|
Lines 123-137
sub add {
Link Here
|
| 123 |
} |
126 |
} |
| 124 |
|
127 |
|
| 125 |
return try { |
128 |
return try { |
| 126 |
my $macro = Koha::AdvancedEditorMacro->new( _to_model( $c->validation->param('body') ) ); |
129 |
my $macro = Koha::AdvancedEditorMacro->new_from_api( $c->validation->param('body') ); |
| 127 |
$macro->store; |
130 |
$macro->store->discard_changes; |
| 128 |
$c->res->headers->location( $c->req->url->to_string . '/' . $macro->id ); |
131 |
$c->res->headers->location( $c->req->url->to_string . '/' . $macro->id ); |
| 129 |
return $c->render( |
132 |
return $c->render( |
| 130 |
status => 201, |
133 |
status => 201, |
| 131 |
openapi => $macro->to_api |
134 |
openapi => $macro->to_api |
| 132 |
); |
135 |
); |
| 133 |
} |
136 |
} |
| 134 |
catch { handle_error($_) }; |
137 |
catch { |
|
|
138 |
$c->unhandled_exception($_); |
| 139 |
}; |
| 135 |
} |
140 |
} |
| 136 |
|
141 |
|
| 137 |
=head3 add_shared |
142 |
=head3 add_shared |
|
Lines 148-162
sub add_shared {
Link Here
|
| 148 |
openapi => { error => "To create private macros you must use advancededitor" } ); |
153 |
openapi => { error => "To create private macros you must use advancededitor" } ); |
| 149 |
} |
154 |
} |
| 150 |
return try { |
155 |
return try { |
| 151 |
my $macro = Koha::AdvancedEditorMacro->new( _to_model( $c->validation->param('body') ) ); |
156 |
my $macro = Koha::AdvancedEditorMacro->new_from_api( $c->validation->param('body') ); |
| 152 |
$macro->store; |
157 |
$macro->store->discard_changes; |
| 153 |
$c->res->headers->location( $c->req->url->to_string . '/' . $macro->id ); |
158 |
$c->res->headers->location( $c->req->url->to_string . '/' . $macro->id ); |
| 154 |
return $c->render( |
159 |
return $c->render( |
| 155 |
status => 201, |
160 |
status => 201, |
| 156 |
openapi => $macro->to_api |
161 |
openapi => $macro->to_api |
| 157 |
); |
162 |
); |
| 158 |
} |
163 |
} |
| 159 |
catch { handle_error($_) }; |
164 |
catch { |
|
|
165 |
$c->unhandled_exception($_); |
| 166 |
}; |
| 160 |
} |
167 |
} |
| 161 |
|
168 |
|
| 162 |
=head3 update |
169 |
=head3 update |
|
Lines 188-198
sub update {
Link Here
|
| 188 |
|
195 |
|
| 189 |
return try { |
196 |
return try { |
| 190 |
my $params = $c->req->json; |
197 |
my $params = $c->req->json; |
| 191 |
$macro->set( _to_model($params) ); |
198 |
$macro->set_from_api( $params ); |
| 192 |
$macro->store(); |
199 |
$macro->store->discard_changes; |
| 193 |
return $c->render( status => 200, openapi => $macro->to_api ); |
200 |
return $c->render( status => 200, openapi => $macro->to_api ); |
| 194 |
} |
201 |
} |
| 195 |
catch { handle_error($_) }; |
202 |
catch { |
|
|
203 |
$c->unhandled_exception($_); |
| 204 |
}; |
| 196 |
} |
205 |
} |
| 197 |
|
206 |
|
| 198 |
=head3 update_shared |
207 |
=head3 update_shared |
|
Lines 218-228
sub update_shared {
Link Here
|
| 218 |
|
227 |
|
| 219 |
return try { |
228 |
return try { |
| 220 |
my $params = $c->req->json; |
229 |
my $params = $c->req->json; |
| 221 |
$macro->set( _to_model($params) ); |
230 |
$macro->set_from_api( $params ); |
| 222 |
$macro->store(); |
231 |
$macro->store->discard_changes; |
| 223 |
return $c->render( status => 200, openapi => $macro->to_api ); |
232 |
return $c->render( status => 200, openapi => $macro->to_api ); |
| 224 |
} |
233 |
} |
| 225 |
catch { handle_error($_) }; |
234 |
catch { |
|
|
235 |
$c->unhandled_exception($_); |
| 236 |
}; |
| 226 |
} |
237 |
} |
| 227 |
|
238 |
|
| 228 |
=head3 delete |
239 |
=head3 delete |
|
Lines 253-261
sub delete {
Link Here
|
| 253 |
|
264 |
|
| 254 |
return try { |
265 |
return try { |
| 255 |
$macro->delete; |
266 |
$macro->delete; |
| 256 |
return $c->render( status => 200, openapi => "" ); |
267 |
return $c->render( status => 204, openapi => q{} ); |
| 257 |
} |
268 |
} |
| 258 |
catch { handle_error($_) }; |
269 |
catch { |
|
|
270 |
$c->unhandled_exception($_); |
| 271 |
}; |
| 259 |
} |
272 |
} |
| 260 |
|
273 |
|
| 261 |
=head3 delete_shared |
274 |
=head3 delete_shared |
|
Lines 280-393
sub delete_shared {
Link Here
|
| 280 |
|
293 |
|
| 281 |
return try { |
294 |
return try { |
| 282 |
$macro->delete; |
295 |
$macro->delete; |
| 283 |
return $c->render( status => 200, openapi => "" ); |
296 |
return $c->render( status => 204, openapi => q{} ); |
| 284 |
} |
|
|
| 285 |
catch { handle_error($_,$c) }; |
| 286 |
} |
| 287 |
|
| 288 |
=head3 _handle_error |
| 289 |
|
| 290 |
Helper function that passes exception or error |
| 291 |
|
| 292 |
=cut |
| 293 |
|
| 294 |
sub _handle_error { |
| 295 |
my ($err,$c) = @_; |
| 296 |
if ( $err->isa('DBIx::Class::Exception') ) { |
| 297 |
return $c->render( status => 500, |
| 298 |
openapi => { error => $err->{msg} } ); |
| 299 |
} |
| 300 |
else { |
| 301 |
return $c->render( status => 500, |
| 302 |
openapi => { error => "Something went wrong, check the logs."} ); |
| 303 |
} |
| 304 |
}; |
| 305 |
|
| 306 |
|
| 307 |
=head3 _to_api |
| 308 |
|
| 309 |
Helper function that maps a hashref of Koha::AdvancedEditorMacro attributes into REST api |
| 310 |
attribute names. |
| 311 |
|
| 312 |
=cut |
| 313 |
|
| 314 |
sub _to_api { |
| 315 |
my $macro = shift; |
| 316 |
|
| 317 |
# Rename attributes |
| 318 |
foreach my $column ( keys %{ $Koha::REST::V1::AdvancedEditorMacro::to_api_mapping } ) { |
| 319 |
my $mapped_column = $Koha::REST::V1::AdvancedEditorMacro::to_api_mapping->{$column}; |
| 320 |
if ( exists $macro->{ $column } |
| 321 |
&& defined $mapped_column ) |
| 322 |
{ |
| 323 |
# key /= undef |
| 324 |
$macro->{ $mapped_column } = delete $macro->{ $column }; |
| 325 |
} |
| 326 |
elsif ( exists $macro->{ $column } |
| 327 |
&& !defined $mapped_column ) |
| 328 |
{ |
| 329 |
# key == undef => to be deleted |
| 330 |
delete $macro->{ $column }; |
| 331 |
} |
| 332 |
} |
| 333 |
|
| 334 |
return $macro; |
| 335 |
} |
| 336 |
|
| 337 |
=head3 _to_model |
| 338 |
|
| 339 |
Helper function that maps REST api objects into Koha::AdvancedEditorMacros |
| 340 |
attribute names. |
| 341 |
|
| 342 |
=cut |
| 343 |
|
| 344 |
sub _to_model { |
| 345 |
my $macro = shift; |
| 346 |
|
| 347 |
foreach my $attribute ( keys %{ $Koha::REST::V1::AdvancedEditorMacro::to_model_mapping } ) { |
| 348 |
my $mapped_attribute = $Koha::REST::V1::AdvancedEditorMacro::to_model_mapping->{$attribute}; |
| 349 |
if ( exists $macro->{ $attribute } |
| 350 |
&& defined $mapped_attribute ) |
| 351 |
{ |
| 352 |
# key /= undef |
| 353 |
$macro->{ $mapped_attribute } = delete $macro->{ $attribute }; |
| 354 |
} |
| 355 |
elsif ( exists $macro->{ $attribute } |
| 356 |
&& !defined $mapped_attribute ) |
| 357 |
{ |
| 358 |
# key == undef => to be deleted |
| 359 |
delete $macro->{ $attribute }; |
| 360 |
} |
| 361 |
} |
297 |
} |
| 362 |
|
298 |
catch { |
| 363 |
if ( exists $macro->{shared} ) { |
299 |
$c->unhandled_exception($_); |
| 364 |
$macro->{shared} = ($macro->{shared}) ? 1 : 0; |
300 |
}; |
| 365 |
} |
|
|
| 366 |
|
| 367 |
|
| 368 |
return $macro; |
| 369 |
} |
301 |
} |
| 370 |
|
302 |
|
| 371 |
=head2 Global variables |
|
|
| 372 |
|
| 373 |
=head3 $to_api_mapping |
| 374 |
|
| 375 |
=cut |
| 376 |
|
| 377 |
our $to_api_mapping = { |
| 378 |
id => 'macro_id', |
| 379 |
macro => 'macro_text', |
| 380 |
borrowernumber => 'patron_id', |
| 381 |
}; |
| 382 |
|
| 383 |
=head3 $to_model_mapping |
| 384 |
|
| 385 |
=cut |
| 386 |
|
| 387 |
our $to_model_mapping = { |
| 388 |
macro_id => 'id', |
| 389 |
macro_text => 'macro', |
| 390 |
patron_id => 'borrowernumber', |
| 391 |
}; |
| 392 |
|
| 393 |
1; |
303 |
1; |