File Coverage

blib/lib/PONAPI/DAO/Request/Role/UpdateLike.pm
Criterion Covered Total %
statement 36 37 97.3
branch 9 10 90.0
condition 2 6 33.3
subroutine 8 8 100.0
pod n/a
total 55 61 90.1


line stmt bran cond sub pod time code
1             # ABSTRACT: DAO request role - `update` & like related functionality
2             package PONAPI::DAO::Request::Role::UpdateLike;
3              
4 8     8   4787 use Moose::Role;
  8         22  
  8         65  
5              
6 8     8   45302 use PONAPI::Constants;
  8         23  
  8         1221  
7 8     8   2925 use PONAPI::Exception;
  8         30  
  8         2834  
8              
9             has respond_to_updates_with_200 => (
10             is => 'ro',
11             isa => 'Bool',
12             );
13              
14             has update_nothing_status => (
15             is => 'ro',
16             isa => 'Int',
17             # http://jsonapi.org/format/#crud-updating-relationship-responses-204
18             # Basically, we return a 204, UNLESS it's an ->update() call, in which
19             # case it has to return a 404:
20             # http://jsonapi.org/format/#crud-updating-responses-404
21             default => sub { 204 },
22             );
23              
24             sub _verify_update_response {
25 29     29   133 my ( $self, $ret, @extra ) = @_;
26              
27             PONAPI::Exception->throw(
28             internal => 1,
29             message => "update-like operation returned an unexpected value"
30 29 100       234 ) unless exists $PONAPI_UPDATE_RETURN_VALUES{$ret};
31              
32 25         1155 my $doc = $self->document;
33 25 50 33     182 return if $doc->has_errors or $doc->has_status;
34              
35 25 100       3532 if ( $self->respond_to_updates_with_200 ) {
36 2         67 $doc->set_status(200);
37 2 100       76 $self->repository->retrieve(
38             type => $self->type,
39             id => $self->id,
40             document => $doc,
41             ) if $ret == PONAPI_UPDATED_EXTENDED;
42 2         18 return 1;
43             }
44             else {
45 23         886 $doc->set_status(202);
46             }
47              
48 23         715 return 1;
49             }
50              
51             sub _add_success_meta {
52 25     25   95 my ($self, $return_status) = @_;
53              
54 25         116 my $resource = $self->_get_resource_for_meta;
55              
56 25         97 my $detail = "successfully modified $resource";
57 25 100       106 if ( $return_status == PONAPI_UPDATED_NOTHING ) {
58 5         183 my $status = $self->update_nothing_status;
59 5         139 $self->document->set_status($status);
60             # This should NEVER show up in a response, because it would
61             # mean a 204 with a body; I'm still leaving it here because
62             # a 204+body is preferable to a server error due to an empty
63             # document.
64 5         96 $detail = "modified nothing for $resource"
65             }
66              
67 25         805 $self->document->add_meta( detail => $detail );
68             }
69              
70             sub _get_resource_for_meta {
71 25     25   68 my $self = shift;
72              
73             my $link = $self->document->req_path
74 25   33     754 // ( join "/" => grep defined, '', @{$self}{qw/type id rel_type/} );
  0         0  
75              
76 25         1247 return $link . " => " . $self->json->encode( $self->data );
77             }
78              
79 8     8   70 no PONAPI::Constants;
  8         21  
  8         240  
80 8     8   54 no Moose::Role; 1;
  8         22  
  8         84  
81              
82             __END__
83              
84             =pod
85              
86             =encoding UTF-8
87              
88             =head1 NAME
89              
90             PONAPI::DAO::Request::Role::UpdateLike - DAO request role - `update` & like related functionality
91              
92             =head1 VERSION
93              
94             version 0.003003
95              
96             =head1 AUTHORS
97              
98             =over 4
99              
100             =item *
101              
102             Mickey Nasriachi <mickey@cpan.org>
103              
104             =item *
105              
106             Stevan Little <stevan@cpan.org>
107              
108             =item *
109              
110             Brian Fraser <hugmeir@cpan.org>
111              
112             =back
113              
114             =head1 COPYRIGHT AND LICENSE
115              
116             This software is copyright (c) 2019 by Mickey Nasriachi, Stevan Little, Brian Fraser.
117              
118             This is free software; you can redistribute it and/or modify it under
119             the same terms as the Perl 5 programming language system itself.
120              
121             =cut