Bug 25032: Make existing controllers use unhandled_exception
[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         $c->unhandled_exception($_);
56     };
57 }
58
59 =head3 get
60
61 Controller function that handles retrieving a single Koha::Acquisition::Order object
62
63 =cut
64
65 sub get {
66     my $c = shift->openapi->valid_input or return;
67
68     my $order = Koha::Acquisition::Orders->find( $c->validation->param('order_id') );
69
70     unless ($order) {
71         return $c->render(
72             status  => 404,
73             openapi => { error => "Order not found" }
74         );
75     }
76
77     return try {
78         my $embed = $c->stash('koha.embed');
79
80         return $c->render(
81             status  => 200,
82             openapi => $order->to_api({ embed => $embed })
83         );
84     }
85     catch {
86         $c->unhandled_exception($_);
87     };
88 }
89
90 =head3 add
91
92 Controller function that handles adding a new Koha::Acquisition::Order object
93
94 =cut
95
96 sub add {
97     my $c = shift->openapi->valid_input or return;
98
99     return try {
100         my $order = Koha::Acquisition::Order->new_from_api( $c->validation->param('body') );
101         $order->store->discard_changes;
102
103         $c->res->headers->location(
104             $c->req->url->to_string . '/' . $order->ordernumber
105         );
106
107         return $c->render(
108             status  => 201,
109             openapi => $order->to_api
110         );
111     }
112     catch {
113         if ( blessed $_ and $_->isa('Koha::Exceptions::Object::DuplicateID') ) {
114             return $c->render(
115                 status  => 409,
116                 openapi => { error => $_->error, conflict => $_->duplicate_id }
117             );
118         }
119
120         $c->unhandled_exception($_);
121     };
122 }
123
124 =head3 update
125
126 Controller function that handles updating a Koha::Acquisition::Order object
127
128 =cut
129
130 sub update {
131     my $c = shift->openapi->valid_input or return;
132
133     my $order = Koha::Acquisition::Orders->find( $c->validation->param('order_id') );
134
135     unless ($order) {
136         return $c->render(
137             status  => 404,
138             openapi => { error => "Order not found" }
139         );
140     }
141
142     return try {
143         $order->set_from_api( $c->validation->param('body') );
144         $order->store()->discard_changes;
145
146         return $c->render(
147             status  => 200,
148             openapi => $order->to_api
149         );
150     }
151     catch {
152         $c->unhandled_exception($_);
153     };
154 }
155
156 =head3 delete
157
158 Controller function that handles deleting a Koha::Patron object
159
160 =cut
161
162 sub delete {
163     my $c = shift->openapi->valid_input or return;
164
165     my $order = Koha::Acquisition::Orders->find( $c->validation->param('order_id') );
166
167     unless ($order) {
168         return $c->render(
169             status  => 404,
170             openapi => { error => 'Order not found' }
171         );
172     }
173
174     return try {
175
176         $order->delete;
177
178         return $c->render(
179             status  => 204,
180             openapi => q{}
181         );
182     }
183     catch {
184         $c->unhandled_exception($_);
185     };
186 }
187
188 1;