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

(-)a/Koha/REST/V1/Biblios.pm (+62 lines)
Lines 25-30 use Koha::RecordProcessor; Link Here
25
use C4::Biblio qw( DelBiblio AddBiblio ModBiblio );
25
use C4::Biblio qw( DelBiblio AddBiblio ModBiblio );
26
use C4::Search qw( FindDuplicate );
26
use C4::Search qw( FindDuplicate );
27
27
28
use C4::Barcodes::ValueBuilder;
29
use C4::Context;
30
31
use Koha::Items;
32
use Koha::Item;
33
28
use List::MoreUtils qw( any );
34
use List::MoreUtils qw( any );
29
use MARC::Record::MiJ;
35
use MARC::Record::MiJ;
30
36
Lines 274-279 sub get_items { Link Here
274
    };
280
    };
275
}
281
}
276
282
283
=head3 add_item
284
285
Controller function that handles creating a biblio's item
286
287
=cut
288
289
sub add_item {
290
    my $c = shift->openapi->valid_input or return;
291
292
    try {
293
        my $biblio_id = $c->validation->param('biblio_id');
294
        my $biblio = Koha::Biblios->find( $biblio_id );
295
296
        unless ($biblio) {
297
            return $c->render(
298
                status  => 404,
299
                openapi => { error => "Biblio not found" }
300
            );
301
        }
302
303
        my $body = $c->validation->param('body');
304
305
        $body->{biblio_id} = $biblio_id;
306
307
        # Don't save extended subfields yet. To be done in another bug.
308
        $body->{extended_subfields} = undef;
309
310
        my $item = Koha::Item->new_from_api($body);
311
312
        if ( ! defined $item->barcode && C4::Context->preference('autoBarcode') eq 'incremental' ) {
313
            my ( $barcode ) = C4::Barcodes::ValueBuilder::incremental::get_barcode;
314
            $item->barcode($barcode);
315
        }
316
317
        if ( defined $item->barcode
318
            && Koha::Items->search( { barcode => $item->barcode } )->count )
319
        {
320
            return $c->render(
321
                status  => 400,
322
                openapi => { error => "Barcode not unique" }
323
            );
324
        }
325
326
        my $storedItem = $item->store;
327
        $storedItem->discard_changes;
328
329
        $c->render(
330
            status => 201,
331
            openapi => $storedItem->to_api
332
        );
333
    }
334
    catch {
335
        $c->unhandled_exception($_);
336
    }
337
}
338
277
=head3 get_checkouts
339
=head3 get_checkouts
278
340
279
List Koha::Checkout objects
341
List Koha::Checkout objects
(-)a/api/v1/swagger/definitions/item.yaml (-7 lines)
Lines 233-242 properties: Link Here
233
      - "null"
233
      - "null"
234
    description: A return claims object if one exists that's unresolved
234
    description: A return claims object if one exists that's unresolved
235
additionalProperties: false
235
additionalProperties: false
236
required:
237
  - item_id
238
  - biblio_id
239
  - not_for_loan_status
240
  - damaged_status
241
  - lost_status
242
  - withdrawn
(-)a/api/v1/swagger/paths/biblios.yaml (+51 lines)
Lines 400-405 Link Here
400
    x-koha-authorization:
400
    x-koha-authorization:
401
      permissions:
401
      permissions:
402
        catalogue: "1"
402
        catalogue: "1"
403
  post:
404
    x-mojo-to: Biblios#add_item
405
    operationId: addBiblioItem
406
    tags:
407
      - biblios
408
    summary: Add an item for a biblio
409
    parameters:
410
      - $ref: "../swagger.yaml#/parameters/biblio_id_pp"
411
      - name: body
412
        in: body
413
        description: A JSON object containing information about the new item
414
        required: true
415
        schema:
416
          $ref: "../swagger.yaml#/definitions/item"
417
    produces:
418
      - application/json
419
    responses:
420
      "201":
421
        description: Item added
422
        schema:
423
          $ref: "../swagger.yaml#/definitions/item"
424
      "400":
425
        description: Bad request
426
        schema:
427
          $ref: "../swagger.yaml#/definitions/error"
428
      "401":
429
        description: Authentication required
430
        schema:
431
          $ref: "../swagger.yaml#/definitions/error"
432
      "403":
433
        description: Access forbidden
434
        schema:
435
          $ref: "../swagger.yaml#/definitions/error"
436
      "404":
437
        description: Not found
438
        schema:
439
          $ref: "../swagger.yaml#/definitions/error"
440
      "500":
441
        description: |
442
          Internal server error. Possible `error_code` attribute values:
443
444
          * `internal_server_error`
445
        schema:
446
          $ref: "../swagger.yaml#/definitions/error"
447
      "503":
448
        description: Under maintenance
449
        schema:
450
          $ref: "../swagger.yaml#/definitions/error"
451
    x-koha-authorization:
452
      permissions:
453
        editcatalogue: edit_catalogue
403
"/biblios/{biblio_id}/pickup_locations":
454
"/biblios/{biblio_id}/pickup_locations":
404
  get:
455
  get:
405
    x-mojo-to: Biblios#pickup_locations
456
    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 => 11;
23
use Test::More tests => 12;
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 1688-1690 subtest 'list() tests' => sub { Link Here
1688
1688
1689
    $schema->storage->txn_rollback;
1689
    $schema->storage->txn_rollback;
1690
};
1690
};
1691
- 
1691
1692
subtest 'add_item() tests' => sub {
1693
  plan tests => 5;
1694
1695
  $schema->storage->txn_begin;
1696
1697
  my $patron = $builder->build_object(
1698
      {
1699
          class => 'Koha::Patrons',
1700
          value => { flags => 0 }
1701
      }
1702
  );
1703
  my $password = 'thePassword123';
1704
  $patron->set_password( { password => $password, skip_validation => 1 } );
1705
  my $userid = $patron->userid;
1706
1707
  my $biblio = $builder->build_sample_biblio();
1708
  my $biblio_id = $biblio->biblionumber;
1709
1710
  my $barcode = 'mybarcode';
1711
  my $matching_items = Koha::Items->search({ barcode => $barcode });
1712
1713
  while (my $item = $matching_items->next) {
1714
    $item->delete;
1715
  }
1716
1717
  $t->post_ok("//$userid:$password@/api/v1/biblios/$biblio_id/items" => json => { external_id => $barcode })
1718
    ->status_is(403, 'Not enough permissions to create an item');
1719
1720
  # Add permissions
1721
  $builder->build(
1722
      {
1723
          source => 'UserPermission',
1724
          value  => {
1725
              borrowernumber => $patron->borrowernumber,
1726
              module_bit     => 9,
1727
              code           => 'edit_catalogue'
1728
          }
1729
      }
1730
  );
1731
1732
  $t->post_ok("//$userid:$password@/api/v1/biblios/$biblio_id/items" => json => {
1733
      external_id => $barcode,
1734
    })
1735
    ->status_is(201, 'Item created')
1736
    ->json_is('/biblio_id', $biblio_id);
1737
1738
  $schema->storage->txn_rollback;
1739
};

Return to bug 31798