Bug 24321: Clean /acquisitions/vendors
authorTomas Cohen Arazi <tomascohen@theke.io>
Tue, 31 Dec 2019 13:50:15 +0000 (10:50 -0300)
committerMartin Renvoize <martin.renvoize@ptfs-europe.com>
Wed, 8 Jan 2020 14:42:32 +0000 (14:42 +0000)
Signed-off-by: Josef Moravec <josef.moravec@gmail.com>
Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>

Koha/REST/V1/Acquisitions/Vendors.pm
api/v1/swagger/paths/acquisitions_vendors.json
t/db_dependent/api/v1/acquisitions_vendors.t

index 986b1ab..0cb33fb 100644 (file)
@@ -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;
index f8da32f..0295231 100644 (file)
@@ -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": [
         "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": [{
   },
   "/acquisitions/vendors/{vendor_id}": {
     "get": {
-      "x-mojo-to": "Acquisitions::Vendors#get_vendor",
+      "x-mojo-to": "Acquisitions::Vendors#get",
       "operationId": "getVendor",
       "tags": ["acquisitions","vendors"],
       "parameters": [{
       }
     },
     "put": {
-      "x-mojo-to": "Acquisitions::Vendors#update_vendor",
+      "x-mojo-to": "Acquisitions::Vendors#update",
       "operationId": "updateVendor",
       "tags": ["acquisitions","vendors"],
       "parameters": [{
       }
     },
     "delete": {
-      "x-mojo-to": "Acquisitions::Vendors#delete_vendor",
+      "x-mojo-to": "Acquisitions::Vendors#delete",
       "operationId": "deleteVendor",
       "tags": ["acquisitions","vendors"],
       "parameters": [{
index 5ae7367..c3ec287 100644 (file)
@@ -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 );