| 1 | package Articulate::Service::Simple; | ||||||||||||||
| 2 | |||||||||||||||
| 3 | 5 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 4 | 5 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 5 | 5 | ▶ | use Articulate::Syntax; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 6 | |||||||||||||||
| 7 | 5 | ▶ | use Moo; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 8 | 5 | with 'Articulate::Role::Service'; | |||||||||||||
| 9 | |||||||||||||||
| 10 | 5 | ▶ | use Try::Tiny; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 11 | 5 | ▶ | use Scalar::Util qw(blessed); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 12 | |||||||||||||||
| 13 | =head1 NAME | ||||||||||||||
| 14 | |||||||||||||||
| 15 | Articulate::Service::Simple - provide create, read, update, delete | ||||||||||||||
| 16 | |||||||||||||||
| 17 | =cut | ||||||||||||||
| 18 | |||||||||||||||
| 19 | =head1 METHODS | ||||||||||||||
| 20 | |||||||||||||||
| 21 | =head3 handle_create | ||||||||||||||
| 22 | |||||||||||||||
| 23 | |||||||||||||||
| 24 | create => { | ||||||||||||||
| 25 | meta => {} | ||||||||||||||
| 26 | content => '...', | ||||||||||||||
| 27 | location => '...' | ||||||||||||||
| 28 | } | ||||||||||||||
| 29 | |||||||||||||||
| 30 | Creates new content. Throws an error if the content already exists or if the user has no write permission on that location. | ||||||||||||||
| 31 | |||||||||||||||
| 32 | =head3 handle_read | ||||||||||||||
| 33 | |||||||||||||||
| 34 | read => { | ||||||||||||||
| 35 | location => '...' | ||||||||||||||
| 36 | } | ||||||||||||||
| 37 | |||||||||||||||
| 38 | Retrieves the content at that location. Throws an error if the content does not exist or if the user has no read permission on that location. | ||||||||||||||
| 39 | |||||||||||||||
| 40 | =head3 handle_update | ||||||||||||||
| 41 | |||||||||||||||
| 42 | update => { | ||||||||||||||
| 43 | meta => {} | ||||||||||||||
| 44 | content => '...', | ||||||||||||||
| 45 | location => '...' | ||||||||||||||
| 46 | } | ||||||||||||||
| 47 | |||||||||||||||
| 48 | Updates the content at that location. Throws an error if the content does not exist or if the user has no write permission on that location. | ||||||||||||||
| 49 | |||||||||||||||
| 50 | =head3 handle_delete | ||||||||||||||
| 51 | |||||||||||||||
| 52 | delete => { | ||||||||||||||
| 53 | location => '...' | ||||||||||||||
| 54 | } | ||||||||||||||
| 55 | |||||||||||||||
| 56 | Deletes the content at that location. Throws an error if the content does not exist or if the user has no write permission on that location. | ||||||||||||||
| 57 | |||||||||||||||
| 58 | =cut | ||||||||||||||
| 59 | |||||||||||||||
| 60 | sub handle_create { | ||||||||||||||
| 61 | 1 | ▶ | my $self = shift; | ||||||||||||
|
Subroutine
handle_create: called CC=6 SCAR=18.6
Pod
documented
| |||||||||||||||
| 62 | 1 | my $request = shift; | |||||||||||||
| 63 | my $item = | ||||||||||||||
| 64 | blessed $request->data ? $request->data : $self->construction->construct( | ||||||||||||||
| 65 | { | ||||||||||||||
| 66 | meta => {}, | ||||||||||||||
| 67 | 1 | ▶ | ( %{ $request->data } ? %{ $request->data } : () ), | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 68 | } | ||||||||||||||
| 69 | ); | ||||||||||||||
| 70 | 1 | my $location = $item->location; | |||||||||||||
| 71 | |||||||||||||||
| 72 | 1 | my $user = $request->user_id; | |||||||||||||
| 73 | 1 | my $permission = $self->authorisation->permitted( $user, write => $location ); | |||||||||||||
| 74 | 1 | ▶ | if ($permission) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 75 | |||||||||||||||
| 76 | 1 | ▶ | throw_error 'AlreadyExists' if $self->storage->item_exists($location); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 77 | |||||||||||||||
| 78 | 1 | ▶ | $self->validation->validate($item) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 79 | or throw_error BadRequest => 'The content did not validate'; | ||||||||||||||
| 80 | 1 | $self->enrichment->enrich( $item, $request ); # this will throw if it fails | |||||||||||||
| 81 | 1 | $self->storage->create_item($item); # this will throw if it fails | |||||||||||||
| 82 | |||||||||||||||
| 83 | 1 | my $item_class = $item->location->[-2]; | |||||||||||||
| 84 | 1 | $self->augmentation->augment($item); # this will throw if it fails | |||||||||||||
| 85 | |||||||||||||||
| 86 | return response $item_class, { | ||||||||||||||
| 87 | $item_class => { | ||||||||||||||
| 88 | schema => $item->meta->{schema}, | ||||||||||||||
| 89 | 1 | content => $item->content, | |||||||||||||
| 90 | location => $item->location, # as string or arrayref? | ||||||||||||||
| 91 | }, | ||||||||||||||
| 92 | }; | ||||||||||||||
| 93 | } | ||||||||||||||
| 94 | else { | ||||||||||||||
| 95 | 0 | throw_error Forbidden => $permission->reason; | |||||||||||||
| 96 | } | ||||||||||||||
| 97 | |||||||||||||||
| 98 | } | ||||||||||||||
| 99 | |||||||||||||||
| 100 | sub handle_read { | ||||||||||||||
| 101 | 1 | ▶ | my $self = shift; | ||||||||||||
|
Subroutine
handle_read: called CC=3 SCAR=11.2
Pod
documented
| |||||||||||||||
| 102 | 1 | my $request = shift; | |||||||||||||
| 103 | 1 | my $location = loc $request->data->{location}; | |||||||||||||
| 104 | 1 | my $user = $request->user_id; | |||||||||||||
| 105 | 1 | my $permission = $self->authorisation->permitted( $user, read => $location ); | |||||||||||||
| 106 | 1 | ▶ | if ($permission) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 107 | 1 | ▶ | throw_error 'NotFound' unless $self->storage->item_exists($location); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 108 | 1 | my $item = $self->construction->construct( | |||||||||||||
| 109 | { | ||||||||||||||
| 110 | meta => $self->storage->get_meta_cached($location), | ||||||||||||||
| 111 | content => $self->storage->get_content_cached($location), | ||||||||||||||
| 112 | location => $location, | ||||||||||||||
| 113 | } | ||||||||||||||
| 114 | ); | ||||||||||||||
| 115 | |||||||||||||||
| 116 | 1 | my $item_class = $item->location->[-2]; | |||||||||||||
| 117 | |||||||||||||||
| 118 | 1 | $self->augmentation->augment($item); # or throw | |||||||||||||
| 119 | |||||||||||||||
| 120 | return response $item_class => { | ||||||||||||||
| 121 | $item_class => { | ||||||||||||||
| 122 | schema => $item->meta->{schema}, | ||||||||||||||
| 123 | 1 | content => $item->content, | |||||||||||||
| 124 | }, | ||||||||||||||
| 125 | }; | ||||||||||||||
| 126 | } | ||||||||||||||
| 127 | else { | ||||||||||||||
| 128 | 0 | return throw_error Forbidden => $permission->reason; | |||||||||||||
| 129 | } | ||||||||||||||
| 130 | } | ||||||||||||||
| 131 | |||||||||||||||
| 132 | sub handle_update { | ||||||||||||||
| 133 | 1 | ▶ | my $self = shift; | ||||||||||||
|
Subroutine
handle_update: called CC=9 SCAR=23.5
Pod
documented
| |||||||||||||||
| 134 | 1 | my $request = shift; | |||||||||||||
| 135 | |||||||||||||||
| 136 | my $item = | ||||||||||||||
| 137 | blessed $request->data ? $request->data : $self->construction->construct( | ||||||||||||||
| 138 | { | ||||||||||||||
| 139 | meta => {}, | ||||||||||||||
| 140 | 1 | ▶ | ( %{ $request->data } ? %{ $request->data } : () ), | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 141 | } | ||||||||||||||
| 142 | ); | ||||||||||||||
| 143 | 1 | my $location = $item->location; | |||||||||||||
| 144 | |||||||||||||||
| 145 | 1 | my $user = $request->user_id; | |||||||||||||
| 146 | 1 | my $permission = $self->authorisation->permitted( $user, write => $location ); | |||||||||||||
| 147 | 1 | ▶ | if ($permission) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 148 | |||||||||||||||
| 149 | 1 | ▶ | throw_error 'NotFound' unless $self->storage->item_exists($location); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 150 | |||||||||||||||
| 151 | 1 | ▶ | $self->validation->validate($item) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 152 | or throw_error BadRequest => 'The content did not validate'; # or throw | ||||||||||||||
| 153 | 1 | $self->enrichment->enrich( $item, $request ); # this will throw if it fails | |||||||||||||
| 154 | 1 | ▶ | $self->storage->set_meta($item) or throw_error 'Internal'; # or throw | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 155 | 1 | ▶ | $self->storage->set_content($item) or throw_error 'Internal'; # or throw | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 156 | |||||||||||||||
| 157 | 1 | my $item_class = $item->location->[-2]; | |||||||||||||
| 158 | 1 | ▶ | $self->augmentation->augment($item) or throw_error 'Internal'; # or throw | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 159 | |||||||||||||||
| 160 | return response $item_class, { | ||||||||||||||
| 161 | $item_class => { | ||||||||||||||
| 162 | schema => $item->meta->{schema}, | ||||||||||||||
| 163 | 1 | content => $item->content, | |||||||||||||
| 164 | location => $item->location, # as string or arrayref? | ||||||||||||||
| 165 | }, | ||||||||||||||
| 166 | }; | ||||||||||||||
| 167 | } | ||||||||||||||
| 168 | else { | ||||||||||||||
| 169 | 0 | return throw_error Forbidden => $permission->reason; | |||||||||||||
| 170 | } | ||||||||||||||
| 171 | |||||||||||||||
| 172 | } | ||||||||||||||
| 173 | |||||||||||||||
| 174 | sub handle_delete { | ||||||||||||||
| 175 | 1 | ▶ | my $self = shift; | ||||||||||||
|
Subroutine
handle_delete: called CC=4 SCAR=14.5
Pod
documented
| |||||||||||||||
| 176 | 1 | my $request = shift; | |||||||||||||
| 177 | |||||||||||||||
| 178 | 1 | my $location = loc $request->data->{location}; | |||||||||||||
| 179 | |||||||||||||||
| 180 | 1 | my $user = $request->user_id; | |||||||||||||
| 181 | 1 | my $permission = $self->authorisation->permitted( $user, write => $location ); | |||||||||||||
| 182 | 1 | ▶ | if ($permission) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 183 | 1 | ▶ | throw_error 'NotFound' unless $self->storage->item_exists($location); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 184 | 1 | ▶ | $self->storage->delete_item($location) or throw_error 'Internal'; # or throw | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 185 | 1 | return response 'success', {}; | |||||||||||||
| 186 | } | ||||||||||||||
| 187 | else { | ||||||||||||||
| 188 | 0 | return throw_error Forbidden => $permission->reason; | |||||||||||||
| 189 | } | ||||||||||||||
| 190 | |||||||||||||||
| 191 | } | ||||||||||||||
| 192 | |||||||||||||||
| 193 | 5 | 1; | |||||||||||||