Line 0
Link Here
|
|
|
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 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::Orders; |
23 |
|
24 |
use Try::Tiny; |
25 |
|
26 |
sub list_orders { |
27 |
|
28 |
my $c = shift->openapi->valid_input or return; |
29 |
|
30 |
return try { |
31 |
my $orders = $c->objects->search( Koha::Acquisition::Orders->new ); |
32 |
|
33 |
return $c->render( |
34 |
status => 200, |
35 |
openapi => $orders |
36 |
); |
37 |
} |
38 |
catch { |
39 |
if ( $_->isa('DBIx::Class::Exception') ) { |
40 |
return $c->render( |
41 |
status => 500, |
42 |
openapi => { error => $_->{msg} } |
43 |
); |
44 |
} |
45 |
else { |
46 |
return $c->render( |
47 |
status => 500, |
48 |
openapi => { error => "Something went wrong, check the logs." } |
49 |
); |
50 |
} |
51 |
}; |
52 |
} |
53 |
|
54 |
sub get_order { |
55 |
my $c = shift->openapi->valid_input or return; |
56 |
|
57 |
my $order = Koha::Acquisition::Orders->find( $c->validation->param('ordernumber') ); |
58 |
unless ($order) { |
59 |
return $c->render( |
60 |
status => 404, |
61 |
openapi => { error => "Order not found" } |
62 |
); |
63 |
} |
64 |
|
65 |
return $c->render( |
66 |
status => 200, |
67 |
openapi => $order |
68 |
); |
69 |
} |
70 |
|
71 |
sub add_order { |
72 |
my $c = shift->openapi->valid_input or return; |
73 |
|
74 |
my $order = Koha::Acquisition::Order->new( $c->validation->param('body') ); |
75 |
|
76 |
return try { |
77 |
$order->store; |
78 |
return $c->render( |
79 |
status => 200, |
80 |
openapi => $order |
81 |
); |
82 |
} |
83 |
catch { |
84 |
if ( $_->isa('DBIx::Class::Exception') ) { |
85 |
return $c->render( |
86 |
status => 500, |
87 |
openapi => { error => $_->msg } |
88 |
); |
89 |
} |
90 |
else { |
91 |
return $c->render( |
92 |
status => 500, |
93 |
openapi => { error => "Something went wrong, check the logs." } |
94 |
); |
95 |
} |
96 |
}; |
97 |
} |
98 |
|
99 |
sub update_order { |
100 |
my $c = shift->openapi->valid_input or return; |
101 |
|
102 |
my $order; |
103 |
|
104 |
return try { |
105 |
$order = Koha::Acquisition::Orders->find( $c->validation->param('ordernumber') ); |
106 |
$order->set( $c->validation->param('body') ); |
107 |
$order->store(); |
108 |
return $c->render( |
109 |
status => 200, |
110 |
openapi => $order |
111 |
); |
112 |
} |
113 |
catch { |
114 |
if ( not defined $order ) { |
115 |
return $c->render( |
116 |
status => 404, |
117 |
openapi => { error => "Object not found" } |
118 |
); |
119 |
} |
120 |
elsif ( $_->isa('Koha::Exceptions::Object') ) { |
121 |
return $c->render( |
122 |
status => 500, |
123 |
openapi => { error => $_->message } |
124 |
); |
125 |
} |
126 |
else { |
127 |
return $c->render( |
128 |
status => 500, |
129 |
openapi => { error => "Something went wrong, check the logs." } |
130 |
); |
131 |
} |
132 |
}; |
133 |
} |
134 |
|
135 |
sub delete_order { |
136 |
my $c = shift->openapi->valid_input or return; |
137 |
|
138 |
my $order; |
139 |
|
140 |
return try { |
141 |
$order = Koha::Acquisition::Orders->find( $c->validation->param('ordernumber') ); |
142 |
$order->delete; |
143 |
return $c->render( |
144 |
status => 200, |
145 |
openapi => q{} |
146 |
); |
147 |
} |
148 |
catch { |
149 |
if ( not defined $order ) { |
150 |
return $c->render( |
151 |
status => 404, |
152 |
openapi => { error => "Object not found" } |
153 |
); |
154 |
} |
155 |
elsif ( $_->isa('DBIx::Class::Exception') ) { |
156 |
return $c->render( |
157 |
status => 500, |
158 |
openapi => { error => $_->msg } |
159 |
); |
160 |
} |
161 |
else { |
162 |
return $c->render( |
163 |
status => 500, |
164 |
openapi => { error => "Something went wrong, check the logs." } |
165 |
); |
166 |
} |
167 |
}; |
168 |
} |
169 |
|
170 |
1; |