From 5b51471e86ca35e41ed37fae79c66f5b9d1c95be Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Mon, 21 Oct 2019 13:29:47 -0300 Subject: [PATCH] Bug 23843: Add mapping to Koha::Acquisition::Bookseller This patch adds a to_api_mapping method to the class. This in effect enables calling ->to_api on the object. The mapping is borrowed from the API controller. It is not removed from the controller so we are able to verify (through the tests) that there is no behavior change. Once this is pushed we need to implement the counter-wise methods and clean the controllers. To test: 1. Run: $ kshell k$ prove t/db_dependent/api/v1/acquisitions_vendors.t => SUCCESS: Tests pass 2. Apply this patch 3. Repeat (1) => SUCCESS: Tests still pass! 4. Sign off :-D --- Koha/Acquisition/Bookseller.pm | 24 ++++++++++++++++++++++- Koha/REST/V1/Acquisitions/Vendors.pm | 29 ++++++++++++++++------------ 2 files changed, 40 insertions(+), 13 deletions(-) diff --git a/Koha/Acquisition/Bookseller.pm b/Koha/Acquisition/Bookseller.pm index f2f08db109..708ae51461 100644 --- a/Koha/Acquisition/Bookseller.pm +++ b/Koha/Acquisition/Bookseller.pm @@ -28,7 +28,7 @@ Koha::Acquisition::Bookseller Object class =head1 API -=head2 Class Methods +=head2 Class methods =head3 baskets @@ -73,6 +73,28 @@ sub subscriptions { return Koha::Subscriptions->search( { aqbooksellerid => $self->id } ); } +=head3 to_api_mapping + +This method returns the mapping for representing a Koha::Acquisition::Bookseller object +on the API. + +=cut + +sub to_api_mapping { + return { + booksellerfax => undef, + bookselleremail => undef, + booksellerurl => undef, + currency => undef, + othersupplier => undef, + listprice => 'list_currency', + invoiceprice => 'invoice_currency', + gstreg => 'gst', + listincgst => 'list_includes_gst', + invoiceincgst => 'invoice_includes_gst' + }; +} + =head2 Internal methods =head3 _type diff --git a/Koha/REST/V1/Acquisitions/Vendors.pm b/Koha/REST/V1/Acquisitions/Vendors.pm index 5f86ac82d4..f5686b0ded 100644 --- a/Koha/REST/V1/Acquisitions/Vendors.pm +++ b/Koha/REST/V1/Acquisitions/Vendors.pm @@ -48,13 +48,12 @@ sub list_vendors { if $args->{$filter_param}; } - my @vendors; - return try { - @vendors = Koha::Acquisition::Booksellers->search($filter); - @vendors = map { _to_api($_->TO_JSON) } @vendors; - return $c->render( status => 200, - openapi => \@vendors ); + my $vendors = Koha::Acquisition::Booksellers->search($filter); + return $c->render( + status => 200, + openapi => $vendors->to_api + ); } catch { if ( $_->isa('DBIx::Class::Exception') ) { @@ -83,8 +82,10 @@ sub get_vendor { openapi => { error => "Vendor not found" } ); } - return $c->render( status => 200, - openapi => _to_api($vendor->TO_JSON) ); + return $c->render( + status => 200, + openapi => $vendor->to_api + ); } =head3 add_vendor @@ -100,8 +101,10 @@ sub add_vendor { return try { $vendor->store; - return $c->render( status => 200, - openapi => _to_api($vendor->TO_JSON) ); + return $c->render( + status => 200, + openapi => $vendor->to_api + ); } catch { if ( $_->isa('DBIx::Class::Exception') ) { @@ -130,8 +133,10 @@ sub update_vendor { $vendor = Koha::Acquisition::Booksellers->find( $c->validation->param('vendor_id') ); $vendor->set( _to_model( $c->validation->param('body') ) ); $vendor->store(); - return $c->render( status => 200, - openapi => _to_api($vendor->TO_JSON) ); + return $c->render( + status => 200, + openapi => $vendor->to_api + ); } catch { if ( not defined $vendor ) { -- 2.23.0