Bug 32030: Add documents to license
[koha.git] / Koha / REST / V1 / ERM / Licenses.pm
1 package Koha::REST::V1::ERM::Licenses;
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::Licenses;
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 $licenses_set = Koha::ERM::Licenses->new;
40         my $licenses = $c->objects->search( $licenses_set );
41         return $c->render( status => 200, openapi => $licenses );
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::License object
52
53 =cut
54
55 sub get {
56     my $c = shift->openapi->valid_input or return;
57
58     return try {
59         my $license_id = $c->validation->param('license_id');
60         my $license    = $c->objects->find( Koha::ERM::Licenses->search, $license_id );
61
62         unless ($license) {
63             return $c->render(
64                 status  => 404,
65                 openapi => { error => "License not found" }
66             );
67         }
68
69         return $c->render(
70             status  => 200,
71             openapi => $license
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::License 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 $documents = delete $body->{documents} // [];
95
96                 my $license = Koha::ERM::License->new_from_api($body)->store;
97                 $license->documents($documents);
98
99                 $c->res->headers->location($c->req->url->to_string . '/' . $license->license_id);
100                 return $c->render(
101                     status  => 201,
102                     openapi => $license->to_api
103                 );
104             }
105         );
106     }
107     catch {
108
109         my $to_api_mapping = Koha::ERM::License->new->to_api_mapping;
110
111         if ( blessed $_ ) {
112             if ( $_->isa('Koha::Exceptions::Object::DuplicateID') ) {
113                 return $c->render(
114                     status  => 409,
115                     openapi => { error => $_->error, conflict => $_->duplicate_id }
116                 );
117             }
118             elsif ( $_->isa('Koha::Exceptions::Object::FKConstraint') ) {
119                 return $c->render(
120                     status  => 400,
121                     openapi => {
122                             error => "Given "
123                             . $to_api_mapping->{ $_->broken_fk }
124                             . " does not exist"
125                     }
126                 );
127             }
128             elsif ( $_->isa('Koha::Exceptions::BadParameter') ) {
129                 return $c->render(
130                     status  => 400,
131                     openapi => {
132                             error => "Given "
133                             . $to_api_mapping->{ $_->parameter }
134                             . " does not exist"
135                     }
136                 );
137             }
138         }
139
140         $c->unhandled_exception($_);
141     };
142 }
143
144 =head3 update
145
146 Controller function that handles updating a Koha::ERM::License object
147
148 =cut
149
150 sub update {
151     my $c = shift->openapi->valid_input or return;
152
153     my $license_id = $c->validation->param('license_id');
154     my $license = Koha::ERM::Licenses->find( $license_id );
155
156     unless ($license) {
157         return $c->render(
158             status  => 404,
159             openapi => { error => "License not found" }
160         );
161     }
162
163     return try {
164         Koha::Database->new->schema->txn_do(
165             sub {
166
167                 my $body = $c->validation->param('body');
168
169                 my $documents = delete $body->{documents} // [];
170
171                 $license->set_from_api($body)->store;
172                 $license->documents($documents);
173
174                 $c->res->headers->location($c->req->url->to_string . '/' . $license->license_id);
175                 return $c->render(
176                     status  => 200,
177                     openapi => $license->to_api
178                 );
179             }
180         );
181     }
182     catch {
183         my $to_api_mapping = Koha::ERM::License->new->to_api_mapping;
184
185         if ( blessed $_ ) {
186             if ( $_->isa('Koha::Exceptions::Object::FKConstraint') ) {
187                 return $c->render(
188                     status  => 400,
189                     openapi => {
190                             error => "Given "
191                             . $to_api_mapping->{ $_->broken_fk }
192                             . " does not exist"
193                     }
194                 );
195             }
196             elsif ( $_->isa('Koha::Exceptions::BadParameter') ) {
197                 return $c->render(
198                     status  => 400,
199                     openapi => {
200                             error => "Given "
201                             . $to_api_mapping->{ $_->parameter }
202                             . " does not exist"
203                     }
204                 );
205             }
206         }
207
208         $c->unhandled_exception($_);
209     };
210 };
211
212 =head3 delete
213
214 =cut
215
216 sub delete {
217     my $c = shift->openapi->valid_input or return;
218
219     my $license = Koha::ERM::Licenses->find( $c->validation->param('license_id') );
220     unless ($license) {
221         return $c->render(
222             status  => 404,
223             openapi => { error => "License not found" }
224         );
225     }
226
227     return try {
228         $license->delete;
229         return $c->render(
230             status  => 204,
231             openapi => q{}
232         );
233     }
234     catch {
235         $c->unhandled_exception($_);
236     };
237 }
238
239 1;