|
Lines 66-77
Controller function that handles retrieving a single Koha::AdvancedEditorMacro
Link Here
|
| 66 |
sub get { |
66 |
sub get { |
| 67 |
my $c = shift->openapi->valid_input or return; |
67 |
my $c = shift->openapi->valid_input or return; |
| 68 |
my $patron = $c->stash('koha.user'); |
68 |
my $patron = $c->stash('koha.user'); |
| 69 |
my $macro = Koha::AdvancedEditorMacros->find({ |
69 |
my $macro = Koha::AdvancedEditorMacros->find( $c->param('advancededitormacro_id') ); |
| 70 |
id => $c->validation->param('advancededitormacro_id'), |
|
|
| 71 |
}); |
| 72 |
unless ($macro) { |
70 |
unless ($macro) { |
| 73 |
return $c->render( status => 404, |
71 |
return $c->render( |
| 74 |
openapi => { error => "Macro not found" } ); |
72 |
status => 404, |
|
|
73 |
openapi => { error => "Macro not found" } |
| 74 |
); |
| 75 |
} |
75 |
} |
| 76 |
if( $macro->shared ){ |
76 |
if( $macro->shared ){ |
| 77 |
return $c->render( status => 403, openapi => { |
77 |
return $c->render( status => 403, openapi => { |
|
Lines 97-103
sub get_shared {
Link Here
|
| 97 |
my $c = shift->openapi->valid_input or return; |
97 |
my $c = shift->openapi->valid_input or return; |
| 98 |
my $patron = $c->stash('koha.user'); |
98 |
my $patron = $c->stash('koha.user'); |
| 99 |
my $macro = Koha::AdvancedEditorMacros->find({ |
99 |
my $macro = Koha::AdvancedEditorMacros->find({ |
| 100 |
id => $c->validation->param('advancededitormacro_id'), |
100 |
id => $c->param('advancededitormacro_id'), |
| 101 |
}); |
101 |
}); |
| 102 |
unless ($macro) { |
102 |
unless ($macro) { |
| 103 |
return $c->render( status => 404, |
103 |
return $c->render( status => 404, |
|
Lines 120-132
Controller function that handles adding a new Koha::AdvancedEditorMacro object
Link Here
|
| 120 |
sub add { |
120 |
sub add { |
| 121 |
my $c = shift->openapi->valid_input or return; |
121 |
my $c = shift->openapi->valid_input or return; |
| 122 |
|
122 |
|
| 123 |
if( defined $c->validation->param('body')->{shared} && $c->validation->param('body')->{shared} == 1 ){ |
123 |
my $body = $c->req->json; |
|
|
124 |
|
| 125 |
if( defined $body->{shared} && $body->{shared} == 1 ){ |
| 124 |
return $c->render( status => 403, |
126 |
return $c->render( status => 403, |
| 125 |
openapi => { error => "To create shared macros you must use advancededitor/shared" } ); |
127 |
openapi => { error => "To create shared macros you must use advancededitor/shared" } ); |
| 126 |
} |
128 |
} |
| 127 |
|
129 |
|
| 128 |
return try { |
130 |
return try { |
| 129 |
my $macro = Koha::AdvancedEditorMacro->new_from_api( $c->validation->param('body') ); |
131 |
my $macro = Koha::AdvancedEditorMacro->new_from_api( $body ); |
| 130 |
$macro->store->discard_changes; |
132 |
$macro->store->discard_changes; |
| 131 |
$c->res->headers->location( $c->req->url->to_string . '/' . $macro->id ); |
133 |
$c->res->headers->location( $c->req->url->to_string . '/' . $macro->id ); |
| 132 |
return $c->render( |
134 |
return $c->render( |
|
Lines 148-159
Controller function that handles adding a new shared Koha::AdvancedEditorMacro o
Link Here
|
| 148 |
sub add_shared { |
150 |
sub add_shared { |
| 149 |
my $c = shift->openapi->valid_input or return; |
151 |
my $c = shift->openapi->valid_input or return; |
| 150 |
|
152 |
|
| 151 |
unless( defined $c->validation->param('body')->{shared} && $c->validation->param('body')->{shared} == 1 ){ |
153 |
my $body = $c->req->json; |
|
|
154 |
|
| 155 |
unless( defined $body->{shared} && $body->{shared} == 1 ){ |
| 152 |
return $c->render( status => 403, |
156 |
return $c->render( status => 403, |
| 153 |
openapi => { error => "To create private macros you must use advancededitor" } ); |
157 |
openapi => { error => "To create private macros you must use advancededitor" } ); |
| 154 |
} |
158 |
} |
| 155 |
return try { |
159 |
return try { |
| 156 |
my $macro = Koha::AdvancedEditorMacro->new_from_api( $c->validation->param('body') ); |
160 |
my $macro = Koha::AdvancedEditorMacro->new_from_api( $body ); |
| 157 |
$macro->store->discard_changes; |
161 |
$macro->store->discard_changes; |
| 158 |
$c->res->headers->location( $c->req->url->to_string . '/' . $macro->id ); |
162 |
$c->res->headers->location( $c->req->url->to_string . '/' . $macro->id ); |
| 159 |
return $c->render( |
163 |
return $c->render( |
|
Lines 175-181
Controller function that handles updating a Koha::AdvancedEditorMacro object
Link Here
|
| 175 |
sub update { |
179 |
sub update { |
| 176 |
my $c = shift->openapi->valid_input or return; |
180 |
my $c = shift->openapi->valid_input or return; |
| 177 |
|
181 |
|
| 178 |
my $macro = Koha::AdvancedEditorMacros->find( $c->validation->param('advancededitormacro_id') ); |
182 |
my $macro = Koha::AdvancedEditorMacros->find( $c->param('advancededitormacro_id') ); |
| 179 |
|
183 |
|
| 180 |
if ( not defined $macro ) { |
184 |
if ( not defined $macro ) { |
| 181 |
return $c->render( status => 404, |
185 |
return $c->render( status => 404, |
|
Lines 183-189
sub update {
Link Here
|
| 183 |
} |
187 |
} |
| 184 |
my $patron = $c->stash('koha.user'); |
188 |
my $patron = $c->stash('koha.user'); |
| 185 |
|
189 |
|
| 186 |
if( $macro->shared == 1 || defined $c->validation->param('body')->{shared} && $c->validation->param('body')->{shared} == 1 ){ |
190 |
my $body = $c->req->json; |
|
|
191 |
|
| 192 |
if( $macro->shared == 1 || defined $body->{shared} && $body->{shared} == 1 ){ |
| 187 |
return $c->render( status => 403, |
193 |
return $c->render( status => 403, |
| 188 |
openapi => { error => "To update a macro as shared you must use the advanced_editor/macros/shared endpoint" } ); |
194 |
openapi => { error => "To update a macro as shared you must use the advanced_editor/macros/shared endpoint" } ); |
| 189 |
} else { |
195 |
} else { |
|
Lines 194-201
sub update {
Link Here
|
| 194 |
} |
200 |
} |
| 195 |
|
201 |
|
| 196 |
return try { |
202 |
return try { |
| 197 |
my $params = $c->req->json; |
203 |
$macro->set_from_api( $body ); |
| 198 |
$macro->set_from_api( $params ); |
|
|
| 199 |
$macro->store->discard_changes; |
204 |
$macro->store->discard_changes; |
| 200 |
return $c->render( status => 200, openapi => $macro->to_api ); |
205 |
return $c->render( status => 200, openapi => $macro->to_api ); |
| 201 |
} |
206 |
} |
|
Lines 213-233
Controller function that handles updating a shared Koha::AdvancedEditorMacro obj
Link Here
|
| 213 |
sub update_shared { |
218 |
sub update_shared { |
| 214 |
my $c = shift->openapi->valid_input or return; |
219 |
my $c = shift->openapi->valid_input or return; |
| 215 |
|
220 |
|
| 216 |
my $macro = Koha::AdvancedEditorMacros->find( $c->validation->param('advancededitormacro_id') ); |
221 |
my $macro = Koha::AdvancedEditorMacros->find( $c->param('advancededitormacro_id') ); |
|
|
222 |
|
| 223 |
my $body = $c->req->json; |
| 217 |
|
224 |
|
| 218 |
if ( not defined $macro ) { |
225 |
if ( not defined $macro ) { |
| 219 |
return $c->render( status => 404, |
226 |
return $c->render( status => 404, |
| 220 |
openapi => { error => "Object not found" } ); |
227 |
openapi => { error => "Object not found" } ); |
| 221 |
} |
228 |
} |
| 222 |
|
229 |
|
| 223 |
unless( $macro->shared == 1 || defined $c->validation->param('body')->{shared} && $c->validation->param('body')->{shared} == 1 ){ |
230 |
unless( $macro->shared == 1 || defined $body->{shared} && $body->{shared} == 1 ){ |
| 224 |
return $c->render( status => 403, |
231 |
return $c->render( status => 403, |
| 225 |
openapi => { error => "You can only update shared macros using this endpoint" } ); |
232 |
openapi => { error => "You can only update shared macros using this endpoint" } ); |
| 226 |
} |
233 |
} |
| 227 |
|
234 |
|
| 228 |
return try { |
235 |
return try { |
| 229 |
my $params = $c->req->json; |
236 |
$macro->set_from_api( $body ); |
| 230 |
$macro->set_from_api( $params ); |
|
|
| 231 |
$macro->store->discard_changes; |
237 |
$macro->store->discard_changes; |
| 232 |
return $c->render( status => 200, openapi => $macro->to_api ); |
238 |
return $c->render( status => 200, openapi => $macro->to_api ); |
| 233 |
} |
239 |
} |
|
Lines 245-251
Controller function that handles deleting a Koha::AdvancedEditorMacro object
Link Here
|
| 245 |
sub delete { |
251 |
sub delete { |
| 246 |
my $c = shift->openapi->valid_input or return; |
252 |
my $c = shift->openapi->valid_input or return; |
| 247 |
|
253 |
|
| 248 |
my $macro = Koha::AdvancedEditorMacros->find( $c->validation->param('advancededitormacro_id') ); |
254 |
my $macro = Koha::AdvancedEditorMacros->find( $c->param('advancededitormacro_id') ); |
| 249 |
if ( not defined $macro ) { |
255 |
if ( not defined $macro ) { |
| 250 |
return $c->render( status => 404, |
256 |
return $c->render( status => 404, |
| 251 |
openapi => { error => "Object not found" } ); |
257 |
openapi => { error => "Object not found" } ); |
|
Lines 280-286
Controller function that handles deleting a shared Koha::AdvancedEditorMacro obj
Link Here
|
| 280 |
sub delete_shared { |
286 |
sub delete_shared { |
| 281 |
my $c = shift->openapi->valid_input or return; |
287 |
my $c = shift->openapi->valid_input or return; |
| 282 |
|
288 |
|
| 283 |
my $macro = Koha::AdvancedEditorMacros->find( $c->validation->param('advancededitormacro_id') ); |
289 |
my $macro = Koha::AdvancedEditorMacros->find( $c->param('advancededitormacro_id') ); |
| 284 |
if ( not defined $macro ) { |
290 |
if ( not defined $macro ) { |
| 285 |
return $c->render( status => 404, |
291 |
return $c->render( status => 404, |
| 286 |
openapi => { error => "Object not found" } ); |
292 |
openapi => { error => "Object not found" } ); |