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