From 3ac92ea881c99ee997d42dac483b26db36848b45 Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Tue, 31 Dec 2019 10:50:15 -0300 Subject: [PATCH] Bug 24321: Clean /acquisitions/vendors Signed-off-by: Josef Moravec Signed-off-by: Kyle M Hall Signed-off-by: Martin Renvoize --- Koha/REST/V1/Acquisitions/Vendors.pm | 84 ++++--------------- .../swagger/paths/acquisitions_vendors.json | 18 ++-- t/db_dependent/api/v1/acquisitions_vendors.t | 2 +- 3 files changed, 29 insertions(+), 75 deletions(-) diff --git a/Koha/REST/V1/Acquisitions/Vendors.pm b/Koha/REST/V1/Acquisitions/Vendors.pm index 986b1ab8f6..0cb33fbf28 100644 --- a/Koha/REST/V1/Acquisitions/Vendors.pm +++ b/Koha/REST/V1/Acquisitions/Vendors.pm @@ -31,28 +31,21 @@ Koha::REST::V1::Acquisitions::Vendors =head2 Methods -=head3 list_vendors +=head3 list Controller function that handles listing Koha::Acquisition::Bookseller objects =cut -sub list_vendors { +sub list { my $c = shift->openapi->valid_input or return; - my $args = _to_model($c->req->params->to_hash); - my $filter; - - for my $filter_param ( keys %$args ) { - $filter->{$filter_param} = { LIKE => $args->{$filter_param} . "%" } - if $args->{$filter_param}; - } - return try { - my $vendors = Koha::Acquisition::Booksellers->search($filter); + my $vendors_rs = Koha::Acquisition::Booksellers->new; + my $vendors = $c->objects->search( $vendors_rs ); return $c->render( status => 200, - openapi => $vendors->to_api + openapi => $vendors ); } catch { @@ -67,13 +60,13 @@ sub list_vendors { }; } -=head3 get_vendor +=head3 get Controller function that handles retrieving a single Koha::Acquisition::Bookseller =cut -sub get_vendor { +sub get { my $c = shift->openapi->valid_input or return; my $vendor = Koha::Acquisition::Booksellers->find( $c->validation->param('vendor_id') ); @@ -88,16 +81,16 @@ sub get_vendor { ); } -=head3 add_vendor +=head3 add Controller function that handles adding a new Koha::Acquisition::Bookseller object =cut -sub add_vendor { +sub add { my $c = shift->openapi->valid_input or return; - my $vendor = Koha::Acquisition::Bookseller->new( _to_model( $c->validation->param('body') ) ); + my $vendor = Koha::Acquisition::Bookseller->new_from_api( $c->validation->param('body') ); return try { $vendor->store; @@ -119,20 +112,20 @@ sub add_vendor { }; } -=head3 update_vendor +=head3 update Controller function that handles updating a Koha::Acquisition::Bookseller object =cut -sub update_vendor { +sub update { my $c = shift->openapi->valid_input or return; my $vendor; return try { $vendor = Koha::Acquisition::Booksellers->find( $c->validation->param('vendor_id') ); - $vendor->set( _to_model( $c->validation->param('body') ) ); + $vendor->set_from_api( $c->validation->param('body') ); $vendor->store(); return $c->render( status => 200, @@ -156,13 +149,13 @@ sub update_vendor { } -=head3 delete_vendor +=head3 delete Controller function that handles deleting a Koha::Acquisition::Bookseller object =cut -sub delete_vendor { +sub delete { my $c = shift->openapi->valid_input or return; my $vendor; @@ -190,51 +183,4 @@ sub delete_vendor { } -=head3 _to_api - -Helper function that maps a Koha::Acquisition::Bookseller object into -the attribute names the exposed REST api spec. - -=cut - -sub _to_api { - my $vendor = shift; - - # Delete unused fields - delete $vendor->{booksellerfax}; - delete $vendor->{bookselleremail}; - delete $vendor->{booksellerurl}; - delete $vendor->{currency}; - delete $vendor->{othersupplier}; - - # Rename changed fields - $vendor->{list_currency} = delete $vendor->{listprice}; - $vendor->{invoice_currency} = delete $vendor->{invoiceprice}; - $vendor->{gst} = delete $vendor->{gstreg}; - $vendor->{list_includes_gst} = delete $vendor->{listincgst}; - $vendor->{invoice_includes_gst} = delete $vendor->{invoiceincgst}; - - return $vendor; -} - -=head3 _to_model - -Helper function that maps REST api objects into Koha::Acquisition::Bookseller -attribute names. - -=cut - -sub _to_model { - my $vendor = shift; - - # Rename back - $vendor->{listprice} = delete $vendor->{list_currency}; - $vendor->{invoiceprice} = delete $vendor->{invoice_currency}; - $vendor->{gstreg} = delete $vendor->{gst}; - $vendor->{listincgst} = delete $vendor->{list_includes_gst}; - $vendor->{invoiceincgst} = delete $vendor->{invoice_includes_gst}; - - return $vendor; -} - 1; diff --git a/api/v1/swagger/paths/acquisitions_vendors.json b/api/v1/swagger/paths/acquisitions_vendors.json index f8da32f16a..02952318e1 100644 --- a/api/v1/swagger/paths/acquisitions_vendors.json +++ b/api/v1/swagger/paths/acquisitions_vendors.json @@ -1,7 +1,7 @@ { "/acquisitions/vendors": { "get": { - "x-mojo-to": "Acquisitions::Vendors#list_vendors", + "x-mojo-to": "Acquisitions::Vendors#list", "operationId": "listVendors", "tags": ["acquisitions","vendors"], "produces": [ @@ -19,6 +19,14 @@ "description": "Case insensitive search on vendor's account number", "required": false, "type": "string" + }, { + "$ref": "../parameters.json#/match" + }, { + "$ref": "../parameters.json#/order_by" + }, { + "$ref": "../parameters.json#/page" + }, { + "$ref": "../parameters.json#/per_page" }], "responses": { "200": { @@ -68,7 +76,7 @@ } }, "post": { - "x-mojo-to": "Acquisitions::Vendors#add_vendor", + "x-mojo-to": "Acquisitions::Vendors#add", "operationId": "addVendor", "tags": ["acquisitions","vendors"], "parameters": [{ @@ -130,7 +138,7 @@ }, "/acquisitions/vendors/{vendor_id}": { "get": { - "x-mojo-to": "Acquisitions::Vendors#get_vendor", + "x-mojo-to": "Acquisitions::Vendors#get", "operationId": "getVendor", "tags": ["acquisitions","vendors"], "parameters": [{ @@ -184,7 +192,7 @@ } }, "put": { - "x-mojo-to": "Acquisitions::Vendors#update_vendor", + "x-mojo-to": "Acquisitions::Vendors#update", "operationId": "updateVendor", "tags": ["acquisitions","vendors"], "parameters": [{ @@ -246,7 +254,7 @@ } }, "delete": { - "x-mojo-to": "Acquisitions::Vendors#delete_vendor", + "x-mojo-to": "Acquisitions::Vendors#delete", "operationId": "deleteVendor", "tags": ["acquisitions","vendors"], "parameters": [{ diff --git a/t/db_dependent/api/v1/acquisitions_vendors.t b/t/db_dependent/api/v1/acquisitions_vendors.t index 5ae7367de3..c3ec28728d 100644 --- a/t/db_dependent/api/v1/acquisitions_vendors.t +++ b/t/db_dependent/api/v1/acquisitions_vendors.t @@ -156,7 +156,7 @@ subtest 'get() test' => sub { $tx->req->env( { REMOTE_ADDR => $remote_address } ); $t->request_ok($tx) ->status_is(200) - ->json_is( Koha::REST::V1::Acquisitions::Vendors::_to_api( $vendor->TO_JSON ) ); + ->json_is( $vendor->to_api ); my $non_existent_id = $vendor->id + 1; $tx = $t->ua->build_tx( GET => "/api/v1/acquisitions/vendors/" . $non_existent_id ); -- 2.39.5