1 package Koha::REST::V1::ERM::Licenses;
3 # This file is part of Koha.
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.
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.
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>.
20 use Mojo::Base 'Mojolicious::Controller';
22 use Koha::ERM::Licenses;
24 use Scalar::Util qw( blessed );
25 use Try::Tiny qw( catch try );
36 my $c = shift->openapi->valid_input or return;
39 my $licenses_set = Koha::ERM::Licenses->new;
40 my $licenses = $c->objects->search( $licenses_set );
41 return $c->render( status => 200, openapi => $licenses );
44 $c->unhandled_exception($_);
51 Controller function that handles retrieving a single Koha::ERM::License object
56 my $c = shift->openapi->valid_input or return;
59 my $license_id = $c->validation->param('license_id');
60 my $license = $c->objects->find( Koha::ERM::Licenses->search, $license_id );
65 openapi => { error => "License not found" }
75 $c->unhandled_exception($_);
81 Controller function that handles adding a new Koha::ERM::License object
86 my $c = shift->openapi->valid_input or return;
89 Koha::Database->new->schema->txn_do(
92 my $body = $c->validation->param('body');
94 my $documents = delete $body->{documents} // [];
96 my $license = Koha::ERM::License->new_from_api($body)->store;
97 $license->documents($documents);
99 $c->res->headers->location($c->req->url->to_string . '/' . $license->license_id);
102 openapi => $license->to_api
109 my $to_api_mapping = Koha::ERM::License->new->to_api_mapping;
112 if ( $_->isa('Koha::Exceptions::Object::DuplicateID') ) {
115 openapi => { error => $_->error, conflict => $_->duplicate_id }
118 elsif ( $_->isa('Koha::Exceptions::Object::FKConstraint') ) {
123 . $to_api_mapping->{ $_->broken_fk }
128 elsif ( $_->isa('Koha::Exceptions::BadParameter') ) {
133 . $to_api_mapping->{ $_->parameter }
140 $c->unhandled_exception($_);
146 Controller function that handles updating a Koha::ERM::License object
151 my $c = shift->openapi->valid_input or return;
153 my $license_id = $c->validation->param('license_id');
154 my $license = Koha::ERM::Licenses->find( $license_id );
159 openapi => { error => "License not found" }
164 Koha::Database->new->schema->txn_do(
167 my $body = $c->validation->param('body');
169 my $documents = delete $body->{documents} // [];
171 $license->set_from_api($body)->store;
172 $license->documents($documents);
174 $c->res->headers->location($c->req->url->to_string . '/' . $license->license_id);
177 openapi => $license->to_api
183 my $to_api_mapping = Koha::ERM::License->new->to_api_mapping;
186 if ( $_->isa('Koha::Exceptions::Object::FKConstraint') ) {
191 . $to_api_mapping->{ $_->broken_fk }
196 elsif ( $_->isa('Koha::Exceptions::BadParameter') ) {
201 . $to_api_mapping->{ $_->parameter }
208 $c->unhandled_exception($_);
217 my $c = shift->openapi->valid_input or return;
219 my $license = Koha::ERM::Licenses->find( $c->validation->param('license_id') );
223 openapi => { error => "License not found" }
235 $c->unhandled_exception($_);