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

(-)a/Koha/REST/V1/Items.pm (+53 lines)
Lines 27-32 use Koha::Items; Link Here
27
27
28
use Try::Tiny;
28
use Try::Tiny;
29
29
30
=head1 NAME
31
32
Koha::REST::V1::Items - Koha REST API for handling items (V1)
33
34
=head1 API
35
36
=head2 Methods
37
38
=cut
39
40
=head3 list
41
42
Controller function that handles listing Koha::Item objects
43
44
=cut
45
46
sub list {
47
    my $c = shift->openapi->valid_input or return;
48
49
    return try {
50
        my $items_set = Koha::Items->new;
51
        my $items     = $c->objects->search( $items_set, \&_to_model, \&_to_api );
52
        return $c->render(
53
            status  => 200,
54
            openapi => $items
55
        );
56
    }
57
    catch {
58
        unless ( blessed $_ && $_->can('rethrow') ) {
59
            return $c->render(
60
                status  => 500,
61
                openapi => {
62
                    error =>
63
                      "Something went wrong, check Koha logs for details."
64
                }
65
            );
66
        }
67
        return $c->render(
68
            status  => 500,
69
            openapi => { error => "$_" }
70
        );
71
    };
72
}
73
74
75
=head3 get
76
77
Controller function that handles retrieving a single Koha::Item
78
79
=cut
80
30
sub get {
81
sub get {
31
    my $c = shift->openapi->valid_input or return;
82
    my $c = shift->openapi->valid_input or return;
32
83
Lines 51-56 sub get { Link Here
51
    };
102
    };
52
}
103
}
53
104
105
=head2 Internal methods
106
54
=head3 _to_api
107
=head3 _to_api
55
108
56
Helper function that maps unblessed Koha::Hold objects into REST api
109
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