Bug 26633: Add REST API for managing transfer limits
[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;
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     $body =
71
72     return try {
73         my $profile = Koha::ImportBatchProfile->new_from_api( $body )->store;
74         return $c->render(
75             status  => 201,
76             openapi => $profile->to_api
77         );
78     }
79     catch {
80         $c->unhandled_exception($_);
81     };
82 }
83
84 =head3 edit
85
86 Method that handles modifying a Koha::Hold object
87
88 =cut
89
90 sub edit {
91     my $c = shift->openapi->valid_input or return;
92
93     return try {
94         my $profile_id = $c->validation->param('import_batch_profile_id');
95         my $profile = Koha::ImportBatchProfiles->find( $profile_id );
96         unless ($profile) {
97             return $c->render( status  => 404,
98                             openapi => {error => "Import batch profile not found"} );
99         }
100
101         my $body = $c->req->json;
102
103         $profile->set_from_api($body)->store;
104
105         return $c->render(
106             status => 200,
107             openapi => $profile->to_api
108         );
109     }
110     catch {
111         $c->unhandled_exception($_);
112     };
113 }
114
115 =head3 delete
116
117 Method that handles deleting a Koha::ImportBatchProfile object
118
119 =cut
120
121 sub delete {
122     my $c = shift->openapi->valid_input or return;
123
124     my $profile_id = $c->validation->param('import_batch_profile_id');
125     my $profile = Koha::ImportBatchProfiles->find( $profile_id );
126
127     unless ($profile) {
128         return $c->render( status  => 404,
129                         openapi => {error => "Import batch profile not found"} );
130     }
131
132     return try {
133         $profile->delete;
134
135         return $c->render(
136             status => 204,
137             openapi => q{}
138         );
139     }
140     catch {
141         $c->unhandled_exception($_);
142     };
143 }
144
145 1;