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

(-)a/Koha/REST/V1/Biblios.pm (+68 lines)
Lines 394-399 sub add_item { Link Here
394
    }
394
    }
395
}
395
}
396
396
397
=head3 update_item
398
399
Controller function that handles updating a biblio's item
400
401
=cut
402
403
sub update_item {
404
    my $c = shift->openapi->valid_input or return;
405
406
    try {
407
        my $biblio_id = $c->validation->param('biblio_id');
408
        my $item_id = $c->validation->param('item_id');
409
        my $biblio = Koha::Biblios->find({ biblionumber => $biblio_id });
410
        unless ($biblio) {
411
            return $c->render(
412
                status  => 404,
413
                openapi => { error => "Biblio not found" }
414
            );
415
        }
416
417
        my $item = $biblio->items->find({ itemnumber => $item_id });
418
419
        unless ($item) {
420
            return $c->render(
421
                status  => 404,
422
                openapi => { error => "Item not found" }
423
            );
424
        }
425
426
        my $body = $c->validation->param('body');
427
428
        $body->{biblio_id} = $biblio_id;
429
430
        # Don't save extended subfields yet. To be done in another bug.
431
        $body->{extended_subfields} = undef;
432
433
        $item->set_from_api($body);
434
435
        my $barcodeSearch;
436
        $barcodeSearch = Koha::Items->search( { barcode => $body->{external_id} } ) if defined $body->{external_id};
437
438
        if ( $barcodeSearch
439
            && ($barcodeSearch->count > 1
440
                || ($barcodeSearch->count == 1
441
                    && $barcodeSearch->next->itemnumber != $item->itemnumber
442
                )
443
            )
444
        )
445
        {
446
            return $c->render(
447
                status  => 400,
448
                openapi => { error => "Barcode not unique" }
449
            );
450
        }
451
452
        my $storedItem = $item->store;
453
        $storedItem->discard_changes;
454
455
        $c->render(
456
            status => 200,
457
            openapi => $storedItem->to_api
458
        );
459
    }
460
    catch {
461
        $c->unhandled_exception($_);
462
    }
463
}
464
397
=head3 get_checkouts
465
=head3 get_checkouts
398
466
399
List Koha::Checkout objects
467
List Koha::Checkout objects
(-)a/api/v1/swagger/paths/biblios.yaml (+53 lines)
Lines 455-460 Link Here
455
    x-koha-authorization:
455
    x-koha-authorization:
456
      permissions:
456
      permissions:
457
        editcatalogue: edit_catalogue
457
        editcatalogue: edit_catalogue
458
"/biblios/{biblio_id}/items/{item_id}":
459
  put:
460
    x-mojo-to: Biblios#update_item
461
    operationId: updateBiblioItem
462
    tags:
463
      - biblios
464
    summary: Update an item for a biblio
465
    parameters:
466
      - $ref: "../swagger.yaml#/parameters/biblio_id_pp"
467
      - $ref: "../swagger.yaml#/parameters/item_id_pp"
468
      - name: body
469
        in: body
470
        description: A JSON object containing information about the item
471
        required: true
472
        schema:
473
          $ref: "../swagger.yaml#/definitions/item"
474
    produces:
475
      - application/json
476
    responses:
477
      "200":
478
        description: Item updated
479
        schema:
480
          $ref: "../swagger.yaml#/definitions/item"
481
      "400":
482
        description: Bad request
483
        schema:
484
          $ref: "../swagger.yaml#/definitions/error"
485
      "401":
486
        description: Authentication required
487
        schema:
488
          $ref: "../swagger.yaml#/definitions/error"
489
      "403":
490
        description: Access forbidden
491
        schema:
492
          $ref: "../swagger.yaml#/definitions/error"
493
      "404":
494
        description: Not found
495
        schema:
496
          $ref: "../swagger.yaml#/definitions/error"
497
      "500":
498
        description: |
499
          Internal server error. Possible `error_code` attribute values:
500
501
          * `internal_server_error`
502
        schema:
503
          $ref: "../swagger.yaml#/definitions/error"
504
      "503":
505
        description: Under maintenance
506
        schema:
507
          $ref: "../swagger.yaml#/definitions/error"
508
    x-koha-authorization:
