File Coverage

blib/lib/Articulate/Service/Simple.pm
Criterion Covered Total %
statement 71 75 94.6
branch 18 36 50.0
condition n/a
subroutine 10 10 100.0
pod 4 4 100.0
total 103 125 82.4


line stmt bran cond sub pod time code
1             package Articulate::Service::Simple;
2              
3 5     5   3051 use strict;
  5         8  
  5         231  
4 5     5   23 use warnings;
  5         7  
  5         111  
5 5     5   25 use Articulate::Syntax;
  5         6  
  5         32  
6              
7 5     5   6560 use Moo;
  5         10  
  5         29  
8             with 'Articulate::Role::Service';
9              
10 5     5   1445 use Try::Tiny;
  5         86  
  5         305  
11 5     5   23 use Scalar::Util qw(blessed);
  5         6  
  5         3875  
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     1 1 972 my $self = shift;
62 1         2 my $request = shift;
63 1         46 my $item =
64             blessed $request->data ? $request->data : $self->construction->construct(
65             {
66             meta => {},
67 1 50       11 ( %{ $request->data } ? %{ $request->data } : () ),
  1 50       7  
68             }
69             );
70 1         8 my $location = $item->location;
71              
72 1         421 my $user = $request->user_id;
73 1         348 my $permission = $self->authorisation->permitted( $user, write => $location );
74 1 50       3 if ($permission) {
75              
76 1 50       6 throw_error 'AlreadyExists' if $self->storage->item_exists($location);
77              
78 1 50       31 $self->validation->validate($item)
79             or throw_error BadRequest => 'The content did not validate';
80 1         5 $self->enrichment->enrich( $item, $request ); # this will throw if it fails
81 1         4 $self->storage->create_item($item); # this will throw if it fails
82              
83 1         21 my $item_class = $item->location->[-2];
84 1         5 $self->augmentation->augment($item); # this will throw if it fails
85              
86 1         29 return response $item_class, {
87             $item_class => {
88             schema => $item->meta->{schema},
89             content => $item->content,
90             location => $item->location, # as string or arrayref?
91             },
92             };
93             }
94             else {
95 0         0 throw_error Forbidden => $permission->reason;
96             }
97              
98             }
99              
100             sub handle_read {
101 1     1 1 30 my $self = shift;
102 1         1 my $request = shift;
103 1         7 my $location = loc $request->data->{location};
104 1         74 my $user = $request->user_id;
105 1         12 my $permission = $self->authorisation->permitted( $user, read => $location );
106 1 50       2 if ($permission) {
107 1 50       4 throw_error 'NotFound' unless $self->storage->item_exists($location);
108 1         36 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         17 my $item_class = $item->location->[-2];
117              
118 1         5 $self->augmentation->augment($item); # or throw
119              
120 1         10 return response $item_class => {
121             $item_class => {
122             schema => $item->meta->{schema},
123             content => $item->content,
124             },
125             };
126             }
127             else {
128 0         0 return throw_error Forbidden => $permission->reason;
129             }
130             }
131              
132             sub handle_update {
133 1     1 1 21 my $self = shift;
134 1         2 my $request = shift;
135              
136 1         40 my $item =
137             blessed $request->data ? $request->data : $self->construction->construct(
138             {
139             meta => {},
140 1 50       8 ( %{ $request->data } ? %{ $request->data } : () ),
  1 50       9  
141             }
142             );
143 1         17 my $location = $item->location;
144              
145 1         18 my $user = $request->user_id;
146 1         10 my $permission = $self->authorisation->permitted( $user, write => $location );
147 1 50       3 if ($permission) {
148              
149 1 50       3 throw_error 'NotFound' unless $self->storage->item_exists($location);
150              
151 1 50       31 $self->validation->validate($item)
152             or throw_error BadRequest => 'The content did not validate'; # or throw
153 1         3 $self->enrichment->enrich( $item, $request ); # this will throw if it fails
154 1 50       8 $self->storage->set_meta($item) or throw_error 'Internal'; # or throw
155 1 50       4 $self->storage->set_content($item) or throw_error 'Internal'; # or throw
156              
157 1         21 my $item_class = $item->location->[-2];
158 1 50       4 $self->augmentation->augment($item) or throw_error 'Internal'; # or throw
159              
160 1         17 return response $item_class, {
161             $item_class => {
162             schema => $item->meta->{schema},
163             content => $item->content,
164             location => $item->location, # as string or arrayref?
165             },
166             };
167             }
168             else {
169 0         0 return throw_error Forbidden => $permission->reason;
170             }
171              
172             }
173              
174             sub handle_delete {
175 1     1 1 27 my $self = shift;
176 1         2 my $request = shift;
177              
178 1         6 my $location = loc $request->data->{location};
179              
180 1         30 my $user = $request->user_id;
181 1         10 my $permission = $self->authorisation->permitted( $user, write => $location );
182 1 50       3 if ($permission) {
183 1 50       3 throw_error 'NotFound' unless $self->storage->item_exists($location);
184 1 50       32 $self->storage->delete_item($location) or throw_error 'Internal'; # or throw
185 1         4 return response 'success', {};
186             }
187             else {
188 0           return throw_error Forbidden => $permission->reason;
189             }
190              
191             }
192              
193             1;