Bug 23858: Make POST add the Location header on vendors endpoint
[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     return try {
52         my $vendors = Koha::Acquisition::Booksellers->search($filter);
53         return $c->render(
54             status  => 200,
55             openapi => $vendors->to_api
56         );
57     }
58     catch {
59         if ( $_->isa('DBIx::Class::Exception') ) {
60             return $c->render( status  => 500,
61                                openapi => { error => $_->{msg} } );
62         }
63         else {
64             return $c->render( status  => 500,
65                                openapi => { error => "Something went wrong, check the logs." } );
66         }
67     };
68 }
69
70 =head3 get_vendor
71
72 Controller function that handles retrieving a single Koha::Acquisition::Bookseller
73
74 =cut
75
76 sub get_vendor {
77     my $c = shift->openapi->valid_input or return;
78
79     my $vendor = Koha::Acquisition::Booksellers->find( $c->validation->param('vendor_id') );
80     unless ($vendor) {
81         return $c->render( status  => 404,
82                            openapi => { error => "Vendor not found" } );
83     }
84
85     return $c->render(
86         status  => 200,
87         openapi => $vendor->to_api
88     );
89 }
90
91 =head3 add_vendor
92
93 Controller function that handles adding a new Koha::Acquisition::Bookseller object
94
95 =cut
96
97 sub add_vendor {
98     my $c = shift->openapi->valid_input or return;
99
100     my $vendor = Koha::Acquisition::Bookseller->new( _to_model( $c->validation->param('body') ) );
101
102     return try {
103         $vendor->store;
104         $c->res->headers->location($c->req->url->to_string . '/' . $vendor->id );
105         return $c->render(
106             status  => 200,
107             openapi => $vendor->to_api
108         );
109     }
110     catch {
111         if ( $_->isa('DBIx::Class::Exception') ) {
112             return $c->render( status  => 500,
113                                openapi => { error => $_->msg } );
114         }
115         else {
116             return $c->render( status  => 500,
117                                openapi => { error => "Something went wrong, check the logs." } );
118         }
119     };
120 }
121
122 =head3 update_vendor
123
124 Controller function that handles updating a Koha::Acquisition::Bookseller object
125
126 =cut
127
128 sub update_vendor {
129     my $c = shift->openapi->valid_input or return;
130
131     my $vendor;
132
133     return try {
134         $vendor = Koha::Acquisition::Booksellers->find( $c->validation->param('vendor_id') );
135         $vendor->set( _to_model( $c->validation->param('body') ) );
136         $vendor->store();
137         return $c->render(
138             status  => 200,
139             openapi => $vendor->to_api
140         );
141     }
142     catch {
143         if ( not defined $vendor ) {
144             return $c->render( status  => 404,
145                                openapi => { error => "Object not found" } );
146         }
147         elsif ( $_->isa('Koha::Exceptions::Object') ) {
148             return $c->render( status  => 500,
149                                openapi => { error => $_->message } );
150         }
151         else {
152             return $c->render( status  => 500,
153                                openapi => { error => "Something went wrong, check the logs." } );
154         }
155     };
156
157 }
158
159 =head3 delete_vendor
160
161 Controller function that handles deleting a Koha::Acquisition::Bookseller object
162
163 =cut
164
165 sub delete_vendor {
166     my $c = shift->openapi->valid_input or return;
167
168     my $vendor;
169
170     return try {
171         $vendor = Koha::Acquisition::Booksellers->find( $c->validation->param('vendor_id') );
172         $vendor->delete;
173         return $c->render( status => 200,
174                            openapi => q{} );
175     }
176     catch {
177         if ( not defined $vendor ) {
178             return $c->render( status  => 404,
179                                openapi => { error => "Object not found" } );
180         }
181         elsif ( $_->isa('DBIx::Class::Exception') ) {
182             return $c->render( status  => 500,
183                                openapi => { error => $_->msg } );
184         }
185         else {
186             return $c->render( status  => 500,
187                                openapi => { error => "Something went wrong, check the logs." } );
188         }
189     };
190
191 }
192
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;