Bug 33573: Add public endpoint for cancelling holds
[koha.git] / Koha / REST / V1 / ImportBatchProfiles.pm
1 package Koha::REST::V1::ImportBatchProfiles;
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::ImportBatchProfiles;
23 use Koha::ImportBatchProfile;
24
25 use Try::Tiny qw( catch try );
26
27 =head1 NAME
28
29 Koha::REST::V1::ImportBatchProfiles - Koha REST API for handling profiles for import batches (V1)
30
31 =head1 API
32
33 =head2 Methods
34
35 =cut
36
37 =head3 list
38
39 Method that handles listing Koha::ImportBatchProfile objects
40
41 =cut
42
43 sub list {
44     my $c = shift->openapi->valid_input or return;
45
46     return try {
47         my $profiles_set = Koha::ImportBatchProfiles->new;
48         my $profiles = $c->objects->search( $profiles_set );
49         return $c->render(
50             status => 200,
51             openapi => $profiles
52         );
53     }
54     catch {
55         $c->unhandled_exception($_);
56     };
57 }
58
59 =head3 add
60
61 Method that handles adding a new Koha::ImportBatchProfile object
62
63 =cut
64
65 sub add {
66     my $c = shift->openapi->valid_input or return;
67
68     my $body = $c->validation->param('body');
69
70     return try {
71         my $profile = Koha::ImportBatchProfile->new_from_api( $body )->store;
72         return $c->render(
73             status  => 201,
74             openapi => $profile->to_api
75         );
76     }
77     catch {
78         $c->unhandled_exception($_);
79     };
80 }
81
82 =head3 edit
83
84 Method that handles modifying a Koha::Hold object
85
86 =cut
87
88 sub edit {
89     my $c = shift->openapi->valid_input or return;
90
91     return try {
92         my $profile_id = $c->validation->param('import_batch_profile_id');
93         my $profile = Koha::ImportBatchProfiles->find( $profile_id );
94         unless ($profile) {
95             return $c->render( status  => 404,
96                             openapi => {error => "Import batch profile not found"} );
97         }
98
99         my $body = $c->req->json;
100
101         $profile->set_from_api($body)->store;
102
103         return $c->render(
104             status => 200,
105             openapi => $profile->to_api
106         );
107     }
108     catch {
109         $c->unhandled_exception($_);
110     };
111 }
112
113 =head3 delete
114
115 Method that handles deleting a Koha::ImportBatchProfile object
116
117 =cut
118
119 sub delete {
120     my $c = shift->openapi->valid_input or return;
121
122     my $profile_id = $c->validation->param('import_batch_profile_id');
123     my $profile = Koha::ImportBatchProfiles->find( $profile_id );
124
125     unless ($profile) {
126         return $c->render( status  => 404,
127                         openapi => {error => "Import batch profile not found"} );
128     }
129
130     return try {
131         $profile->delete;
132
133         return $c->render(
134             status => 204,
135             openapi => q{}
136         );
137     }
138     catch {
139         $c->unhandled_exception($_);
140     };
141 }
142
143 1;