line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Mojo::Leds::Rest; |
2
|
|
|
|
|
|
|
$Mojo::Leds::Rest::VERSION = '1.15'; |
3
|
1
|
|
|
1
|
|
428
|
use Mojo::Base 'Mojo::Leds::Page'; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
6
|
|
4
|
1
|
|
|
1
|
|
60
|
use Mojo::Util qw(decamelize class_to_path); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
39
|
|
5
|
1
|
|
|
1
|
|
372
|
use Mojo::JSON qw(decode_json); |
|
1
|
|
|
|
|
19819
|
|
|
1
|
|
|
|
|
1035
|
|
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
has table => sub { |
8
|
|
|
|
|
|
|
return decamelize( |
9
|
|
|
|
|
|
|
( split( /\//, class_to_path( ref shift ) ) )[-1] =~ s/\.pm$//r ); |
10
|
|
|
|
|
|
|
}; |
11
|
|
|
|
|
|
|
has pk => 'id'; |
12
|
|
|
|
|
|
|
has ro => 0; |
13
|
|
|
|
|
|
|
has dbHelper => 'db'; |
14
|
|
|
|
|
|
|
has f_search => 'search'; |
15
|
|
|
|
|
|
|
has f_table => 'resultset'; |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
sub create { |
18
|
0
|
|
|
0
|
1
|
|
my $c = shift; |
19
|
0
|
0
|
|
|
|
|
return $c->_raise_error( "Resource is read-only", 403 ) if $c->ro; |
20
|
0
|
|
|
|
|
|
my $rec = $c->_json_from_body; |
21
|
0
|
0
|
|
|
|
|
return unless ($rec); |
22
|
0
|
|
|
|
|
|
$rec = $c->_create($rec); |
23
|
0
|
0
|
|
|
|
|
return unless ($rec); |
24
|
0
|
|
|
|
|
|
$c->render_json( $c->_rec2json($rec) ); |
25
|
|
|
|
|
|
|
} |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
sub delete { |
28
|
0
|
|
|
0
|
1
|
|
my $c = shift; |
29
|
0
|
0
|
|
|
|
|
return $c->_raise_error( "Resource is read-only", 403 ) if $c->ro; |
30
|
0
|
|
|
|
|
|
my $rec = $c->stash( $c->_class_name . '::record' ); |
31
|
0
|
0
|
|
|
|
|
return $c->_raise_error( 'Element not found', 404 ) unless $rec; |
32
|
0
|
|
|
|
|
|
$c->_delete($rec); |
33
|
0
|
|
|
|
|
|
$c->render_json( undef, 204 ); |
34
|
|
|
|
|
|
|
} |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
sub list { |
37
|
0
|
|
|
0
|
1
|
|
my $c = shift; |
38
|
0
|
|
|
|
|
|
my $query = $c->param('query'); |
39
|
0
|
0
|
|
|
|
|
return $c->$query(@_) if ($query); |
40
|
|
|
|
|
|
|
|
41
|
0
|
|
|
|
|
|
my ( $qry, $opt, $rc ) = $c->_qs2q; |
42
|
0
|
|
|
|
|
|
my $rec = $c->searchDB( $qry, $opt ); |
43
|
0
|
|
|
|
|
|
my $recs = $c->_list( $rec, $qry, $opt, $rc ); |
44
|
|
|
|
|
|
|
|
45
|
0
|
|
|
|
|
|
$c->render_json($recs); |
46
|
|
|
|
|
|
|
} |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
sub listupdate { |
49
|
0
|
|
|
0
|
1
|
|
my $c = shift; |
50
|
0
|
0
|
|
|
|
|
return $c->_raise_error( "Resource is read-only", 403 ) if $c->ro; |
51
|
0
|
|
|
|
|
|
my $json = $c->_json_from_body; |
52
|
0
|
0
|
|
|
|
|
return unless ($json); |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
# json deve essere un array |
55
|
0
|
0
|
|
|
|
|
return $c->_raise_error( 'Not an array of records', 422 ) |
56
|
|
|
|
|
|
|
unless ( ref($json) eq 'ARRAY' ); |
57
|
|
|
|
|
|
|
|
58
|
0
|
|
|
|
|
|
my @recs = $c->_listupdate($json); |
59
|
|
|
|
|
|
|
|
60
|
0
|
|
|
|
|
|
$c->render_json( \@recs ); |
61
|
|
|
|
|
|
|
} |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
sub patch { |
64
|
0
|
|
|
0
|
0
|
|
my $c = shift; |
65
|
0
|
0
|
|
|
|
|
return $c->_raise_error( "Resource is read-only", 403 ) if $c->ro; |
66
|
0
|
|
|
|
|
|
my $json = $c->_json_from_body; |
67
|
0
|
0
|
|
|
|
|
return unless ($json); |
68
|
0
|
|
|
|
|
|
my $rec = $c->_patch($json); |
69
|
0
|
0
|
|
|
|
|
return unless ($rec); |
70
|
0
|
|
|
|
|
|
$c->render_json( $c->_rec2json($rec) ); |
71
|
|
|
|
|
|
|
} |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
sub read { |
74
|
0
|
|
|
0
|
1
|
|
my $c = shift; |
75
|
0
|
|
|
|
|
|
$c->render_json( $c->_rec2json ); |
76
|
|
|
|
|
|
|
} |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
sub render_json { |
79
|
0
|
|
|
0
|
0
|
|
my $c = shift; |
80
|
0
|
|
|
|
|
|
my $json = shift; |
81
|
0
|
|
0
|
|
|
|
my $status = shift || 200; |
82
|
0
|
|
|
|
|
|
$c->render( json => $json, status => $status ); |
83
|
|
|
|
|
|
|
} |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
sub resource_lookup { |
86
|
0
|
|
|
0
|
0
|
|
my $c = shift; |
87
|
0
|
|
|
|
|
|
my $rec = $c->_resource_lookup; |
88
|
0
|
0
|
|
|
|
|
$rec || return $c->_raise_error( 'Element not found', 404 ); |
89
|
0
|
|
|
|
|
|
$c->stash( $c->_class_name . '::record' => $rec ); |
90
|
0
|
|
|
|
|
|
return $rec; |
91
|
|
|
|
|
|
|
} |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
sub searchDB { |
94
|
0
|
|
|
0
|
0
|
|
my $c = shift; |
95
|
0
|
|
|
|
|
|
my $qry = shift; |
96
|
0
|
|
|
|
|
|
my $opt = shift; |
97
|
|
|
|
|
|
|
|
98
|
0
|
|
|
|
|
|
my $f_search = $c->f_search; |
99
|
0
|
|
|
|
|
|
return $c->tableDB->$f_search( $qry, $opt ); |
100
|
|
|
|
|
|
|
} |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
sub tableDB { |
103
|
0
|
|
|
0
|
0
|
|
my $c = shift; |
104
|
0
|
|
|
|
|
|
my $helper = $c->dbHelper; |
105
|
0
|
|
|
|
|
|
my $f_table = $c->f_table; |
106
|
0
|
|
|
|
|
|
return $c->helpers->$helper->$f_table( $c->table ); |
107
|
|
|
|
|
|
|
} |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
sub update { |
110
|
0
|
|
|
0
|
1
|
|
my $c = shift; |
111
|
0
|
0
|
|
|
|
|
return $c->_raise_error( "Resource is read-only", 403 ) if $c->ro; |
112
|
0
|
|
|
|
|
|
my $json = $c->_json_from_body; |
113
|
0
|
0
|
|
|
|
|
return unless ($json); |
114
|
0
|
|
|
|
|
|
my $rec = $c->_update($json); |
115
|
0
|
0
|
|
|
|
|
return unless ($rec); |
116
|
0
|
|
|
|
|
|
$c->render_json( $c->_rec2json($rec) ); |
117
|
|
|
|
|
|
|
} |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
sub _class_name { |
120
|
0
|
|
|
0
|
|
|
return ref shift; |
121
|
|
|
|
|
|
|
} |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
sub _json_from_body { |
124
|
0
|
|
|
0
|
|
|
my $c = shift; |
125
|
0
|
|
|
|
|
|
my $content = $c->req->body; |
126
|
0
|
|
|
|
|
|
my $json; |
127
|
0
|
|
|
|
|
|
eval { $json = decode_json $content}; |
|
0
|
|
|
|
|
|
|
128
|
0
|
0
|
|
|
|
|
if ($@) { |
129
|
0
|
|
|
|
|
|
$@ =~ s/\sat\s\/(.*?)\n$//g; |
130
|
0
|
|
|
|
|
|
return $c->_raise_error( $@, 400 ); |
131
|
|
|
|
|
|
|
} |
132
|
0
|
|
|
|
|
|
return $json; |
133
|
|
|
|
|
|
|
} |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
sub _raise_error { |
136
|
0
|
|
|
0
|
|
|
my $c = shift; |
137
|
0
|
|
|
|
|
|
my $txt = shift; |
138
|
0
|
|
0
|
|
|
|
my $status = shift || 400; |
139
|
0
|
|
|
|
|
|
$c->render_json( |
140
|
|
|
|
|
|
|
{ |
141
|
|
|
|
|
|
|
status => $status, |
142
|
|
|
|
|
|
|
message => $txt |
143
|
|
|
|
|
|
|
}, |
144
|
|
|
|
|
|
|
$status |
145
|
|
|
|
|
|
|
); |
146
|
0
|
|
|
|
|
|
return undef; |
147
|
|
|
|
|
|
|
} |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
1; |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
=pod |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
=head1 NAME |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
Mojo::Leds::Rest - Abstract class for RESTFul webservices interface |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
=head1 VERSION |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
version 1.15 |
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
=head1 RESTFul API |
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
=head2 create |
164
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
PUT /url/ |
166
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
create a new record |
168
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
B |
170
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
=over 4 |
172
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
=item * |
174
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
body JSON - C<{col1: ..., col2:... }> |
176
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
=back |
178
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
B: |
180
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
=over 4 |
182
|
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
=item * |
184
|
|
|
|
|
|
|
|
185
|
|
|
|
|
|
|
Created record in JSON C<{_id:...., col1:. ...., }> |
186
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
=back |
188
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
=head2 read |
190
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
GET /url/id |
192
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
return a single record with _id: id |
194
|
|
|
|
|
|
|
|
195
|
|
|
|
|
|
|
B |
196
|
|
|
|
|
|
|
|
197
|
|
|
|
|
|
|
=over 4 |
198
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
=item * |
200
|
|
|
|
|
|
|
|
201
|
|
|
|
|
|
|
None |
202
|
|
|
|
|
|
|
|
203
|
|
|
|
|
|
|
=back |
204
|
|
|
|
|
|
|
|
205
|
|
|
|
|
|
|
B: |
206
|
|
|
|
|
|
|
|
207
|
|
|
|
|
|
|
=over 4 |
208
|
|
|
|
|
|
|
|
209
|
|
|
|
|
|
|
=item * |
210
|
|
|
|
|
|
|
|
211
|
|
|
|
|
|
|
Record found in JSON C<{_id:...., col1:. ...., }> |
212
|
|
|
|
|
|
|
|
213
|
|
|
|
|
|
|
=back |
214
|
|
|
|
|
|
|
|
215
|
|
|
|
|
|
|
=head2 update |
216
|
|
|
|
|
|
|
|
217
|
|
|
|
|
|
|
PUT /url/id |
218
|
|
|
|
|
|
|
|
219
|
|
|
|
|
|
|
update a single record |
220
|
|
|
|
|
|
|
|
221
|
|
|
|
|
|
|
B |
222
|
|
|
|
|
|
|
|
223
|
|
|
|
|
|
|
=over 4 |
224
|
|
|
|
|
|
|
|
225
|
|
|
|
|
|
|
=item * |
226
|
|
|
|
|
|
|
|
227
|
|
|
|
|
|
|
body JSON - C<{_id:...., col1: new_value, col2: new_value, }> |
228
|
|
|
|
|
|
|
|
229
|
|
|
|
|
|
|
=back |
230
|
|
|
|
|
|
|
|
231
|
|
|
|
|
|
|
B: |
232
|
|
|
|
|
|
|
|
233
|
|
|
|
|
|
|
=over 4 |
234
|
|
|
|
|
|
|
|
235
|
|
|
|
|
|
|
=item * |
236
|
|
|
|
|
|
|
|
237
|
|
|
|
|
|
|
Updated record in JSON C<{_id:...., col1:. ...., }> |
238
|
|
|
|
|
|
|
|
239
|
|
|
|
|
|
|
=back |
240
|
|
|
|
|
|
|
|
241
|
|
|
|
|
|
|
=head2 delete |
242
|
|
|
|
|
|
|
|
243
|
|
|
|
|
|
|
DELETE /url/id |
244
|
|
|
|
|
|
|
|
245
|
|
|
|
|
|
|
delete a record |
246
|
|
|
|
|
|
|
|
247
|
|
|
|
|
|
|
B |
248
|
|
|
|
|
|
|
|
249
|
|
|
|
|
|
|
=over 4 |
250
|
|
|
|
|
|
|
|
251
|
|
|
|
|
|
|
=item * |
252
|
|
|
|
|
|
|
|
253
|
|
|
|
|
|
|
None |
254
|
|
|
|
|
|
|
|
255
|
|
|
|
|
|
|
=back |
256
|
|
|
|
|
|
|
|
257
|
|
|
|
|
|
|
B: |
258
|
|
|
|
|
|
|
|
259
|
|
|
|
|
|
|
=over 4 |
260
|
|
|
|
|
|
|
|
261
|
|
|
|
|
|
|
=item * |
262
|
|
|
|
|
|
|
|
263
|
|
|
|
|
|
|
Empty body |
264
|
|
|
|
|
|
|
|
265
|
|
|
|
|
|
|
=item * |
266
|
|
|
|
|
|
|
|
267
|
|
|
|
|
|
|
HTTP Status: C<204 No Content> |
268
|
|
|
|
|
|
|
|
269
|
|
|
|
|
|
|
=back |
270
|
|
|
|
|
|
|
|
271
|
|
|
|
|
|
|
=head2 list |
272
|
|
|
|
|
|
|
|
273
|
|
|
|
|
|
|
GET /url/ |
274
|
|
|
|
|
|
|
|
275
|
|
|
|
|
|
|
return all records |
276
|
|
|
|
|
|
|
|
277
|
|
|
|
|
|
|
B |
278
|
|
|
|
|
|
|
|
279
|
|
|
|
|
|
|
=over 4 |
280
|
|
|
|
|
|
|
|
281
|
|
|
|
|
|
|
=item * |
282
|
|
|
|
|
|
|
|
283
|
|
|
|
|
|
|
None |
284
|
|
|
|
|
|
|
|
285
|
|
|
|
|
|
|
=back |
286
|
|
|
|
|
|
|
|
287
|
|
|
|
|
|
|
B: |
288
|
|
|
|
|
|
|
|
289
|
|
|
|
|
|
|
=over 4 |
290
|
|
|
|
|
|
|
|
291
|
|
|
|
|
|
|
=item * |
292
|
|
|
|
|
|
|
|
293
|
|
|
|
|
|
|
All records in JSON array: C<[ {_id:...., }, {_id:...., }, ...} ]> |
294
|
|
|
|
|
|
|
|
295
|
|
|
|
|
|
|
=back |
296
|
|
|
|
|
|
|
|
297
|
|
|
|
|
|
|
=head2 listupdate |
298
|
|
|
|
|
|
|
|
299
|
|
|
|
|
|
|
POST /url/ |
300
|
|
|
|
|
|
|
|
301
|
|
|
|
|
|
|
update/creare multi records. Record with _id is updated, record without _id is created. |
302
|
|
|
|
|
|
|
|
303
|
|
|
|
|
|
|
B |
304
|
|
|
|
|
|
|
|
305
|
|
|
|
|
|
|
=over 4 |
306
|
|
|
|
|
|
|
|
307
|
|
|
|
|
|
|
=item * |
308
|
|
|
|
|
|
|
|
309
|
|
|
|
|
|
|
body JSON array: C<[ {col1,... }, {_id:...., col1: new_value, col2: new_value, } ]> |
310
|
|
|
|
|
|
|
|
311
|
|
|
|
|
|
|
=back |
312
|
|
|
|
|
|
|
|
313
|
|
|
|
|
|
|
B: |
314
|
|
|
|
|
|
|
|
315
|
|
|
|
|
|
|
=over 4 |
316
|
|
|
|
|
|
|
|
317
|
|
|
|
|
|
|
=item * |
318
|
|
|
|
|
|
|
|
319
|
|
|
|
|
|
|
Created/ Updated record in JSON C<[{_id:...., col1:. ...., }, ...]> |
320
|
|
|
|
|
|
|
|
321
|
|
|
|
|
|
|
=back |
322
|
|
|
|
|
|
|
|
323
|
|
|
|
|
|
|
=head1 AUTHOR |
324
|
|
|
|
|
|
|
|
325
|
|
|
|
|
|
|
Emiliano Bruni |
326
|
|
|
|
|
|
|
|
327
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
328
|
|
|
|
|
|
|
|
329
|
|
|
|
|
|
|
This software is copyright (c) 2022 by Emiliano Bruni. |
330
|
|
|
|
|
|
|
|
331
|
|
|
|
|
|
|
This is free software; you can redistribute it and/or modify it under |
332
|
|
|
|
|
|
|
the same terms as the Perl 5 programming language system itself. |
333
|
|
|
|
|
|
|
|
334
|
|
|
|
|
|
|
=cut |
335
|
|
|
|
|
|
|
|
336
|
|
|
|
|
|
|
__END__ |