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