line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
2
|
|
|
2
|
|
17059
|
% my $a = shift; |
|
2
|
|
|
2
|
|
5
|
|
|
2
|
|
|
2
|
|
14
|
|
|
2
|
|
|
2
|
|
174
|
|
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
1760
|
|
|
2
|
|
|
|
|
16729
|
|
|
2
|
|
|
|
|
6
|
|
|
2
|
|
|
|
|
14
|
|
|
2
|
|
|
|
|
199
|
|
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
1629
|
|
2
|
|
|
|
|
|
|
package <%= $a->{class} %>; |
3
|
|
|
|
|
|
|
use Mojo::Base '<%= $a->{controller_namespace} %>', -signatures; |
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
# GET /<%= $a->{t} %>/create |
7
|
|
|
|
|
|
|
# Display form for creating resource in table <%= $a->{t} %>. |
8
|
|
|
|
|
|
|
sub create($c) { |
9
|
|
|
|
|
|
|
return $c->render(<%= $a->{t} %> => {}) |
10
|
|
|
|
|
|
|
} |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
# POST /<%= $a->{t} %> |
13
|
|
|
|
|
|
|
# Add a new record to table <%= $a->{t} %>. |
14
|
|
|
|
|
|
|
sub store($c) { |
15
|
|
|
|
|
|
|
if($c->current_route =~ /^api\./) { #invoked via OpenAPI |
16
|
|
|
|
|
|
|
$c->openapi->valid_input or return; |
17
|
|
|
|
|
|
|
my $in = $c->validation->output; |
18
|
|
|
|
|
|
|
my $id = $c-><%= $a->{t} %>->add($in); |
19
|
|
|
|
|
|
|
$c->res->headers->location($c->url_for("api.show_<%= $a->{t} %>", id => $id)->to_string); |
20
|
|
|
|
|
|
|
return $c->render(openapi => '', status => 201); |
21
|
|
|
|
|
|
|
} |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
# 1. Validate input |
24
|
|
|
|
|
|
|
my $validation = $c->_validation; |
25
|
|
|
|
|
|
|
return $c->render(action => 'create', <%= $a->{t} %> => {}) |
26
|
|
|
|
|
|
|
if $validation->has_error; |
27
|
|
|
|
|
|
|
# 2. Insert it into the database |
28
|
|
|
|
|
|
|
my $id = $c-><%= $a->{t} %>->add($validation->output); |
29
|
|
|
|
|
|
|
# 3. Prepare the response data or just return "201 Created" |
30
|
|
|
|
|
|
|
# See https://developer.mozilla.org/docs/Web/HTTP/Status/201 |
31
|
|
|
|
|
|
|
return $c->redirect_to('show_<%= $a->{t} %>', id => $id); |
32
|
|
|
|
|
|
|
} |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
# GET /<%= $a->{t} %>/:id/edit |
35
|
|
|
|
|
|
|
# Display form for edititing resource in table <%= $a->{t} %>. |
36
|
|
|
|
|
|
|
sub edit($c) { |
37
|
|
|
|
|
|
|
return $c->render(<%= $a->{t} %> => $c-><%= $a->{t} %>->find($c->param('id'))); |
38
|
|
|
|
|
|
|
} |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
# PUT /<%= $a->{t} %>/:id |
41
|
|
|
|
|
|
|
# Update the record in table <%= $a->{t} %> |
42
|
|
|
|
|
|
|
sub update($c) { |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
# Validate input |
45
|
|
|
|
|
|
|
my $validation = $c->_validation; |
46
|
|
|
|
|
|
|
return $c->render(action => 'edit', <%= $a->{t} %> => {}) |
47
|
|
|
|
|
|
|
if $validation->has_error; |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
# Update the record |
50
|
|
|
|
|
|
|
my $id = $c->param('id'); |
51
|
|
|
|
|
|
|
$c-><%= $a->{t} %>->save($id, $validation->output); |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
# Redirect to the updated record or just send "204 No Content" |
54
|
|
|
|
|
|
|
# See https://developer.mozilla.org/docs/Web/HTTP/Status/204 |
55
|
|
|
|
|
|
|
return $c->redirect_to('show_<%= $a->{t} %>', id => $id); |
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
# GET /<%= $a->{t} %>/:id |
59
|
|
|
|
|
|
|
# Display a record from table <%= $a->{t} %>. |
60
|
|
|
|
|
|
|
sub show($c) { |
61
|
|
|
|
|
|
|
my $row = $c-><%= $a->{t} %>->find($c->param('id')); |
62
|
|
|
|
|
|
|
if($c->current_route =~ /^api\./) { #invoked via OpenAPI |
63
|
|
|
|
|
|
|
return $c->render(openapi=> { |
64
|
|
|
|
|
|
|
errors => [{path => $c->url_for->to_string, message => 'Not Found'}] |
65
|
|
|
|
|
|
|
}, status => 404) unless $row; |
66
|
|
|
|
|
|
|
return $c->render(openapi => $row); |
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
$row = $c-><%= $a->{t} %>->find($c->param('id')); |
69
|
|
|
|
|
|
|
return $c->render(text => $c->res->default_message(404), status => 404) |
70
|
|
|
|
|
|
|
unless $row; |
71
|
|
|
|
|
|
|
return $c->render(<%= $a->{t} %> => $row); |
72
|
|
|
|
|
|
|
} |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
# GET /<%= $a->{t} %> |
75
|
|
|
|
|
|
|
# List resources from table <%= $a->{t} %>. |
76
|
|
|
|
|
|
|
## no critic qw(Subroutines::ProhibitBuiltinHomonyms) |
77
|
|
|
|
|
|
|
sub index($c) { |
78
|
|
|
|
|
|
|
if($c->current_route =~ /^api\./) { #invoked via OpenAPI |
79
|
|
|
|
|
|
|
$c->openapi->valid_input or return; |
80
|
|
|
|
|
|
|
my $input = $c->validation->output; |
81
|
|
|
|
|
|
|
return $c->render(openapi => $c-><%= $a->{t} %>->all($input)); |
82
|
|
|
|
|
|
|
} |
83
|
|
|
|
|
|
|
return $c->render(<%= $a->{t} %> => $c-><%= $a->{t} %>->all); |
84
|
|
|
|
|
|
|
} |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
# DELETE /<%= $a->{t} %>/:id |
87
|
|
|
|
|
|
|
sub remove($c) { |
88
|
|
|
|
|
|
|
if($c->current_route =~ /^api\./) { #invoked via OpenAPI |
89
|
|
|
|
|
|
|
$c->openapi->valid_input or return; |
90
|
|
|
|
|
|
|
my $input = $c->validation->output; |
91
|
|
|
|
|
|
|
my $row = $c-><%= $a->{t} %>->find($input->{id}); |
92
|
|
|
|
|
|
|
$c->render(openapi=> { |
93
|
|
|
|
|
|
|
errors => [{path => $c->url_for, message => 'Not Found'}] |
94
|
|
|
|
|
|
|
}, status => 404) && return unless $row; |
95
|
|
|
|
|
|
|
$c-><%= $a->{t} %>->remove($input->{id}); |
96
|
|
|
|
|
|
|
return $c->render(openapi => '', status=> 204 ); |
97
|
|
|
|
|
|
|
} |
98
|
|
|
|
|
|
|
$c-><%= $a->{t} %>->remove($c->param('id')); |
99
|
|
|
|
|
|
|
return $c->redirect_to('home_<%= $a->{t} %>'); |
100
|
|
|
|
|
|
|
} |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
# Validation for actions that store or update |
104
|
|
|
|
|
|
|
sub _validation($c) { |
105
|
|
|
|
|
|
|
my $v = $c->validation; |
106
|
|
|
|
|
|
|
# Add validation rules for the record to be stored in the database |
107
|
|
|
|
|
|
|
%= $a->{validation} |
108
|
|
|
|
|
|
|
return $v; |
109
|
|
|
|
|
|
|
} |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
1; |
112
|
|
|
|
|
|
|
|