1 package Koha::REST::V1::ERM::Packages;
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::Packages;
24 use Scalar::Util qw( blessed );
25 use Try::Tiny qw( catch try );
36 my $c = shift->openapi->valid_input or return;
39 my $packages_set = Koha::ERM::Packages->new;
40 my $packages = $c->objects->search( $packages_set );
41 return $c->render( status => 200, openapi => $packages );
44 $c->unhandled_exception($_);
51 Controller function that handles retrieving a single Koha::ERM::Package object
56 my $c = shift->openapi->valid_input or return;
59 my $package_id = $c->validation->param('package_id');
60 my $package = $c->objects->find( Koha::ERM::Packages->search, $package_id );
65 openapi => { error => "Package not found" }
75 $c->unhandled_exception($_);
81 Controller function that handles adding a new Koha::ERM::Package 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 $package = Koha::ERM::Package->new_from_api($body)->store;
96 $c->res->headers->location($c->req->url->to_string . '/' . $package->package_id);
99 openapi => $package->to_api
106 my $to_api_mapping = Koha::ERM::Package->new->to_api_mapping;
109 if ( $_->isa('Koha::Exceptions::Object::DuplicateID') ) {
112 openapi => { error => $_->error, conflict => $_->duplicate_id }
115 elsif ( $_->isa('Koha::Exceptions::Object::FKConstraint') ) {
120 . $to_api_mapping->{ $_->broken_fk }
125 elsif ( $_->isa('Koha::Exceptions::BadParameter') ) {
130 . $to_api_mapping->{ $_->parameter }
137 $c->unhandled_exception($_);
143 Controller function that handles updating a Koha::ERM::Package object
148 my $c = shift->openapi->valid_input or return;
150 my $package_id = $c->validation->param('package_id');
151 my $package = Koha::ERM::Packages->find( $package_id );
156 openapi => { error => "Package not found" }
161 Koha::Database->new->schema->txn_do(
164 my $body = $c->validation->param('body');
166 $package->set_from_api($body)->store;
168 $c->res->headers->location($c->req->url->to_string . '/' . $package->package_id);
171 openapi => $package->to_api
177 my $to_api_mapping = Koha::ERM::Package->new->to_api_mapping;
180 if ( $_->isa('Koha::Exceptions::Object::FKConstraint') ) {
185 . $to_api_mapping->{ $_->broken_fk }
190 elsif ( $_->isa('Koha::Exceptions::BadParameter') ) {
195 . $to_api_mapping->{ $_->parameter }
202 $c->unhandled_exception($_);
211 my $c = shift->openapi->valid_input or return;
213 my $package = Koha::ERM::Packages->find( $c->validation->param('package_id') );
217 openapi => { error => "Package not found" }
229 $c->unhandled_exception($_);