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

(-)a/Koha/REST/V1/Biblios.pm (+68 lines)
Lines 332-337 sub add_item { Link Here
332
    }
332
    }
333
}
333
}
334
334
335
=head3 update_item
336
337
Controller function that handles updating a biblio's item
338
339
=cut
340
341
sub update_item {
342
    my $c = shift->openapi->valid_input or return;
343
344
    try {
345
        my $biblio_id = $c->validation->param('biblio_id');
346
        my $item_id = $c->validation->param('item_id');
347
        my $biblio = Koha::Biblios->find({ biblionumber => $biblio_id });
348
        unless ($biblio) {
349
            return $c->render(
350
                status  => 404,
351
                openapi => { error => "Biblio not found" }
352
            );
353
        }
354
355
        my $item = $biblio->items->find({ itemnumber => $item_id });
356
357
        unless ($item) {
358
            return $c->render(
359
                status  => 404,
360
                openapi => { error => "Item not found" }
361
            );
362
        }
363
364
        my $body = $c->validation->param('body');
365
366
        $body->{biblio_id} = $biblio_id;
367
368
        # Don't save extended subfields yet. To be done in another bug.
369
        $body->{extended_subfields} = undef;
370
371
        $item->set_from_api($body);
372
373
        my $barcodeSearch;
374
        $barcodeSearch = Koha::Items->search( { barcode => $body->{external_id} } ) if defined $body->{external_id};
375
376
        if ( $barcodeSearch
377
            && ($barcodeSearch->count > 1
378
                || ($barcodeSearch->count == 1
379
                    && $barcodeSearch->next->itemnumber != $item->itemnumber
380
                )
381
            )
382
        )
383
        {
384
            return $c->render(
385
                status  => 400,
386
                openapi => { error => "Barcode not unique" }
387
            );
388
        }
389
390
        my $storedItem = $item->store;
391
        $storedItem->discard_changes;
392
393
        $c->render(
394
            status => 200,
395
            openapi => $storedItem->to_api
396
        );
397
    }
398
    catch {
399
        $c->unhandled_exception($_);
400
    }
401
}
402
335
=head3 get_checkouts
403
=head3 get_checkouts
336
404
337
List Koha::Checkout objects
405
List Koha::Checkout objects
(-)a/api/v1/swagger/paths/biblios.yaml (+53 lines)
Lines 268-273 Link Here
268
    x-koha-authorization:
268
    x-koha-authorization:
269
      permissions:
269
      permissions:
270
        editcatalogue: edit_catalogue
270
        editcatalogue: edit_catalogue
271
"/biblios/{biblio_id}/items/{item_id}":
272
  put:
273
    x-mojo-to: Biblios#update_item
274
    operationId: updateBiblioItem
275
    tags:
276
      - biblios
277
    summary: Update an item for a biblio
278
    parameters:
279
      - $ref: "../swagger.yaml#/parameters/biblio_id_pp"
280
      - $ref: "../swagger.yaml#/parameters/item_id_pp"
281
      - name: body
282
        in: body
283
        description: A JSON object containing information about the item
284
        required: true
285
        schema:
286
          $ref: "../swagger.yaml#/definitions/item"
287
    produces:
288
      - application/json
289
    responses:
290
      "200":
291
        description: Item updated
292
        schema:
293
          $ref: "../swagger.yaml#/definitions/item"
294
      "400":
295
        description: Bad request
296
        schema:
297
          $ref: "../swagger.yaml#/definitions/error"
298
      "401":
299
        description: Authentication required
300
        schema:
301
          $ref: "../swagger.yaml#/definitions/error"
302
      "403":
303
        description: Access forbidden
304
        schema:
305
          $ref: "../swagger.yaml#/definitions/error"
306
      "404":
307
        description: Not found
308
        schema:
309
          $ref: "../swagger.yaml#/definitions/error"
310
      "500":
311
        description: |
312
          Internal server error. Possible `error_code` attribute values:
313
314
          * `internal_server_error`
315
        schema:
316
          $ref: "../swagger.yaml#/definitions/error"
317
      "503":
