Bug 34587: Fix ->validation->output in API endpoints
[koha.git] / Koha / REST / V1 / ERM / EUsage / DefaultUsageReports.pm
1 package Koha::REST::V1::ERM::EUsage::DefaultUsageReports;
2
3 # Copyright 2023 PTFS Europe
4
5 # This file is part of Koha.
6 #
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.
11 #
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.
16 #
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>.
19
20 use Modern::Perl;
21
22 use Mojo::Base 'Mojolicious::Controller';
23
24 use Koha::ERM::EUsage::DefaultUsageReport;
25 use Koha::ERM::EUsage::DefaultUsageReports;
26
27 use Scalar::Util qw( blessed );
28 use Try::Tiny    qw( catch try );
29
30 =head1 API
31
32 =head2 Methods
33
34 =head3 list
35
36 =cut
37
38 sub list {
39     my $c = shift->openapi->valid_input or return;
40
41     return try {
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 );
45     } catch {
46         $c->unhandled_exception($_);
47     };
48
49 }
50
51 =head3 add
52
53 Controller function that handles adding a new Koha::ERM::EUsage::DefaultUsageReport object
54
55 =cut
56
57 sub add {
58     my $c = shift->openapi->valid_input or return;
59
60     return try {
61         Koha::Database->new->schema->txn_do(
62             sub {
63
64                 my $body = $c->req->json;
65
66                 my $default_report = Koha::ERM::EUsage::DefaultUsageReport->new_from_api($body)->store;
67
68                 $c->res->headers->location(
69                     $c->req->url->to_string . '/' . $default_report->erm_default_usage_report_id );
70                 return $c->render(
71                     status  => 201,
72                     openapi => $default_report->to_api
73                 );
74             }
75         );
76     } catch {
77
78         my $to_api_mapping = Koha::ERM::EUsage::DefaultUsageReport->new->to_api_mapping;
79
80         if ( blessed $_ ) {
81             if ( $_->isa('Koha::Exceptions::Object::DuplicateID') ) {
82                 return $c->render(
83                     status  => 409,
84                     openapi => { error => $_->error, conflict => $_->duplicate_id }
85                 );
86             } elsif ( $_->isa('Koha::Exceptions::Object::FKConstraint') ) {
87                 return $c->render(
88                     status  => 400,
89                     openapi => { error => "Given " . $to_api_mapping->{ $_->broken_fk } . " does not exist" }
90                 );
91             } elsif ( $_->isa('Koha::Exceptions::BadParameter') ) {
92                 return $c->render(
93                     status  => 400,
94                     openapi => { error => "Given " . $to_api_mapping->{ $_->parameter } . " does not exist" }
95                 );
96             } elsif ( $_->isa('Koha::Exceptions::PayloadTooLarge') ) {
97                 return $c->render(
98                     status  => 413,
99                     openapi => { error => $_->error }
100                 );
101             }
102         }
103
104         $c->unhandled_exception($_);
105     };
106 }
107
108 =head3 delete
109
110 =cut
111
112 sub delete {
113     my $c = shift->openapi->valid_input or return;
114
115     my $default_report = Koha::ERM::EUsage::DefaultUsageReports->find( $c->param('erm_default_usage_report_id') );
116     unless ($default_report) {
117         return $c->render(
118             status  => 404,
119             openapi => { error => "Default report not found" }
120         );
121     }
122
123     return try {
124         $default_report->delete;
125         return $c->render(
126             status  => 204,
127             openapi => q{}
128         );
129     } catch {
130         $c->unhandled_exception($_);
131     };
132 }
133
134 1;