Bug 13895: Adapt naming according to voted RFC
[koha.git] / Koha / REST / V1 / Checkout.pm
blobe228216a96ed6b20e921479d8b29378acfeff713
1 package Koha::REST::V1::Checkout;
3 # This file is part of Koha.
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.
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.
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.
18 use Mojo::Base 'Mojolicious::Controller';
20 use C4::Auth qw( haspermission );
21 use C4::Context;
22 use C4::Circulation;
23 use Koha::Checkouts;
25 use Try::Tiny;
27 =head1 NAME
29 Koha::REST::V1::Checkout
31 =head1 API
33 =head2 Methods
35 =head3 list
37 List Koha::Checkout objects
39 =cut
41 sub list {
42 my $c = shift->openapi->valid_input or return;
43 try {
44 my $checkouts_set = Koha::Checkouts->new;
45 my $checkouts = $c->objects->search( $checkouts_set, \&_to_model, \&_to_api );
46 return $c->render( status => 200, openapi => $checkouts );
47 } catch {
48 if ( $_->isa('DBIx::Class::Exception') ) {
49 return $c->render(
50 status => 500,
51 openapi => { error => $_->{msg} }
53 } else {
54 return $c->render(
55 status => 500,
56 openapi => { error => "Something went wrong, check the logs." }
62 =head3 get
64 get one checkout
66 =cut
68 sub get {
69 my $c = shift->openapi->valid_input or return;
71 my $checkout = Koha::Checkouts->find( $c->validation->param('checkout_id') );
73 unless ($checkout) {
74 return $c->render(
75 status => 404,
76 openapi => { error => "Checkout doesn't exist" }
80 return $c->render(
81 status => 200,
82 openapi => _to_api($checkout->TO_JSON)
86 =head3 renew
88 Renew a checkout
90 =cut
92 sub renew {
93 my $c = shift->openapi->valid_input or return;
95 my $checkout_id = $c->validation->param('checkout_id');
96 my $checkout = Koha::Checkouts->find( $checkout_id );
98 unless ($checkout) {
99 return $c->render(
100 status => 404,
101 openapi => { error => "Checkout doesn't exist" }
105 my $borrowernumber = $checkout->borrowernumber;
106 my $itemnumber = $checkout->itemnumber;
108 my ($can_renew, $error) = C4::Circulation::CanBookBeRenewed(
109 $borrowernumber, $itemnumber);
111 if (!$can_renew) {
112 return $c->render(
113 status => 403,
114 openapi => { error => "Renewal not authorized ($error)" }
118 AddRenewal($borrowernumber, $itemnumber, $checkout->branchcode);
119 $checkout = Koha::Checkouts->find($checkout_id);
121 $c->res->headers->location( $c->req->url->to_string );
122 return $c->render(
123 status => 201,
124 openapi => _to_api( $checkout->TO_JSON )
128 =head3 _to_api
130 Helper function that maps a hashref of Koha::Checkout attributes into REST api
131 attribute names.
133 =cut
135 sub _to_api {
136 my $checkout = shift;
138 foreach my $column ( keys %{ $Koha::REST::V1::Checkout::to_api_mapping } ) {
139 my $mapped_column = $Koha::REST::V1::Checkout::to_api_mapping->{$column};
140 if ( exists $checkout->{ $column } && defined $mapped_column )
142 $checkout->{ $mapped_column } = delete $checkout->{ $column };
144 elsif ( exists $checkout->{ $column } && !defined $mapped_column ) {
145 delete $checkout->{ $column };
148 return $checkout;
151 =head3 _to_model
153 Helper function that maps REST api objects into Koha::Checkouts
154 attribute names.
156 =cut
158 sub _to_model {
159 my $checkout = shift;
161 foreach my $attribute ( keys %{ $Koha::REST::V1::Checkout::to_model_mapping } ) {
162 my $mapped_attribute = $Koha::REST::V1::Checkout::to_model_mapping->{$attribute};
163 if ( exists $checkout->{ $attribute } && defined $mapped_attribute )
165 $checkout->{ $mapped_attribute } = delete $checkout->{ $attribute };
167 elsif ( exists $checkout->{ $attribute } && !defined $mapped_attribute )
169 delete $checkout->{ $attribute };
172 return $checkout;
175 =head2 Global variables
177 =head3 $to_api_mapping
179 =cut
181 our $to_api_mapping = {
182 issue_id => 'checkout_id',
183 borrowernumber => 'patron_id',
184 itemnumber => 'item_id',
185 date_due => 'due_date',
186 branchcode => 'library_id',
187 returndate => 'checkin_date',
188 lastreneweddate => 'last_renewed_date',
189 issuedate => 'checkout_date',
190 notedate => 'note_date',
193 =head3 $to_model_mapping
195 =cut
197 our $to_model_mapping = {
198 checkout_id => 'issue_id',
199 patron_id => 'borrowernumber',
200 item_id => 'itemnumber',
201 due_date => 'date_due',
202 library_id => 'branchcode',
203 checkin_date => 'returndate',
204 last_renewed_date => 'lastreneweddate',
205 checkout_date => 'issuedate',
206 note_date => 'notedate',