Bug 30719: (QA follow-up) Squash:
[koha.git] / Koha / REST / V1 / Illrequests.pm
1 package Koha::REST::V1::Illrequests;
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 C4::Context;
23 use Koha::Illrequests;
24 use Koha::Illrequestattributes;
25 use Koha::Libraries;
26 use Koha::Patrons;
27 use Koha::Libraries;
28 use Koha::DateUtils qw( format_sqldatetime );
29
30 use Scalar::Util qw( blessed );
31 use Try::Tiny qw( catch try );
32
33 =head1 NAME
34
35 Koha::REST::V1::Illrequests
36
37 =head2 Operations
38
39 =head3 list
40
41 Controller function that handles listing Koha::Illrequest objects
42
43 =cut
44
45 sub list {
46     my $c = shift->openapi->valid_input or return;
47
48     return try {
49
50         my $reqs = $c->objects->search(Koha::Illrequests->new->filter_by_visible);
51
52         return $c->render(
53             status  => 200,
54             openapi => $reqs,
55         );
56     } catch {
57         $c->unhandled_exception($_);
58     };
59 }
60
61 =head3 add
62
63 Adds a new ILL request
64
65 =cut
66
67 sub add {
68     my $c = shift->openapi->valid_input or return;
69
70     return try {
71
72         Koha::Database->new->schema->txn_do(
73             sub {
74
75                 my $body = $c->validation->param('body');
76                 $body->{backend} = delete $body->{ill_backend_id};
77                 $body->{borrowernumber} = delete $body->{patron_id};
78                 $body->{branchcode} = delete $body->{library_id};
79
80                 my $request = Koha::Illrequest->new->load_backend( $body->{backend} );
81
82                 my $create_api = $request->_backend->capabilities('create_api');
83
84                 if (!$create_api) {
85                     return $c->render(
86                         status  => 405,
87                         openapi => {
88                             errors => [ 'This backend does not allow request creation via API' ]
89                         }
90                     );
91                 }
92
93                 my $create_result = &{$create_api}($body, $request);
94                 my $new_id = $create_result->illrequest_id;
95
96                 my $new_req = Koha::Illrequests->find($new_id);
97
98                 $c->res->headers->location($c->req->url->to_string . '/' . $new_req->illrequest_id);
99                 return $c->render(
100                     status  => 201,
101                     openapi => $new_req->to_api
102                 );
103             }
104         );
105     }
106     catch {
107
108         my $to_api_mapping = Koha::Illrequest->new->to_api_mapping;
109
110         if ( blessed $_ ) {
111             if ( $_->isa('Koha::Exceptions::Object::DuplicateID') ) {
112                 return $c->render(
113                     status  => 409,
114                     openapi => { error => $_->error, conflict => $_->duplicate_id }
115                 );
116             }
117             elsif ( $_->isa('Koha::Exceptions::Object::FKConstraint') ) {
118                 return $c->render(
119                     status  => 400,
120                     openapi => {
121                             error => "Given "
122                             . $to_api_mapping->{ $_->broken_fk }
123                             . " does not exist"
124                     }
125                 );
126             }
127             elsif ( $_->isa('Koha::Exceptions::BadParameter') ) {
128                 return $c->render(
129                     status  => 400,
130                     openapi => {
131                             error => "Given "
132                             . $to_api_mapping->{ $_->parameter }
133                             . " does not exist"
134                     }
135                 );
136             }
137         }
138
139         $c->unhandled_exception($_);
140     };
141 }
142
143 1;