Koha-clms/Koha/REST/V1/ArticleRequests.pm

107 lines
2.8 KiB
Perl

package Koha::REST::V1::ArticleRequests;
# This file is part of Koha.
#
# Koha is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# Koha is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Koha; if not, see <http://www.gnu.org/licenses>.
use Modern::Perl;
use Mojo::Base 'Mojolicious::Controller';
use Koha::Database;
use Koha::ArticleRequests;
use Scalar::Util qw( blessed );
use Try::Tiny qw( catch try );
=head1 NAME
Koha::REST::V1::ArticleRequests
=head1 API
=head2 Methods
=head3 cancel
Controller function that handles cancelling a Koha::ArticleRequest object
=cut
sub cancel {
my $c = shift->openapi->valid_input or return;
my $article_request = Koha::ArticleRequests->find( $c->param('article_request_id') );
return $c->render_resource_not_found("Article request")
unless $article_request;
my $reason = $c->param('cancellation_reason');
my $notes = $c->param('notes');
return try {
$article_request->cancel(
{
cancellation_reason => $reason,
notes => $notes
}
);
return $c->render_resource_deleted;
} catch {
$c->unhandled_exception($_);
};
}
=head3 patron_cancel (public route)
Controller function that handles cancelling a patron's Koha::ArticleRequest object
=cut
sub patron_cancel {
my $c = shift->openapi->valid_input or return;
my $patron_id = $c->param('patron_id');
return try {
$c->auth->public($patron_id);
my $patron = Koha::Patrons->find($patron_id);
return $c->render_resource_not_found("Patron")
unless $patron;
# patron_id has been validated by the $c->auth->public check, so the following call to related
# article requests covers the case of article requests not belonging to the patron
my $article_request = $patron->article_requests->find( $c->param('article_request_id') );
return $c->render_resource_not_found("Article request")
unless $article_request;
my $reason = $c->param('cancellation_reason');
my $notes = $c->param('notes');
$article_request->cancel(
{
cancellation_reason => $reason,
notes => $notes
}
);
return $c->render_resource_deleted;
} catch {
$c->unhandled_exception($_);
};
}
1;