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

(-)a/Koha/REST/V1/AdvancedEditorMacro.pm (-135 / +45 lines)
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;
(-)a/api/v1/swagger/paths/advancededitormacros.json (-2 / +2 lines)
Lines 308-314 Link Here
308
        "application/json"
308
        "application/json"
309
      ],
309
      ],
310
      "responses": {
310
      "responses": {
311
        "200": {
311
        "204": {
312
          "description": "Advanced editor macro deleted",
312
          "description": "Advanced editor macro deleted",
313
          "schema": {
313
          "schema": {
314
            "type": "string"
314
            "type": "string"
Lines 477-483 Link Here
477
        "application/json"
477
        "application/json"
478
      ],
478
      ],
479
      "responses": {
479
      "responses": {
480
        "200": {
480
        "204": {
481
          "description": "Advanced editor macro deleted",
481
          "description": "Advanced editor macro deleted",
482
          "schema": {
482
          "schema": {
483
            "type": "string"
483
            "type": "string"
(-)a/t/db_dependent/api/v1/advanced_editor_macros.t (-7 / +6 lines)
Lines 151-157 subtest 'get() tests' => sub { Link Here
151
151
152
    $t->get_ok( "//$userid:$password@/api/v1/advanced_editor/macros/shared/" . $macro_1->id )
152
    $t->get_ok( "//$userid:$password@/api/v1/advanced_editor/macros/shared/" . $macro_1->id )
153
      ->status_is( 200, 'Can get a shared macro via shared endpoint' )
153
      ->status_is( 200, 'Can get a shared macro via shared endpoint' )
154
      ->json_is( '' => Koha::REST::V1::AdvancedEditorMacro::_to_api( $macro_1->TO_JSON ), 'Macro correctly retrieved' );
154
      ->json_is( $macro_1->to_api );
155
155
156
    $t->get_ok( "//$userid:$password@/api/v1/advanced_editor/macros/" . $macro_2->id )
156
    $t->get_ok( "//$userid:$password@/api/v1/advanced_editor/macros/" . $macro_2->id )
157
      ->status_is( 403, 'Cannot access another users macro' )
157
      ->status_is( 403, 'Cannot access another users macro' )
Lines 159-165 subtest 'get() tests' => sub { Link Here
159
159
160
    $t->get_ok( "//$userid:$password@/api/v1/advanced_editor/macros/" . $macro_3->id )
160
    $t->get_ok( "//$userid:$password@/api/v1/advanced_editor/macros/" . $macro_3->id )
161
      ->status_is( 200, 'Can get your own private macro' )
161
      ->status_is( 200, 'Can get your own private macro' )
162
      ->json_is( '' => Koha::REST::V1::AdvancedEditorMacro::_to_api( $macro_3->TO_JSON ), 'Macro correctly retrieved' );
162
      ->json_is( $macro_3->to_api );
163
163
164
    my $non_existent_code = $macro_1->id;
164
    my $non_existent_code = $macro_1->id;
165
    $macro_1->delete;
165
    $macro_1->delete;
Lines 202-208 subtest 'add() tests' => sub { Link Here
202
        class => 'Koha::AdvancedEditorMacros',
202
        class => 'Koha::AdvancedEditorMacros',
203
        value => { shared => 0 }
203
        value => { shared => 0 }
204
    });
204
    });
205
    my $macro_values     = Koha::REST::V1::AdvancedEditorMacro::_to_api( $macro->TO_JSON );
205
    my $macro_values = $macro->to_api;
206
    delete $macro_values->{macro_id};
206
    delete $macro_values->{macro_id};
207
    $macro->delete;
207
    $macro->delete;
208
208
Lines 317-323 subtest 'update() tests' => sub { Link Here
317
    });
317
    });
318
    my $macro_id = $macro->id;
318
    my $macro_id = $macro->id;
319
    my $macro_2_id = $macro_2->id;
319
    my $macro_2_id = $macro_2->id;
320
    my $macro_values     = Koha::REST::V1::AdvancedEditorMacro::_to_api( $macro->TO_JSON );
320
    my $macro_values = $macro->to_api;
321
    delete $macro_values->{macro_id};
321
    delete $macro_values->{macro_id};
322
322
323
    # Unauthorized attempt to update
323
    # Unauthorized attempt to update
Lines 448-454 subtest 'delete() tests' => sub { Link Here
448
      ->status_is(403, "Cannot delete macro without permission");
448
      ->status_is(403, "Cannot delete macro without permission");
449
449
450
    $t->delete_ok( "//$auth_userid:$password@/api/v1/advanced_editor/macros/$macro_id")
450
    $t->delete_ok( "//$auth_userid:$password@/api/v1/advanced_editor/macros/$macro_id")
451
      ->status_is(200, 'Can delete macro with permission');
451
      ->status_is( 204, 'Can delete macro with permission');
452
452
453
    $t->delete_ok( "//$auth_userid:$password@/api/v1/advanced_editor/macros/$macro_2_id")
453
    $t->delete_ok( "//$auth_userid:$password@/api/v1/advanced_editor/macros/$macro_2_id")
454
      ->status_is(403, 'Cannot delete other users macro with permission');
454
      ->status_is(403, 'Cannot delete other users macro with permission');
Lines 469-475 subtest 'delete() tests' => sub { Link Here
469
    $t->delete_ok( "//$auth_userid:$password@/api/v1/advanced_editor/macros/$macro_2_id")
469
    $t->delete_ok( "//$auth_userid:$password@/api/v1/advanced_editor/macros/$macro_2_id")
470
      ->status_is(403, 'Cannot delete other users shared macro with permission on private endpoint');
470
      ->status_is(403, 'Cannot delete other users shared macro with permission on private endpoint');
471
    $t->delete_ok( "//$auth_userid:$password@/api/v1/advanced_editor/macros/shared/$macro_2_id")
471
    $t->delete_ok( "//$auth_userid:$password@/api/v1/advanced_editor/macros/shared/$macro_2_id")
472
      ->status_is(200, 'Can delete other users shared macro with permission');
472
      ->status_is(204, 'Can delete other users shared macro with permission');
473
473
474
};
474
};
475
475
476
- 

Return to bug 25502