Revert "Bug 22026: Removed 'use Modern::Perl;' from Koha::REST::classes"
[srvgit] / Koha / REST / V1 / Acquisitions / Vendors.pm
1 package Koha::REST::V1::Acquisitions::Vendors;
2
3 # This file is part of Koha.
4 #
5 # Koha is free software; you can redistribute it and/or modify it under the
6 # terms of the GNU General Public License as published by the Free Software
7 # Foundation; either version 3 of the License, or (at your option) any later
8 # version.
9 #
10 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
11 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License along
15 # with Koha; if not, write to the Free Software Foundation, Inc.,
16 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17
18 use Modern::Perl;
19
20 use Mojo::Base 'Mojolicious::Controller';
21
22 use Koha::Acquisition::Booksellers;
23
24 use Try::Tiny;
25
26 =head1 NAME
27
28 Koha::REST::V1::Acquisitions::Vendors
29
30 =head1 API
31
32 =head2 Methods
33
34 =head3 list_vendors
35
36 Controller function that handles listing Koha::Acquisition::Bookseller objects
37
38 =cut
39
40 sub list_vendors {
41     my $c = shift->openapi->valid_input or return;
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     my @vendors;
52
53     return try {
54         @vendors = Koha::Acquisition::Booksellers->search($filter);
55         @vendors = map { _to_api($_->TO_JSON) } @vendors;
56         return $c->render( status  => 200,
57                            openapi => \@vendors );
58     }
59     catch {
60         if ( $_->isa('DBIx::Class::Exception') ) {
61             return $c->render( status  => 500,
62                                openapi => { error => $_->{msg} } );
63         }
64         else {
65             return $c->render( status  => 500,
66                                openapi => { error => "Something went wrong, check the logs." } );
67         }
68     };
69 }
70
71 =head3 get_vendor
72
73 Controller function that handles retrieving a single Koha::Acquisition::Bookseller
74
75 =cut
76
77 sub get_vendor {
78     my $c = shift->openapi->valid_input or return;
79
80     my $vendor = Koha::Acquisition::Booksellers->find( $c->validation->param('vendor_id') );
81     unless ($vendor) {
82         return $c->render( status  => 404,
83                            openapi => { error => "Vendor not found" } );
84     }
85
86     return $c->render( status  => 200,
87                        openapi => _to_api($vendor->TO_JSON) );
88 }
89
90 =head3 add_vendor
91
92 Controller function that handles adding a new Koha::Acquisition::Bookseller object
93
94 =cut
95
96 sub add_vendor {
97     my $c = shift->openapi->valid_input or return;
98
99     my $vendor = Koha::Acquisition::Bookseller->new( _to_model( $c->validation->param('body') ) );
100
101     return try {
102         $vendor->store;
103         return $c->render( status  => 200,
104                            openapi => _to_api($vendor->TO_JSON) );
105     }
106     catch {
107         if ( $_->isa('DBIx::Class::Exception') ) {
108             return $c->render( status  => 500,
109                                openapi => { error => $_->msg } );
110         }
111         else {
112             return $c->render( status  => 500,
113                                openapi => { error => "Something went wrong, check the logs." } );
114         }
115     };
116 }
117
118 =head3 update_vendor
119
120 Controller function that handles updating a Koha::Acquisition::Bookseller object
121
122 =cut
123
124 sub update_vendor {
125     my $c = shift->openapi->valid_input or return;
126
127     my $vendor;
128
129     return try {
130         $vendor = Koha::Acquisition::Booksellers->find( $c->validation->param('vendor_id') );
131         $vendor->set( _to_model( $c->validation->param('body') ) );
132         $vendor->store();
133         return $c->render( status  => 200,
134                            openapi => _to_api($vendor->TO_JSON) );
135     }
136     catch {
137         if ( not defined $vendor ) {
138             return $c->render( status  => 404,
139                                openapi => { error => "Object not found" } );
140         }
141         elsif ( $_->isa('Koha::Exceptions::Object') ) {
142             return $c->render( status  => 500,
143                                openapi => { error => $_->message } );
144         }
145         else {
146             return $c->render( status  => 500,
147                                openapi => { error => "Something went wrong, check the logs." } );
148         }
149     };
150
151 }
152
153 =head3 delete_vendor
154
155 Controller function that handles deleting a Koha::Acquisition::Bookseller object
156
157 =cut
158
159 sub delete_vendor {
160     my $c = shift->openapi->valid_input or return;
161
162     my $vendor;
163
164     return try {
165         $vendor = Koha::Acquisition::Booksellers->find( $c->validation->param('vendor_id') );
166         $vendor->delete;
167         return $c->render( status => 200,
168                            openapi => q{} );
169     }
170     catch {
171         if ( not defined $vendor ) {
172             return $c->render( status  => 404,
173                                openapi => { error => "Object not found" } );
174         }
175         elsif ( $_->isa('DBIx::Class::Exception') ) {
176             return $c->render( status  => 500,
177                                openapi => { error => $_->msg } );
178         }
179         else {
180             return $c->render( status  => 500,
181                                openapi => { error => "Something went wrong, check the logs." } );
182         }
183     };
184
185 }
186
187 =head3 _to_api
188
189 Helper function that maps a Koha::Acquisition::Bookseller object into
190 the attribute names the exposed REST api spec.
191
192 =cut
193
194 sub _to_api {
195     my $vendor = shift;
196
197     # Delete unused fields
198     delete $vendor->{booksellerfax};
199     delete $vendor->{bookselleremail};
200     delete $vendor->{booksellerurl};
201     delete $vendor->{currency};
202     delete $vendor->{othersupplier};
203
204     # Rename changed fields
205     $vendor->{list_currency}        = delete $vendor->{listprice};
206     $vendor->{invoice_currency}     = delete $vendor->{invoiceprice};
207     $vendor->{gst}                  = delete $vendor->{gstreg};
208     $vendor->{list_includes_gst}    = delete $vendor->{listincgst};
209     $vendor->{invoice_includes_gst} = delete $vendor->{invoiceincgst};
210
211     return $vendor;
212 }
213
214 =head3 _to_model
215
216 Helper function that maps REST api objects into Koha::Acquisition::Bookseller
217 attribute names.
218
219 =cut
220
221 sub _to_model {
222     my $vendor = shift;
223
224     # Rename back
225     $vendor->{listprice}     = delete $vendor->{list_currency};
226     $vendor->{invoiceprice}  = delete $vendor->{invoice_currency};
227     $vendor->{gstreg}        = delete $vendor->{gst};
228     $vendor->{listincgst}    = delete $vendor->{list_includes_gst};
229     $vendor->{invoiceincgst} = delete $vendor->{invoice_includes_gst};
230
231     return $vendor;
232 }
233
234 1;