86a2f46648846df11ddf627adfc858492bc229b5
[srvgit] / Koha / REST / V1 / AdvancedEditorMacro.pm
1 package Koha::REST::V1::AdvancedEditorMacro;
2
3 # This file is part of Koha.
4 #
5 # Koha is free software; you can redistribute it and/or modify it under the
6 # terms of the GNU General Public License as published by the Free Software
7 # Foundation; either version 3 of the License, or (at your option) any later
8 # version.
9 #
10 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
11 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with Koha; if not, see <http://www.gnu.org/licenses>.
16
17 use Modern::Perl;
18
19 use Mojo::Base 'Mojolicious::Controller';
20 use Koha::AdvancedEditorMacros;
21
22 use Try::Tiny;
23
24 =head1 Name
25
26 Koha::REST::V1::AdvancedEditorMacro
27
28 =head1 API
29
30 =head2 Methods
31
32 =head3 list
33
34 Controller function that handles listing Koha::AdvancedEditorMacro objects
35
36 =cut
37
38 sub list {
39     my $c = shift->openapi->valid_input or return;
40     my $patron = $c->stash('koha.user');
41     return try {
42         my $macros_set = Koha::AdvancedEditorMacros->search(
43             {
44                 -or =>
45                   { shared => 1, borrowernumber => $patron->borrowernumber }
46             }
47         );
48         my $macros = $c->objects->search( $macros_set );
49         return $c->render(
50             status  => 200,
51             openapi => $macros
52         );
53     }
54     catch {
55         $c->unhandled_exception($_);
56     };
57
58 }
59
60 =head3 get
61
62 Controller function that handles retrieving a single Koha::AdvancedEditorMacro
63
64 =cut
65
66 sub get {
67     my $c = shift->openapi->valid_input or return;
68     my $patron = $c->stash('koha.user');
69     my $macro = Koha::AdvancedEditorMacros->find({
70         id => $c->validation->param('advancededitormacro_id'),
71     });
72     unless ($macro) {
73         return $c->render( status  => 404,
74                            openapi => { error => "Macro not found" } );
75     }
76     if( $macro->shared ){
77         return $c->render( status => 403, openapi => {
78             error => "This macro is shared, you must access it via advanced_editor/macros/shared"
79         });
80     }
81     if( $macro->borrowernumber != $patron->borrowernumber ){
82         return $c->render( status => 403, openapi => {
83             error => "You do not have permission to access this macro"
84         });
85     }
86
87     return $c->render( status => 200, openapi => $macro->to_api );
88 }
89
90 =head3 get_shared
91
92 Controller function that handles retrieving a single Koha::AdvancedEditorMacro
93
94 =cut
95
96 sub get_shared {
97     my $c = shift->openapi->valid_input or return;
98     my $patron = $c->stash('koha.user');
99     my $macro = Koha::AdvancedEditorMacros->find({
100         id => $c->validation->param('advancededitormacro_id'),
101     });
102     unless ($macro) {
103         return $c->render( status  => 404,
104                            openapi => { error => "Macro not found" } );
105     }
106     unless( $macro->shared ){
107         return $c->render( status => 403, openapi => {
108             error => "This macro is not shared, you must access it via advanced_editor/macros"
109         });
110     }
111     return $c->render( status => 200, openapi => $macro->to_api );
112 }
113
114 =head3 add
115
116 Controller function that handles adding a new Koha::AdvancedEditorMacro object
117
118 =cut
119
120 sub add {
121     my $c = shift->openapi->valid_input or return;
122
123     if( defined $c->validation->param('body')->{shared} && $c->validation->param('body')->{shared} == 1 ){
124         return $c->render( status  => 403,
125                            openapi => { error => "To create shared macros you must use advancededitor/shared" } );
126     }
127
128     return try {
129         my $macro = Koha::AdvancedEditorMacro->new_from_api( $c->validation->param('body') );
130         $macro->store->discard_changes;
131         $c->res->headers->location( $c->req->url->to_string . '/' . $macro->id );
132         return $c->render(
133             status  => 201,
134             openapi => $macro->to_api
135         );
136     }
137     catch {
138         $c->unhandled_exception($_);
139     };
140 }
141
142 =head3 add_shared
143
144 Controller function that handles adding a new shared Koha::AdvancedEditorMacro object
145
146 =cut
147
148 sub add_shared {
149     my $c = shift->openapi->valid_input or return;
150
151     unless( defined $c->validation->param('body')->{shared} && $c->validation->param('body')->{shared} == 1 ){
152         return $c->render( status  => 403,
153                            openapi => { error => "To create private macros you must use advancededitor" } );
154     }
155     return try {
156         my $macro = Koha::AdvancedEditorMacro->new_from_api( $c->validation->param('body') );
157         $macro->store->discard_changes;
158         $c->res->headers->location( $c->req->url->to_string . '/' . $macro->id );
159         return $c->render(
160             status  => 201,
161             openapi => $macro->to_api
162         );
163     }
164     catch {
165         $c->unhandled_exception($_);
166     };
167 }
168
169 =head3 update
170
171 Controller function that handles updating a Koha::AdvancedEditorMacro object
172
173 =cut
174
175 sub update {
176     my $c = shift->openapi->valid_input or return;
177
178     my $macro = Koha::AdvancedEditorMacros->find( $c->validation->param('advancededitormacro_id') );
179
180     if ( not defined $macro ) {
181         return $c->render( status  => 404,
182                            openapi => { error => "Object not found" } );
183     }
184     my $patron = $c->stash('koha.user');
185
186     if( $macro->shared == 1 || defined $c->validation->param('body')->{shared} && $c->validation->param('body')->{shared} == 1 ){
187         return $c->render( status  => 403,
188                            openapi => { error => "To update a macro as shared you must use the advanced_editor/macros/shared endpoint" } );
189     } else {
190         unless ( $macro->borrowernumber == $patron->borrowernumber ){
191             return $c->render( status  => 403,
192                                openapi => { error => "You can only edit macros you own" } );
193         }
194     }
195
196     return try {
197         my $params = $c->req->json;
198         $macro->set_from_api( $params );
199         $macro->store->discard_changes;
200         return $c->render( status => 200, openapi => $macro->to_api );
201     }
202     catch {
203         $c->unhandled_exception($_);
204     };
205 }
206
207 =head3 update_shared
208
209 Controller function that handles updating a shared Koha::AdvancedEditorMacro object
210
211 =cut
212
213 sub update_shared {
214     my $c = shift->openapi->valid_input or return;
215
216     my $macro = Koha::AdvancedEditorMacros->find( $c->validation->param('advancededitormacro_id') );
217
218     if ( not defined $macro ) {
219         return $c->render( status  => 404,
220                            openapi => { error => "Object not found" } );
221     }
222
223     unless( $macro->shared == 1 || defined $c->validation->param('body')->{shared} && $c->validation->param('body')->{shared} == 1 ){
224         return $c->render( status  => 403,
225                            openapi => { error => "You can only update shared macros using this endpoint" } );
226     }
227
228     return try {
229         my $params = $c->req->json;
230         $macro->set_from_api( $params );
231         $macro->store->discard_changes;
232         return $c->render( status => 200, openapi => $macro->to_api );
233     }
234     catch {
235         $c->unhandled_exception($_);
236     };
237 }
238
239 =head3 delete
240
241 Controller function that handles deleting a Koha::AdvancedEditorMacro object
242
243 =cut
244
245 sub delete {
246     my $c = shift->openapi->valid_input or return;
247
248     my $macro = Koha::AdvancedEditorMacros->find( $c->validation->param('advancededitormacro_id') );
249     if ( not defined $macro ) {
250         return $c->render( status  => 404,
251                            openapi => { error => "Object not found" } );
252     }
253
254     my $patron = $c->stash('koha.user');
255     if( $macro->shared == 1 ){
256         return $c->render( status  => 403,
257                            openapi => { error => "You cannot delete shared macros using this endpoint" } );
258     } else {
259         unless ( $macro->borrowernumber == $patron->borrowernumber ){
260             return $c->render( status  => 403,
261                                openapi => { error => "You can only delete macros you own" } );
262         }
263     }
264
265     return try {
266         $macro->delete;
267         return $c->render( status => 204, openapi => q{} );
268     }
269     catch {
270         $c->unhandled_exception($_);
271     };
272 }
273
274 =head3 delete_shared
275
276 Controller function that handles deleting a shared Koha::AdvancedEditorMacro object
277
278 =cut
279
280 sub delete_shared {
281     my $c = shift->openapi->valid_input or return;
282
283     my $macro = Koha::AdvancedEditorMacros->find( $c->validation->param('advancededitormacro_id') );
284     if ( not defined $macro ) {
285         return $c->render( status  => 404,
286                            openapi => { error => "Object not found" } );
287     }
288
289     unless( $macro->shared == 1 ){
290         return $c->render( status  => 403,
291                            openapi => { error => "You can only delete shared macros using this endpoint" } );
292     }
293
294     return try {
295         $macro->delete;
296         return $c->render( status => 204, openapi => q{} );
297     }
298     catch {
299         $c->unhandled_exception($_);
300     };
301 }
302
303 1;