| 1 | package MyApp::API; | ||||||||||
| 2 | 1 | ▶ | use parent 'PAGI::Endpoint::Router'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 1 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 1 | ▶ | use Future::AsyncAwait; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | |||||||||||
| 7 | 1 | my @USERS = ( | |||||||||
| 8 | { id => 1, name => 'Alice', email => 'alice@example.com' }, | ||||||||||
| 9 | { id => 2, name => 'Bob', email => 'bob@example.com' }, | ||||||||||
| 10 | ); | ||||||||||
| 11 | |||||||||||
| 12 | 1 | ▶ | sub routes { | ||||||||
|
Subroutine
routes: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 13 | 1 | my ($self, $r) = @_; | |||||||||
| 14 | |||||||||||
| 15 | 1 | $r->get('/info' => 'get_info'); | |||||||||
| 16 | 1 | $r->get('/users' => 'list_users'); | |||||||||
| 17 | 1 | $r->get('/users/:id' => 'get_user'); | |||||||||
| 18 | 1 | $r->post('/users' => 'create_user'); | |||||||||
| 19 | } | ||||||||||
| 20 | |||||||||||
| 21 | 1 | ▶ | async sub get_info { | ||||||||
|
Subroutine
get_info: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 22 | 1 | my ($self, $ctx) = @_; | |||||||||
| 23 | 1 | $ctx->state->{metrics}{requests}++; | |||||||||
| 24 | |||||||||||
| 25 | 1 | my $config = $ctx->state->{config}; | |||||||||
| 26 | |||||||||||
| 27 | return $ctx->json({ | ||||||||||
| 28 | app => $config->{app_name}, | ||||||||||
| 29 | version => $config->{version}, | ||||||||||
| 30 | 1 | api => 'v1', | |||||||||
| 31 | }); | ||||||||||
| 32 | 1 | } | |||||||||
| 33 | |||||||||||
| 34 | 1 | ▶ | async sub list_users { | ||||||||
|
Subroutine
list_users: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 35 | 1 | my ($self, $ctx) = @_; | |||||||||
| 36 | 1 | $ctx->state->{metrics}{requests}++; | |||||||||
| 37 | 1 | return $ctx->json(\@USERS); | |||||||||
| 38 | 1 | } | |||||||||
| 39 | |||||||||||
| 40 | 0 | ▶ | async sub get_user { | ||||||||
|
Subroutine
get_user: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||
| 41 | 0 | my ($self, $ctx) = @_; | |||||||||
| 42 | 0 | $ctx->state->{metrics}{requests}++; | |||||||||
| 43 | |||||||||||
| 44 | 0 | my $id = $ctx->request->path_param('id'); | |||||||||
| 45 | 0 | my ($user) = grep { $_->{id} == $id } @USERS; | |||||||||
| 46 | |||||||||||
| 47 | 0 | ▶ | if ($user) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 48 | 0 | return $ctx->json($user); | |||||||||
| 49 | } else { | ||||||||||
| 50 | 0 | return $ctx->json({ error => 'User not found' }, status => 404); | |||||||||
| 51 | } | ||||||||||
| 52 | 1 | } | |||||||||
| 53 | |||||||||||
| 54 | 0 | ▶ | async sub create_user { | ||||||||
|
Subroutine
create_user: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 55 | 0 | my ($self, $ctx) = @_; | |||||||||
| 56 | 0 | $ctx->state->{metrics}{requests}++; | |||||||||
| 57 | |||||||||||
| 58 | 0 | my $data = await $ctx->request->json; | |||||||||
| 59 | |||||||||||
| 60 | my $new_user = { | ||||||||||
| 61 | id => scalar(@USERS) + 1, | ||||||||||
| 62 | name => $data->{name}, | ||||||||||
| 63 | email => $data->{email}, | ||||||||||
| 64 | 0 | }; | |||||||||
| 65 | 0 | push @USERS, $new_user; | |||||||||
| 66 | |||||||||||
| 67 | 0 | return $ctx->json($new_user, status => 201); | |||||||||
| 68 | 1 | } | |||||||||
| 69 | |||||||||||
| 70 | 1 | 1; | |||||||||