Bug 30719: DB and API
[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->search({
97                     illrequest_id => $new_id
98                 })->as_list;
99
100                 $c->res->headers->location($c->req->url->to_string . '/' . $new_req[0]->illrequest_id);
101                 return $c->render(
102                     status  => 201,
103                     openapi => $new_req[0]->to_api
104                 );
105             }
106         );
107     }
108     catch {
109
110         my $to_api_mapping = Koha::Illrequest->new->to_api_mapping;
111
112         if ( blessed $_ ) {
113             if ( $_->isa('Koha::Exceptions::Object::DuplicateID') ) {
114                 return $c->render(
115                     status  => 409,
116                     openapi => { error => $_->error, conflict => $_->duplicate_id }
117                 );
118             }
119             elsif ( $_->isa('Koha::Exceptions::Object::FKConstraint') ) {
120                 return $c->render(
121                     status  => 400,
122                     openapi => {
123                             error => "Given "
124                             . $to_api_mapping->{ $_->broken_fk }
125                             . " does not exist"
126                     }
127                 );
128             }
129             elsif ( $_->isa('Koha::Exceptions::BadParameter') ) {
130                 return $c->render(
131                     status  => 400,
132                     openapi => {
133                             error => "Given "
134                             . $to_api_mapping->{ $_->parameter }
135                             . " does not exist"
136                     }
137                 );
138             }
139         }
140
141         $c->unhandled_exception($_);
142     };
143 }
144
145 1;