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

(-)a/Koha/REST/V1/Items.pm (-165 / +1 lines)
Lines 44-50 sub list { Link Here
44
44
45
    return try {
45
    return try {
46
        my $items_set = Koha::Items->new;
46
        my $items_set = Koha::Items->new;
47
        my $items     = $c->objects->search( $items_set, \&_to_model, \&_to_api );
47
        my $items     = $c->objects->search( $items_set );
48
        return $c->render(
48
        return $c->render(
49
            status  => 200,
49
            status  => 200,
50
            openapi => $items
50
            openapi => $items
Lines 98-265 sub get { Link Here
98
    };
98
    };
99
}
99
}
100
100
101
=head2 Internal methods
102
103
=head3 _to_api
104
105
Helper function that maps unblessed Koha::Hold objects into REST api
106
attribute names.
107
108
=cut
109
110
sub _to_api {
111
    my $item = shift;
112
113
    # Rename attributes
114
    foreach my $column ( keys %{ $Koha::REST::V1::Items::to_api_mapping } ) {
115
        my $mapped_column = $Koha::REST::V1::Items::to_api_mapping->{$column};
116
        if (    exists $item->{ $column }
117
             && defined $mapped_column )
118
        {
119
            # key != undef
120
            $item->{ $mapped_column } = delete $item->{ $column };
121
        }
122
        elsif (    exists $item->{ $column }
123
                && !defined $mapped_column )
124
        {
125
            # key == undef
126
            delete $item->{ $column };
127
        }
128
    }
129
130
    return $item;
131
}
132
133
=head3 _to_model
134
135
Helper function that maps REST api objects into Koha::Hold
136
attribute names.
137
138
=cut
139
140
sub _to_model {
141
    my $item = shift;
142
143
    foreach my $attribute ( keys %{ $Koha::REST::V1::Items::to_model_mapping } ) {
144
        my $mapped_attribute = $Koha::REST::V1::Items::to_model_mapping->{$attribute};
145
        if (    exists $item->{ $attribute }
146
             && defined $mapped_attribute )
147
        {
148
            # key => !undef
149
            $item->{ $mapped_attribute } = delete $item->{ $attribute };
150
        }
151
        elsif (    exists $item->{ $attribute }
152
                && !defined $mapped_attribute )
153
        {
154
            # key => undef / to be deleted
155
            delete $item->{ $attribute };
156
        }
157
    }
158
159
    return $item;
160
}
161
162
=head2 Global variables
163
164
=head3 $to_api_mapping
165
166
=cut
167
168
our $to_api_mapping = {
169
    itemnumber => 'item_id',
170
    biblionumber => 'biblio_id',
171
    biblioitemnumber => undef,
172
    barcode => 'external_id',
173
    dateaccessioned => 'acquisition_date',
174
    booksellerid => 'acquisition_source',
175
    homebranch => 'home_library_id',
176
    price => 'purchase_price',
177
    replacementprice => 'replacement_price',
178
    replacementpricedate => 'replacement_price_date',
179
    datelastborrowed => 'last_checkout_date',
180
    datelastseen => 'last_seen_date',
181
    stack => undef,
182
    notforloan => 'not_for_loan_status',
183
    damaged => 'damaged_status',
184
    damaged_on => 'damaged_date',
185
    itemlost => 'lost_status',
186
    itemlost_on => 'lost_date',
187
    withdrawn => 'withdrawn',
188
    withdrawn_on => 'withdrawn_date',
189
    itemcallnumber => 'callnumber',
190
    coded_location_qualifier => 'coded_location_qualifier',
191
    issues => 'checkouts_count',
192
    renewals => 'renewals_count',
193
    reserves => 'holds_count',
194
    restricted => 'restricted_status',
195
    itemnotes => 'public_notes',
196
    itemnotes_nonpublic => 'internal_notes',
197
    holdingbranch => 'holding_library_id',
198
    paidfor => undef,
199
    timestamp => 'timestamp',
200
    location => 'location',
201
    permanent_location => 'permanent_location',
202
    onloan => 'checked_out_date',
203
    cn_source => 'call_number_source',
204
    cn_sort => 'call_number_sort',
205
    ccode => 'collection_code',
206
    materials => 'materials_notes',
207
    uri => 'uri',
208
    itype => 'item_type',
209
    more_subfields_xml => 'extended_subfields',
210
    enumchron => 'serial_issue_number',
211
    copynumber => 'copy_number',
212
    stocknumber => 'inventory_number',
213
    new_status => 'new_status'
214
};
215
216
=head3 $to_model_mapping
217
218
=cut
219
220
our $to_model_mapping = {
221
    item_id => 'itemnumber',
222
    biblio_id => 'biblionumber',
223
    external_id => 'barcode',
224
    acquisition_date => 'dateaccessioned',
225
    acquisition_source => 'booksellerid',
226
    home_library_id => 'homebranch',
227
    purchase_price => 'price',
228
    replacement_price => 'replacementprice',
229
    replacement_price_date => 'replacementpricedate',
230
    last_checkout_date => 'datelastborrowed',
231
    last_seen_date => 'datelastseen',
232
    not_for_loan_status => 'notforloan',
233
    damaged_status => 'damaged',
234
    damaged_date => 'damaged_on',
235
    lost_status => 'itemlost',
236
    lost_date => 'itemlost_on',
237
    withdrawn => 'withdrawn',
238
    withdrawn_date => 'withdrawn_on',
239
    callnumber => 'itemcallnumber',
240
    coded_location_qualifier => 'coded_location_qualifier',
241
    checkouts_count => 'issues',
242
    renewals_count => 'renewals',
243
    holds_count => 'reserves',
244
    restricted_status => 'restricted',
245
    public_notes => 'itemnotes',
246
    internal_notes => 'itemnotes_nonpublic',
247
    holding_library_id => 'holdingbranch',
248
    timestamp => 'timestamp',
249
    location => 'location',
250
    permanent_location => 'permanent_location',
251
    checked_out_date => 'onloan',
252
    call_number_source => 'cn_source',
253
    call_number_sort => 'cn_sort',
254
    collection_code => 'ccode',
255
    materials_notes => 'materials',
256
    uri => 'uri',
257
    item_type => 'itype',
258
    extended_subfields => 'more_subfields_xml',
259
    serial_issue_number => 'enumchron',
260
    copy_number => 'copynumber',
261
    inventory_number => 'stocknumber',
262
    new_status => 'new_status'
263
};
264
265
1;
101
1;
(-)a/t/db_dependent/api/v1/items.t (-3 / +2 lines)
Lines 82-88 subtest 'list() tests' => sub { Link Here
82
82
83
    $t->get_ok( "//$userid:$password@/api/v1/items?external_id=" . $item->barcode )
83
    $t->get_ok( "//$userid:$password@/api/v1/items?external_id=" . $item->barcode )
84
      ->status_is(200)
84
      ->status_is(200)
85
      ->json_is( '' => [ Koha::REST::V1::Items::_to_api( $item->TO_JSON ) ], 'SWAGGER3.3.2');
85
      ->json_is( '' => [ $item->to_api ], 'SWAGGER3.3.2');
86
86
87
    my $barcode = $item->barcode;
87
    my $barcode = $item->barcode;
88
    $item->delete;
88
    $item->delete;
Lines 126-132 subtest 'get() tests' => sub { Link Here
126
126
127
    $t->get_ok( "//$userid:$password@/api/v1/items/" . $item->itemnumber )
127
    $t->get_ok( "//$userid:$password@/api/v1/items/" . $item->itemnumber )
128
      ->status_is( 200, 'SWAGGER3.2.2' )
128
      ->status_is( 200, 'SWAGGER3.2.2' )
129
      ->json_is( '' => Koha::REST::V1::Items::_to_api( $item->TO_JSON ), 'SWAGGER3.3.2' );
129
      ->json_is( '' => $item->to_api, 'SWAGGER3.3.2' );
130
130
131
    my $non_existent_code = $item->itemnumber;
131
    my $non_existent_code = $item->itemnumber;
132
    $item->delete;
132
    $item->delete;
133
- 

Return to bug 24321