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

(-)a/Koha/REST/V1/Biblios.pm (+62 lines)
Lines 24-29 use Koha::Ratings; Link Here
24
use Koha::RecordProcessor;
24
use Koha::RecordProcessor;
25
use C4::Biblio qw( DelBiblio );
25
use C4::Biblio qw( DelBiblio );
26
26
27
use C4::Barcodes::ValueBuilder;
28
use C4::Context;
29
30
use Koha::Items;
31
use Koha::Item;
32
27
use List::MoreUtils qw( any );
33
use List::MoreUtils qw( any );
28
use MARC::Record::MiJ;
34
use MARC::Record::MiJ;
29
35
Lines 270-275 sub get_items { Link Here
270
    };
276
    };
271
}
277
}
272
278
279
=head3 add_item
280
281
Controller function that handles creating a biblio's item
282
283
=cut
284
285
sub add_item {
286
    my $c = shift->openapi->valid_input or return;
287
288
    try {
289
        my $biblio_id = $c->validation->param('biblio_id');
290
        my $biblio = Koha::Biblios->find( $biblio_id );
291
292
        unless ($biblio) {
293
            return $c->render(
294
                status  => 404,
295
                openapi => { error => "Biblio not found" }
296
            );
297
        }
298
299
        my $body = $c->validation->param('body');
300
301
        $body->{biblio_id} = $biblio_id;
302
303
        # Don't save extended subfields yet. To be done in another bug.
304
        $body->{extended_subfields} = undef;
305
306
        my $item = Koha::Item->new_from_api($body);
307
308
        if ( ! defined $item->barcode && C4::Context->preference('autoBarcode') eq 'incremental' ) {
309
            my ( $barcode ) = C4::Barcodes::ValueBuilder::incremental::get_barcode;
310
            $item->barcode($barcode);
311
        }
312
313
        if ( defined $item->barcode
314
            && Koha::Items->search( { barcode => $item->barcode } )->count )
315
        {
316
            return $c->render(
317
                status  => 400,
318
                openapi => { error => "Barcode not unique" }
319
            );
320
        }
321
322
        my $storedItem = $item->store;
323
        $storedItem->discard_changes;
324
325
        $c->render(
326
            status => 201,
327
            openapi => $storedItem->to_api
328
        );
329
    }
330
    catch {
331
        $c->unhandled_exception($_);
332
    }
333
}
334
273
=head3 get_checkouts
335
=head3 get_checkouts
274
336
275
List Koha::Checkout objects
337
List Koha::Checkout objects
(-)a/api/v1/swagger/definitions/item.yaml (-7 lines)
Lines 229-238 properties: Link Here
229
      - "null"
229
      - "null"
230
    description: A return claims object if one exists that's unresolved
230
    description: A return claims object if one exists that's unresolved
231
additionalProperties: false
231
additionalProperties: false
232
required:
233
  - item_id
234
  - biblio_id
235
  - not_for_loan_status
236
  - damaged_status
237
  - lost_status
238
  - withdrawn
(-)a/api/v1/swagger/paths/biblios.yaml (+51 lines)
Lines 217-222 Link Here
217
    x-koha-authorization:
217
    x-koha-authorization:
218
      permissions:
218
      permissions:
219
        catalogue: "1"
219
        catalogue: "1"
220
  post:
221
    x-mojo-to: Biblios#add_item
222
    operationId: addBiblioItem
223
    tags:
224
      - biblios
225
    summary: Add an item for a biblio
226
    parameters:
227
      - $ref: "../swagger.yaml#/parameters/biblio_id_pp"
228
      - name: body
229
        in: body
230
        description: A JSON object containing information about the new item
231
        required: true
232
        schema:
233
          $ref: "../swagger.yaml#/definitions/item"
234
    produces:
235
      - application/json
236
    responses:
237
      "201":
238
        description: Item added
239
        schema:
240
          $ref: "../swagger.yaml#/definitions/item"
241
      "400":
242
        description: Bad request
243
        schema:
244
          $ref: "../swagger.yaml#/definitions/error"
245
      "401":
246
        description: Authentication required
247
        schema:
248
          $ref: "../swagger.yaml#/definitions/error"
249
      "403":
250
        description: Access forbidden
251
        schema:
252
          $ref: "../swagger.yaml#/definitions/error"
253
      "404":
254
        description: Not found
255
        schema:
256
          $ref: "../swagger.yaml#/definitions/error"
257
      "500":
258
        description: |
259
          Internal server error. Possible `error_code` attribute values:
260
261
          * `internal_server_error`
262
        schema:
263
          $ref: "../swagger.yaml#/definitions/error"
264
      "503":
265
        description: Under maintenance
266
        schema:
267
          $ref: "../swagger.yaml#/definitions/error"
268
    x-koha-authorization:
269
      permissions:
270
        editcatalogue: edit_catalogue
220
"/biblios/{biblio_id}/pickup_locations":
271
"/biblios/{biblio_id}/pickup_locations":
221
  get:
272
  get:
222
    x-mojo-to: Biblios#pickup_locations
273
    x-mojo-to: Biblios#pickup_locations
(-)a/t/db_dependent/api/v1/biblios.t (-2 / +50 lines)
Lines 20-26 use Modern::Perl; Link Here
20
use utf8;
20
use utf8;
21
use Encode;
21
use Encode;
22
22
23
use Test::More tests => 8;
23
use Test::More tests => 9;
24
use Test::MockModule;
24
use Test::MockModule;
25
use Test::Mojo;
25
use Test::Mojo;
26
use Test::Warn;
26
use Test::Warn;
Lines 713-715 subtest 'set_rating() tests' => sub { Link Here
713
    $schema->storage->txn_rollback;
713
    $schema->storage->txn_rollback;
714
714
715
};
715
};
716
- 
716
717
subtest 'add_item() tests' => sub {
718
  plan tests => 5;
719
720
  $schema->storage->txn_begin;
721
722
  my $patron = $builder->build_object(
723
      {
724
          class => 'Koha::Patrons',
725
          value => { flags => 0 }
726
      }
727
  );
728
  my $password = 'thePassword123';
729
  $patron->set_password( { password => $password, skip_validation => 1 } );
730
  my $userid = $patron->userid;
731
732
  my $biblio = $builder->build_sample_biblio();
733
  my $biblio_id = $biblio->biblionumber;
734
735
  my $barcode = 'mybarcode';
736
  my $matching_items = Koha::Items->search({ barcode => $barcode });
737
738
  while (my $item = $matching_items->next) {
739
    $item->delete;
740
  }
741
742
  $t->post_ok("//$userid:$password@/api/v1/biblios/$biblio_id/items" => json => { external_id => $barcode })
743
    ->status_is(403, 'Not enough permissions to create an item');
744
745
  # Add permissions
746
  $builder->build(
747
      {
748
          source => 'UserPermission',
749
          value  => {
750
              borrowernumber => $patron->borrowernumber,
751
              module_bit     => 9,
752
              code           => 'edit_catalogue'
753
          }
754
      }
755
  );
756
757
  $t->post_ok("//$userid:$password@/api/v1/biblios/$biblio_id/items" => json => {
758
      external_id => $barcode,
759
    })
760
    ->status_is(201, 'Item created')
761
    ->json_is('/biblio_id', $biblio_id);
762
763
  $schema->storage->txn_rollback;
764
};

Return to bug 31798