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; |