318
        description: Under maintenance
319
        schema:
320
          $ref: "../swagger.yaml#/definitions/error"
321
    x-koha-authorization:
322
      permissions:
323
        editcatalogue: edit_catalogue
271
"/biblios/{biblio_id}/pickup_locations":
324
"/biblios/{biblio_id}/pickup_locations":
272
  get:
325
  get:
273
    x-mojo-to: Biblios#pickup_locations
326
    x-mojo-to: Biblios#pickup_locations
(-)a/api/v1/swagger/swagger.yaml (+2 lines)
Lines 143-148 paths: Link Here
143
    $ref: "./paths/biblios.yaml#/~1biblios~1{biblio_id}~1checkouts"
143
    $ref: "./paths/biblios.yaml#/~1biblios~1{biblio_id}~1checkouts"
144
  "/biblios/{biblio_id}/items":
144
  "/biblios/{biblio_id}/items":
145
    $ref: "./paths/biblios.yaml#/~1biblios~1{biblio_id}~1items"
145
    $ref: "./paths/biblios.yaml#/~1biblios~1{biblio_id}~1items"
146
  "/biblios/{biblio_id}/items/{item_id}":
147
    $ref: "./paths/biblios.yaml#/~1biblios~1{biblio_id}~1items~1{item_id}"
146
  "/biblios/{biblio_id}/pickup_locations":
148
  "/biblios/{biblio_id}/pickup_locations":
147
    $ref: "./paths/biblios.yaml#/~1biblios~1{biblio_id}~1pickup_locations"
149
    $ref: "./paths/biblios.yaml#/~1biblios~1{biblio_id}~1pickup_locations"
148
  "/biblios/{biblio_id}/item_groups":
150
  "/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 => 9;
23
use Test::More tests => 10;
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 762-764 subtest 'add_item() tests' => sub { Link Here
762
762
763
  $schema->storage->txn_rollback;
763
  $schema->storage->txn_rollback;
764
};
764
};
765
- 
765
766
subtest 'update_item() tests' => sub {
767
  plan tests => 7;
768
769
  $schema->storage->txn_begin;
770
771
  my $patron = $builder->build_object(
772
      {
773
          class => 'Koha::Patrons',
774
          value => { flags => 0 }
775
      }
776
  );
777
  my $password = 'thePassword123';
778
  $patron->set_password( { password => $password, skip_validation => 1 } );
779
  my $userid = $patron->userid;
780
781
  my $item = $builder->build_sample_item({ replacementprice => 5 });
782
  my $biblio_id = $item->biblionumber;
783
  my $item_id = $item->itemnumber;
784
785
  my $biblio = Koha::Biblios->find($item->biblionumber);
786
787
  my $matching_items = Koha::Items->search({ barcode => $item->barcode });
788
789
  while (my $mbcitem = $matching_items->next) {
790
    $mbcitem->delete if $mbcitem->biblionumber != $item->biblionumber;
791
  }
792
793
  $t->put_ok("//$userid:$password@/api/v1/biblios/$biblio_id/items/$item_id" => json => { external_id => 'something' })
794
    ->status_is(403, 'Not enough permissions to update an item');
795
796
  # Add permissions
797
  $builder->build(
798
      {
799
          source => 'UserPermission',
800
          value  => {
801
              borrowernumber => $patron->borrowernumber,
802
              module_bit     => 9,
803
              code           => 'edit_catalogue'
804
          }
805
      }
806
  );
807
808
  my $other_item = $builder->build_sample_item();
809
810
  $t->put_ok("//$userid:$password@/api/v1/biblios/$biblio_id/items/$item_id" => json => {
811
      external_id => $other_item->barcode,
812
    })
813
    ->status_is(400, 'Barcode not unique');
814
815
  $t->put_ok("//$userid:$password@/api/v1/biblios/$biblio_id/items/$item_id" => json => {
816
      replacement_price => 30,
817
    })
818
    ->status_is(200, 'Item updated')
819
    ->json_is('/replacement_price', 30);
820
821
  $schema->storage->txn_rollback;
822
};

Return to bug 31799