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

(-)a/Koha/REST/V1/Items.pm (+53 lines)
Lines 23-28 use Koha::Items; Link Here
23
23
24
use Try::Tiny;
24
use Try::Tiny;
25
25
26
=head1 NAME
27
28
Koha::REST::V1::Items - Koha REST API for handling items (V1)
29
30
=head1 API
31
32
=head2 Methods
33
34
=cut
35
36
=head3 list
37
38
Controller function that handles listing Koha::Item objects
39
40
=cut
41
42
sub list {
43
    my $c = shift->openapi->valid_input or return;
44
45
    return try {
46
        my $items_set = Koha::Items->new;
47
        my $items     = $c->objects->search( $items_set, \&_to_model, \&_to_api );
48
        return $c->render(
49
            status  => 200,
50
            openapi => $items
51
        );
52
    }
53
    catch {
54
        unless ( blessed $_ && $_->can('rethrow') ) {
55
            return $c->render(
56
                status  => 500,
57
                openapi => {
58
                    error =>
59
                      "Something went wrong, check Koha logs for details."
60
                }
61
            );
62
        }
63
        return $c->render(
64
            status  => 500,
65
            openapi => { error => "$_" }
66
        );
67
    };
68
}
69
70
71
=head3 get
72
73
Controller function that handles retrieving a single Koha::Item
74
75
=cut
76
26
sub get {
77
sub get {
27
    my $c = shift->openapi->valid_input or return;
78
    my $c = shift->openapi->valid_input or return;
28
79
Lines 47-52 sub get { Link Here
47
    };
98
    };
48
}
99
}
49
100
101
=head2 Internal methods
102
50
=head3 _to_api
103
=head3 _to_api
51
104
52
Helper function that maps unblessed Koha::Hold objects into REST api
105
Helper function that maps unblessed Koha::Hold objects into REST api
(-)a/t/db_dependent/api/v1/items.t (-2 / +59 lines)
Lines 19-25 Link Here
19
19
20
use Modern::Perl;
20
use Modern::Perl;
21
21
22
use Test::More tests => 1;
22
use Test::More tests => 2;
23
use Test::Mojo;
23
use Test::Mojo;
24
use Test::Warn;
24
use Test::Warn;
25
25
Lines 37-42 t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 ); Link Here
37
37
38
my $t = Test::Mojo->new('Koha::REST::V1');
38
my $t = Test::Mojo->new('Koha::REST::V1');
39
39
40
subtest 'list() tests' => sub {
41
42
    plan tests => 12;
43
44
    $schema->storage->txn_begin;
45
46
    my $item   = $builder->build_object( { class => 'Koha::Items' } );
47
    my $patron = $builder->build_object(
48
        {
49
            class => 'Koha::Patrons',
50
            value => { flags => 4 }
51
        }
52
    );
53
54
    my $nonprivilegedpatron = $builder->build_object(
55
        {
56
            class => 'Koha::Patrons',
57
            value => { flags => 0 }
58
        }
59
    );
60
61
    my $password = 'thePassword123';
62
63
    $nonprivilegedpatron->set_password(
64
        { password => $password, skip_validation => 1 } );
65
    my $userid = $nonprivilegedpatron->userid;
66
67
    $t->get_ok( "//$userid:$password@/api/v1/items" )
68
      ->status_is(403)
69
      ->json_is(
70
        '/error' => 'Authorization failure. Missing required permission(s).' );
71
72
    $patron->set_password( { password => $password, skip_validation => 1 } );
73
    $userid = $patron->userid;
74
75
    $t->get_ok( "//$userid:$password@/api/v1/items" )
76
      ->status_is( 200, 'SWAGGER3.2.2' );
77
78
    my $items_count = Koha::Items->search->count;
79
    my $response_count = scalar @{ $t->tx->res->json };
80
81
    is( $items_count, $response_count, 'The API returns all the items' );
82
83
    $t->get_ok( "//$userid:$password@/api/v1/items?external_id=" . $item->barcode )
84
      ->status_is(200)
85
      ->json_is( '' => [ Koha::REST::V1::Items::_to_api( $item->TO_JSON ) ], 'SWAGGER3.3.2');
86
87
    my $barcode = $item->barcode;
88
    $item->delete;
89
90
    $t->get_ok( "//$userid:$password@/api/v1/items?external_id=" . $item->barcode )
91
      ->status_is(200)
92
      ->json_is( '' => [] );
93
94
    $schema->storage->txn_rollback;
95
};
96
97
40
subtest 'get() tests' => sub {
98
subtest 'get() tests' => sub {
41
99
42
    plan tests => 9;
100
    plan tests => 9;
43
- 

Return to bug 23667