Bug 24545: Fix license statements
[koha.git] / Koha / REST / V1 / Acquisitions / Orders.pm
1 package Koha::REST::V1::Acquisitions::Orders;
2
3 # This file is part of Koha.
4 #
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18 use Modern::Perl;
19
20 use Mojo::Base 'Mojolicious::Controller';
21
22 use Koha::Acquisition::Orders;
23 use Koha::DateUtils;
24
25 use Scalar::Util qw( blessed );
26 use Try::Tiny;
27
28 =head1 NAME
29
30 Koha::REST::V1::Acquisitions::Orders
31
32 =head1 API
33
34 =head2 Methods
35
36 =head3 list
37
38 Controller function that handles listing Koha::Acquisition::Order objects
39
40 =cut
41
42 sub list {
43
44     my $c = shift->openapi->valid_input or return;
45
46     return try {
47         my $orders = $c->objects->search( Koha::Acquisition::Orders->new );
48
49         return $c->render(
50             status  => 200,
51             openapi => $orders
52         );
53     }
54     catch {
55         if ( $_->isa('Koha::Exceptions::Exception') ) {
56             return $c->render(
57                 status  => 500,
58                 openapi => { error => "Unhandled exception ($_)" }
59             );
60         }
61         else {
62             return $c->render(
63                 status  => 500,
64                 openapi => { error => "Something went wrong, check the logs. ($_)" }
65             );
66         }
67     };
68 }
69
70 =head3 get
71
72 Controller function that handles retrieving a single Koha::Acquisition::Order object
73
74 =cut
75
76 sub get {
77     my $c = shift->openapi->valid_input or return;
78
79     my $order = Koha::Acquisition::Orders->find( $c->validation->param('order_id') );
80
81     unless ($order) {
82         return $c->render(
83             status  => 404,
84             openapi => { error => "Order not found" }
85         );
86     }
87
88     my $embed = $c->stash('koha.embed');
89
90     return $c->render(
91         status  => 200,
92         openapi => $order->to_api({ embed => $embed })
93     );
94 }
95
96 =head3 add
97
98 Controller function that handles adding a new Koha::Acquisition::Order object
99
100 =cut
101
102 sub add {
103     my $c = shift->openapi->valid_input or return;
104
105     return try {
106         my $order = Koha::Acquisition::Order->new_from_api( $c->validation->param('body') );
107         $order->store->discard_changes;
108
109         $c->res->headers->location(
110             $c->req->url->to_string . '/' . $order->ordernumber
111         );
112
113         return $c->render(
114             status  => 201,
115             openapi => $order->to_api
116         );
117     }
118     catch {
119         unless ( blessed $_ && $_->can('rethrow') ) {
120             return $c->render(
121                 status  => 500,
122                 openapi => {
123                     error =>
124                       "Something went wrong, check Koha logs for details."
125                 }
126             );
127         }
128         if ( $_->isa('Koha::Exceptions::Object::DuplicateID') ) {
129             return $c->render(
130                 status  => 409,
131                 openapi => { error => $_->error, conflict => $_->duplicate_id }
132             );
133         }
134         else {
135             return $c->render(
136                 status  => 500,
137                 openapi => { error => "$_" }
138             );
139         }
140     };
141 }
142
143 =head3 update
144
145 Controller function that handles updating a Koha::Acquisition::Order object
146
147 =cut
148
149 sub update {
150     my $c = shift->openapi->valid_input or return;
151
152     my $order = Koha::Acquisition::Orders->find( $c->validation->param('order_id') );
153
154     unless ($order) {
155         return $c->render(
156             status  => 404,
157             openapi => { error => "Order not found" }
158         );
159     }
160
161     return try {
162         $order->set_from_api( $c->validation->param('body') );
163         $order->store()->discard_changes;
164
165         return $c->render(
166             status  => 200,
167             openapi => $order->to_api
168         );
169     }
170     catch {
171         if ( $_->isa('Koha::Exceptions::Exception') ) {
172             return $c->render(
173                 status  => 500,
174                 openapi => { error => "Unhandled exception ($_)" }
175             );
176         }
177         else {
178             return $c->render(
179                 status  => 500,
180                 openapi => { error => "Something went wrong, check the logs." }
181             );
182         }
183     };
184 }
185
186 =head3 delete
187
188 Controller function that handles deleting a Koha::Patron object
189
190 =cut
191
192 sub delete {
193     my $c = shift->openapi->valid_input or return;
194
195     my $order = Koha::Acquisition::Orders->find( $c->validation->param('order_id') );
196
197     unless ($order) {
198         return $c->render(
199             status  => 404,
200             openapi => { error => 'Order not found' }
201         );
202     }
203
204     return try {
205
206         $order->delete;
207
208         return $c->render(
209             status  => 204,
210             openapi => q{}
211         );
212     }
213     catch {
214         if ( $_->isa('Koha::Exceptions::Exception') ) {
215             return $c->render(
216                 status  => 500,
217                 openapi => { error => "Unhandled exception ($_)" }
218             );
219         }
220         else {
221             return $c->render(
222                 status  => 500,
223                 openapi => { error => "Something went wrong, check the logs." }
224             );
225         }
226     };
227 }
228
229 1;