509
      permissions:
510
        editcatalogue: edit_catalogue
458
"/biblios/{biblio_id}/pickup_locations":
511
"/biblios/{biblio_id}/pickup_locations":
459
  get:
512
  get:
460
    x-mojo-to: Biblios#pickup_locations
513
    x-mojo-to: Biblios#pickup_locations
(-)a/api/v1/swagger/swagger.yaml (+2 lines)
Lines 161-166 paths: Link Here
161
    $ref: "./paths/biblios.yaml#/~1biblios~1{biblio_id}~1checkouts"
161
    $ref: "./paths/biblios.yaml#/~1biblios~1{biblio_id}~1checkouts"
162
  "/biblios/{biblio_id}/items":
162
  "/biblios/{biblio_id}/items":
163
    $ref: "./paths/biblios.yaml#/~1biblios~1{biblio_id}~1items"
163
    $ref: "./paths/biblios.yaml#/~1biblios~1{biblio_id}~1items"
164
  "/biblios/{biblio_id}/items/{item_id}":
165
    $ref: "./paths/biblios.yaml#/~1biblios~1{biblio_id}~1items~1{item_id}"
164
  "/biblios/{biblio_id}/pickup_locations":
166
  "/biblios/{biblio_id}/pickup_locations":
165
    $ref: "./paths/biblios.yaml#/~1biblios~1{biblio_id}~1pickup_locations"
167
    $ref: "./paths/biblios.yaml#/~1biblios~1{biblio_id}~1pickup_locations"
166
  "/biblios/{biblio_id}/item_groups":
168
  "/biblios/{biblio_id}/item_groups":
(-)a/t/db_dependent/api/v1/biblios.t (-2 / +59 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 => 12;
23
use Test::More tests => 13;
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 1724-1726 subtest 'add_item() tests' => sub { Link Here
1724
1724
1725
  $schema->storage->txn_rollback;
1725
  $schema->storage->txn_rollback;
1726
};
1726
};
1727
- 
1727
1728
subtest 'update_item() tests' => sub {
1729
  plan tests => 7;
1730
1731
  $schema->storage->txn_begin;
1732
1733
  my $patron = $builder->build_object(
1734
      {
1735
          class => 'Koha::Patrons',
1736
          value => { flags => 0 }
1737
      }
1738
  );
1739
  my $password = 'thePassword123';
1740
  $patron->set_password( { password => $password, skip_validation => 1 } );
1741
  my $userid = $patron->userid;
1742
1743
  my $item = $builder->build_sample_item({ replacementprice => 5 });
1744
  my $biblio_id = $item->biblionumber;
1745
  my $item_id = $item->itemnumber;
1746
1747
  my $biblio = Koha::Biblios->find($item->biblionumber);
1748
1749
  my $matching_items = Koha::Items->search({ barcode => $item->barcode });
1750
1751
  while (my $mbcitem = $matching_items->next) {
1752
    $mbcitem->delete if $mbcitem->biblionumber != $item->biblionumber;
1753
  }
1754
1755
  $t->put_ok("//$userid:$password@/api/v1/biblios/$biblio_id/items/$item_id" => json => { external_id => 'something' })
1756
    ->status_is(403, 'Not enough permissions to update an item');
1757
1758
  # Add permissions
1759
  $builder->build(
1760
      {
1761
          source => 'UserPermission',
1762
          value  => {
1763
              borrowernumber => $patron->borrowernumber,
1764
              module_bit     => 9,
1765
              code           => 'edit_catalogue'
1766
          }
1767
      }
1768
  );
1769
1770
  my $other_item = $builder->build_sample_item();
1771
1772
  $t->put_ok("//$userid:$password@/api/v1/biblios/$biblio_id/items/$item_id" => json => {
1773
      external_id => $other_item->barcode,
1774
    })
1775
    ->status_is(400, 'Barcode not unique');
1776
1777
  $t->put_ok("//$userid:$password@/api/v1/biblios/$biblio_id/items/$item_id" => json => {
1778
      replacement_price => 30,
1779
    })
1780
    ->status_is(200, 'Item updated')
1781
    ->json_is('/replacement_price', 30);
1782
1783
  $schema->storage->txn_rollback;
1784
};

Return to bug 31799