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

(-)a/Koha/REST/V1/Acquisitions/Vendors.pm (-69 / +15 lines)
Lines 31-58 Koha::REST::V1::Acquisitions::Vendors Link Here
31
31
32
=head2 Methods
32
=head2 Methods
33
33
34
=head3 list_vendors
34
=head3 list
35
35
36
Controller function that handles listing Koha::Acquisition::Bookseller objects
36
Controller function that handles listing Koha::Acquisition::Bookseller objects
37
37
38
=cut
38
=cut
39
39
40
sub list_vendors {
40
sub list {
41
    my $c = shift->openapi->valid_input or return;
41
    my $c = shift->openapi->valid_input or return;
42
42
43
    my $args = _to_model($c->req->params->to_hash);
44
    my $filter;
45
46
    for my $filter_param ( keys %$args ) {
47
        $filter->{$filter_param} = { LIKE => $args->{$filter_param} . "%" }
48
            if $args->{$filter_param};
49
    }
50
51
    return try {
43
    return try {
52
        my $vendors = Koha::Acquisition::Booksellers->search($filter);
44
        my $vendors_rs = Koha::Acquisition::Booksellers->new;
45
        my $vendors    = $c->objects->search( $vendors_rs );
53
        return $c->render(
46
        return $c->render(
54
            status  => 200,
47
            status  => 200,
55
            openapi => $vendors->to_api
48
            openapi => $vendors
56
        );
49
        );
57
    }
50
    }
58
    catch {
51
    catch {
Lines 67-79 sub list_vendors { Link Here
67
    };
60
    };
68
}
61
}
69
62
70
=head3 get_vendor
63
=head3 get
71
64
72
Controller function that handles retrieving a single Koha::Acquisition::Bookseller
65
Controller function that handles retrieving a single Koha::Acquisition::Bookseller
73
66
74
=cut
67
=cut
75
68
76
sub get_vendor {
69
sub get {
77
    my $c = shift->openapi->valid_input or return;
70
    my $c = shift->openapi->valid_input or return;
78
71
79
    my $vendor = Koha::Acquisition::Booksellers->find( $c->validation->param('vendor_id') );
72
    my $vendor = Koha::Acquisition::Booksellers->find( $c->validation->param('vendor_id') );
Lines 88-103 sub get_vendor { Link Here
88
    );
81
    );
89
}
82
}
90
83
91
=head3 add_vendor
84
=head3 add
92
85
93
Controller function that handles adding a new Koha::Acquisition::Bookseller object
86
Controller function that handles adding a new Koha::Acquisition::Bookseller object
94
87
95
=cut
88
=cut
96
89
97
sub add_vendor {
90
sub add {
98
    my $c = shift->openapi->valid_input or return;
91
    my $c = shift->openapi->valid_input or return;
99
92
100
    my $vendor = Koha::Acquisition::Bookseller->new( _to_model( $c->validation->param('body') ) );
93
    my $vendor = Koha::Acquisition::Bookseller->new_from_api( $c->validation->param('body') );
101
94
102
    return try {
95
    return try {
103
        $vendor->store;
96
        $vendor->store;
Lines 119-138 sub add_vendor { Link Here
119
    };
112
    };
120
}
113
}
121
114
122
=head3 update_vendor
115
=head3 update
123
116
124
Controller function that handles updating a Koha::Acquisition::Bookseller object
117
Controller function that handles updating a Koha::Acquisition::Bookseller object
125
118
126
=cut
119
=cut
127
120
128
sub update_vendor {
121
sub update {
129
    my $c = shift->openapi->valid_input or return;
122
    my $c = shift->openapi->valid_input or return;
130
123
131
    my $vendor;
124
    my $vendor;
132
125
133
    return try {
126
    return try {
134
        $vendor = Koha::Acquisition::Booksellers->find( $c->validation->param('vendor_id') );
127
        $vendor = Koha::Acquisition::Booksellers->find( $c->validation->param('vendor_id') );
135
        $vendor->set( _to_model( $c->validation->param('body') ) );
128
        $vendor->set_from_api( $c->validation->param('body') );
136
        $vendor->store();
129
        $vendor->store();
137
        return $c->render(
130
        return $c->render(
138
            status  => 200,
131
            status  => 200,
Lines 156-168 sub update_vendor { Link Here
156
149
157
}
150
}
158
151
159
=head3 delete_vendor
152
=head3 delete
160
153
161
Controller function that handles deleting a Koha::Acquisition::Bookseller object
154
Controller function that handles deleting a Koha::Acquisition::Bookseller object
162
155
163
=cut
156
=cut
164
157
165
sub delete_vendor {
158
sub delete {
166
    my $c = shift->openapi->valid_input or return;
159
    my $c = shift->openapi->valid_input or return;
167
160
168
    my $vendor;
161
    my $vendor;
Lines 190-240 sub delete_vendor { Link Here
190
183
191
}
184
}
192
185
193
=head3 _to_api
194
195
Helper function that maps a Koha::Acquisition::Bookseller object into
196
the attribute names the exposed REST api spec.
197
198
=cut
199
200
sub _to_api {
201
    my $vendor = shift;
202
203
    # Delete unused fields
204
    delete $vendor->{booksellerfax};
205
    delete $vendor->{bookselleremail};
206
    delete $vendor->{booksellerurl};
207
    delete $vendor->{currency};
208
    delete $vendor->{othersupplier};
209
210
    # Rename changed fields
211
    $vendor->{list_currency}        = delete $vendor->{listprice};
212
    $vendor->{invoice_currency}     = delete $vendor->{invoiceprice};
213
    $vendor->{gst}                  = delete $vendor->{gstreg};
214
    $vendor->{list_includes_gst}    = delete $vendor->{listincgst};
215
    $vendor->{invoice_includes_gst} = delete $vendor->{invoiceincgst};
216
217
    return $vendor;
218
}
219
220
=head3 _to_model
221
222
Helper function that maps REST api objects into Koha::Acquisition::Bookseller
223
attribute names.
224
225
=cut
226
227
sub _to_model {
228
    my $vendor = shift;
229
230
    # Rename back
231
    $vendor->{listprice}     = delete $vendor->{list_currency};
232
    $vendor->{invoiceprice}  = delete $vendor->{invoice_currency};
233
    $vendor->{gstreg}        = delete $vendor->{gst};
234
    $vendor->{listincgst}    = delete $vendor->{list_includes_gst};
235
    $vendor->{invoiceincgst} = delete $vendor->{invoice_includes_gst};
236
237
    return $vendor;
238
}
239
240
1;
186
1;
(-)a/api/v1/swagger/paths/acquisitions_vendors.json (-5 / +13 lines)
Lines 1-7 Link Here
1
{
1
{
2
  "/acquisitions/vendors": {
2
  "/acquisitions/vendors": {
3
    "get": {
3
    "get": {
4
      "x-mojo-to": "Acquisitions::Vendors#list_vendors",
4
      "x-mojo-to": "Acquisitions::Vendors#list",
5
      "operationId": "listVendors",
5
      "operationId": "listVendors",
6
      "tags": ["acquisitions","vendors"],
6
      "tags": ["acquisitions","vendors"],
7
      "produces": [
7
      "produces": [
Lines 19-24 Link Here
19
        "description": "Case insensitive search on vendor's account number",
19
        "description": "Case insensitive search on vendor's account number",
20
        "required": false,
20
        "required": false,
21
        "type": "string"
21
        "type": "string"
22
      }, {
23
        "$ref": "../parameters.json#/match"
24
      }, {
25
        "$ref": "../parameters.json#/order_by"
26
      }, {
27
        "$ref": "../parameters.json#/page"
28
      }, {
29
        "$ref": "../parameters.json#/per_page"
22
      }],
30
      }],
23
      "responses": {
31
      "responses": {
24
        "200": {
32
        "200": {
Lines 68-74 Link Here
68
      }
76
      }
69
    },
77
    },
70
    "post": {
78
    "post": {
71
      "x-mojo-to": "Acquisitions::Vendors#add_vendor",
79
      "x-mojo-to": "Acquisitions::Vendors#add",
72
      "operationId": "addVendor",
80
      "operationId": "addVendor",
73
      "tags": ["acquisitions","vendors"],
81
      "tags": ["acquisitions","vendors"],
74
      "parameters": [{
82
      "parameters": [{
Lines 130-136 Link Here
130
  },
138
  },
131
  "/acquisitions/vendors/{vendor_id}": {
139
  "/acquisitions/vendors/{vendor_id}": {
132
    "get": {
140
    "get": {
133
      "x-mojo-to": "Acquisitions::Vendors#get_vendor",
141
      "x-mojo-to": "Acquisitions::Vendors#get",
134
      "operationId": "getVendor",
142
      "operationId": "getVendor",
135
      "tags": ["acquisitions","vendors"],
143
      "tags": ["acquisitions","vendors"],
136
      "parameters": [{
144
      "parameters": [{
Lines 184-190 Link Here
184
      }
192
      }
185
    },
193
    },
186
    "put": {
194
    "put": {
187
      "x-mojo-to": "Acquisitions::Vendors#update_vendor",
195
      "x-mojo-to": "Acquisitions::Vendors#update",
188
      "operationId": "updateVendor",
196
      "operationId": "updateVendor",
189
      "tags": ["acquisitions","vendors"],
197
      "tags": ["acquisitions","vendors"],
190
      "parameters": [{
198
      "parameters": [{
Lines 246-252 Link Here
246
      }
254
      }
247
    },
255
    },
248
    "delete": {
256
    "delete": {
249
      "x-mojo-to": "Acquisitions::Vendors#delete_vendor",
257
      "x-mojo-to": "Acquisitions::Vendors#delete",
250
      "operationId": "deleteVendor",
258
      "operationId": "deleteVendor",
251
      "tags": ["acquisitions","vendors"],
259
      "tags": ["acquisitions","vendors"],
252
      "parameters": [{
260
      "parameters": [{
(-)a/t/db_dependent/api/v1/acquisitions_vendors.t (-2 / +1 lines)
Lines 156-162 subtest 'get() test' => sub { Link Here
156
    $tx->req->env( { REMOTE_ADDR => $remote_address } );
156
    $tx->req->env( { REMOTE_ADDR => $remote_address } );
157
    $t->request_ok($tx)
157
    $t->request_ok($tx)
158
      ->status_is(200)
158
      ->status_is(200)
159
      ->json_is( Koha::REST::V1::Acquisitions::Vendors::_to_api( $vendor->TO_JSON ) );
159
      ->json_is( $vendor->to_api );
160
160
161
    my $non_existent_id = $vendor->id + 1;
161
    my $non_existent_id = $vendor->id + 1;
162
    $tx = $t->ua->build_tx( GET => "/api/v1/acquisitions/vendors/" . $non_existent_id );
162
    $tx = $t->ua->build_tx( GET => "/api/v1/acquisitions/vendors/" . $non_existent_id );
163
- 

Return to bug 24321