Bug 34587: SUSHI COUNTER Harvester
[koha.git] / Koha / REST / V1 / ERM / CounterFiles.pm
1 package Koha::REST::V1::ERM::CounterFiles;
2
3 # This file is part of Koha.
4 #
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.
9 #
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.
14 #
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>.
17
18 use Modern::Perl;
19
20 use Mojo::Base 'Mojolicious::Controller';
21
22 use Koha::ERM::CounterFiles;
23
24 use Scalar::Util qw( blessed );
25 use Try::Tiny    qw( catch try );
26
27 =head1 API
28
29 =head2 Methods
30
31 =head3 list
32
33 =cut
34
35 sub list {
36     my $c = shift->openapi->valid_input or return;
37
38     return try {
39         my $counter_files_set = Koha::ERM::CounterFiles->new;
40         my $counter_files     = $c->objects->search($counter_files_set);
41         return $c->render( status => 200, openapi => $counter_files );
42     }
43     catch {
44         $c->unhandled_exception($_);
45     };
46
47 }
48
49 =head3 get
50
51 Controller function that handles retrieving a single Koha::ERM::CounterFile object
52
53 =cut
54
55 sub get {
56     my $c = shift->openapi->valid_input or return;
57
58     return try {
59         my $counter_file_id = $c->validation->param('erm_counter_files_id');
60
61         # Do not use $c->objects->find here, we need the file_content
62         my $counter_file = Koha::ERM::CounterFiles->find($counter_file_id);
63
64         if ( !$counter_file ) {
65             return $c->render(
66                 status  => 404,
67                 openapi => { error => "Counter file not found" }
68             );
69         }
70
71         $c->render_file(
72             'data'     => $counter_file->file_content,
73             'filename' => $counter_file->filename . '.csv'
74         );
75     }
76     catch {
77         $c->unhandled_exception($_);
78     };
79 }
80
81 =head3 delete
82
83 =cut
84
85 sub delete {
86     my $c = shift->openapi->valid_input or return;
87
88     my $counter_file_id = $c->validation->param('erm_counter_files_id');
89     my $counter_file    = Koha::ERM::CounterFiles->find($counter_file_id);
90     unless ($counter_file) {
91         return $c->render(
92             status  => 404,
93             openapi => { error => "Counter file not found" }
94         );
95     }
96
97     return try {
98         $counter_file->delete;
99         return $c->render(
100             status  => 204,
101             openapi => q{}
102         );
103     }
104     catch {
105         $c->unhandled_exception($_);
106     };
107 }
108
109 1;