1 package Koha::REST::V1::ERM::EUsage::DefaultUsageReports;
3 # Copyright 2023 PTFS Europe
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
22 use Mojo::Base 'Mojolicious::Controller';
24 use Koha::ERM::EUsage::DefaultUsageReport;
25 use Koha::ERM::EUsage::DefaultUsageReports;
27 use Scalar::Util qw( blessed );
28 use Try::Tiny qw( catch try );
39 my $c = shift->openapi->valid_input or return;
42 my $default_usage_report_set = Koha::ERM::EUsage::DefaultUsageReports->new;
43 my $default_usage_report = $c->objects->search($default_usage_report_set);
44 return $c->render( status => 200, openapi => $default_usage_report );
46 $c->unhandled_exception($_);
53 Controller function that handles adding a new Koha::ERM::EUsage::DefaultUsageReport object
58 my $c = shift->openapi->valid_input or return;
61 Koha::Database->new->schema->txn_do(
64 my $body = $c->req->json;
66 my $default_report = Koha::ERM::EUsage::DefaultUsageReport->new_from_api($body)->store;
68 $c->res->headers->location(
69 $c->req->url->to_string . '/' . $default_report->erm_default_usage_report_id );
72 openapi => $default_report->to_api
78 my $to_api_mapping = Koha::ERM::EUsage::DefaultUsageReport->new->to_api_mapping;
81 if ( $_->isa('Koha::Exceptions::Object::DuplicateID') ) {
84 openapi => { error => $_->error, conflict => $_->duplicate_id }
86 } elsif ( $_->isa('Koha::Exceptions::Object::FKConstraint') ) {
89 openapi => { error => "Given " . $to_api_mapping->{ $_->broken_fk } . " does not exist" }
91 } elsif ( $_->isa('Koha::Exceptions::BadParameter') ) {
94 openapi => { error => "Given " . $to_api_mapping->{ $_->parameter } . " does not exist" }
96 } elsif ( $_->isa('Koha::Exceptions::PayloadTooLarge') ) {
99 openapi => { error => $_->error }
104 $c->unhandled_exception($_);
113 my $c = shift->openapi->valid_input or return;
115 my $default_report = Koha::ERM::EUsage::DefaultUsageReports->find( $c->param('erm_default_usage_report_id') );
116 unless ($default_report) {
119 openapi => { error => "Default report not found" }
124 $default_report->delete;
130 $c->unhandled_exception($_);