Bug 32030: ERM - Package - Koha classes
[koha.git] / Koha / REST / V1 / ERM / Packages.pm
1 package Koha::REST::V1::ERM::Packages;
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::ERM::Packages;
23
24 use Scalar::Util qw( blessed );
25 use Try::Tiny qw( catch try );
26
27 =head1 API
28
29 =head2 Methods
30
31 =head3 list
32
33 =cut
34
35 sub list {
36     my $c = shift->openapi->valid_input or return;
37
38     return try {
39         my $packages_set = Koha::ERM::Packages->new;
40         my $packages = $c->objects->search( $packages_set );
41         return $c->render( status => 200, openapi => $packages );
42     }
43     catch {
44         $c->unhandled_exception($_);
45     };
46
47 }
48
49 =head3 get
50
51 Controller function that handles retrieving a single Koha::ERM::Package object
52
53 =cut
54
55 sub get {
56     my $c = shift->openapi->valid_input or return;
57
58     return try {
59         my $package_id = $c->validation->param('package_id');
60         my $package    = $c->objects->find( Koha::ERM::Packages->search, $package_id );
61
62         unless ($package) {
63             return $c->render(
64                 status  => 404,
65                 openapi => { error => "Package not found" }
66             );
67         }
68
69         return $c->render(
70             status  => 200,
71             openapi => $package
72         );
73     }
74     catch {
75         $c->unhandled_exception($_);
76     };
77 }
78
79 =head3 add
80
81 Controller function that handles adding a new Koha::ERM::Package object
82
83 =cut
84
85 sub add {
86     my $c = shift->openapi->valid_input or return;
87
88     return try {
89         Koha::Database->new->schema->txn_do(
90             sub {
91
92                 my $body = $c->validation->param('body');
93
94                 my $package = Koha::ERM::Package->new_from_api($body)->store;
95
96                 $c->res->headers->location($c->req->url->to_string . '/' . $package->package_id);
97                 return $c->render(
98                     status  => 201,
99                     openapi => $package->to_api
100                 );
101             }
102         );
103     }
104     catch {
105
106         my $to_api_mapping = Koha::ERM::Package->new->to_api_mapping;
107
108         if ( blessed $_ ) {
109             if ( $_->isa('Koha::Exceptions::Object::DuplicateID') ) {
110                 return $c->render(
111                     status  => 409,
112                     openapi => { error => $_->error, conflict => $_->duplicate_id }
113                 );
114             }
115             elsif ( $_->isa('Koha::Exceptions::Object::FKConstraint') ) {
116                 return $c->render(
117                     status  => 400,
118                     openapi => {
119                             error => "Given "
120                             . $to_api_mapping->{ $_->broken_fk }
121                             . " does not exist"
122                     }
123                 );
124             }
125             elsif ( $_->isa('Koha::Exceptions::BadParameter') ) {
126                 return $c->render(
127                     status  => 400,
128                     openapi => {
129                             error => "Given "
130                             . $to_api_mapping->{ $_->parameter }
131                             . " does not exist"
132                     }
133                 );
134             }
135         }
136
137         $c->unhandled_exception($_);
138     };
139 }
140
141 =head3 update
142
143 Controller function that handles updating a Koha::ERM::Package object
144
145 =cut
146
147 sub update {
148     my $c = shift->openapi->valid_input or return;
149
150     my $package_id = $c->validation->param('package_id');
151     my $package = Koha::ERM::Packages->find( $package_id );
152
153     unless ($package) {
154         return $c->render(
155             status  => 404,
156             openapi => { error => "Package not found" }
157         );
158     }
159
160     return try {
161         Koha::Database->new->schema->txn_do(
162             sub {
163
164                 my $body = $c->validation->param('body');
165
166                 $package->set_from_api($body)->store;
167
168                 $c->res->headers->location($c->req->url->to_string . '/' . $package->package_id);
169                 return $c->render(
170                     status  => 200,
171                     openapi => $package->to_api
172                 );
173             }
174         );
175     }
176     catch {
177         my $to_api_mapping = Koha::ERM::Package->new->to_api_mapping;
178
179         if ( blessed $_ ) {
180             if ( $_->isa('Koha::Exceptions::Object::FKConstraint') ) {
181                 return $c->render(
182                     status  => 400,
183                     openapi => {
184                             error => "Given "
185                             . $to_api_mapping->{ $_->broken_fk }
186                             . " does not exist"
187                     }
188                 );
189             }
190             elsif ( $_->isa('Koha::Exceptions::BadParameter') ) {
191                 return $c->render(
192                     status  => 400,
193                     openapi => {
194                             error => "Given "
195                             . $to_api_mapping->{ $_->parameter }
196                             . " does not exist"
197                     }
198                 );
199             }
200         }
201
202         $c->unhandled_exception($_);
203     };
204 };
205
206 =head3 delete
207
208 =cut
209
210 sub delete {
211     my $c = shift->openapi->valid_input or return;
212
213     my $package = Koha::ERM::Packages->find( $c->validation->param('package_id') );
214     unless ($package) {
215         return $c->render(
216             status  => 404,
217             openapi => { error => "Package not found" }
218         );
219     }
220
221     return try {
222         $package->delete;
223         return $c->render(
224             status  => 204,
225             openapi => q{}
226         );
227     }
228     catch {
229         $c->unhandled_exception($_);
230     };
231 }
232
233 1;