| 1 | package WebAPI::DBIC::Resource::Role::ItemWritable; | ||||||||||||||||||||
| 2 | 25 | $WebAPI::DBIC::Resource::Role::ItemWritable::VERSION = '0.004002'; | |||||||||||||||||||
| 3 | |||||||||||||||||||||
| 4 | 25 | ▶ | use Carp qw(croak confess); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | 25 | ▶ | use Devel::Dwarn; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | |||||||||||||||||||||
| 7 | 25 | ▶ | use Moo::Role; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | |||||||||||||||||||||
| 9 | |||||||||||||||||||||
| 10 | 25 | requires 'render_item_into_body'; | |||||||||||||||||||
| 11 | 25 | requires 'decode_json'; | |||||||||||||||||||
| 12 | 25 | requires 'item'; | |||||||||||||||||||
| 13 | 25 | requires 'param'; | |||||||||||||||||||
| 14 | 25 | requires 'prefetch'; | |||||||||||||||||||
| 15 | 25 | requires 'request'; | |||||||||||||||||||
| 16 | 25 | requires 'response'; | |||||||||||||||||||
| 17 | 25 | requires 'path_for_item'; | |||||||||||||||||||
| 18 | |||||||||||||||||||||
| 19 | |||||||||||||||||||||
| 20 | # By default the DBIx::Class::Row update() call will only update the | ||||||||||||||||||||
| 21 | # columns where %$hal contains different values to the ones in $item. | ||||||||||||||||||||
| 22 | # This is usually a useful optimization but not always. So we provide | ||||||||||||||||||||
| 23 | # a way to disable it on individual resources. | ||||||||||||||||||||
| 24 | 25 | has skip_dirty_check => ( | |||||||||||||||||||
| 25 | is => 'rw', | ||||||||||||||||||||
| 26 | ); | ||||||||||||||||||||
| 27 | |||||||||||||||||||||
| 28 | 25 | has _pre_update_resource_method => ( | |||||||||||||||||||
| 29 | is => 'rw', | ||||||||||||||||||||
| 30 | ); | ||||||||||||||||||||
| 31 | |||||||||||||||||||||
| 32 | 25 | has content_types_accepted => ( | |||||||||||||||||||
| 33 | is => 'lazy', | ||||||||||||||||||||
| 34 | ); | ||||||||||||||||||||
| 35 | |||||||||||||||||||||
| 36 | 10 | ▶ | sub _build_content_types_accepted { | ||||||||||||||||||
|
Subroutine
_build_content_types_accepted: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 37 | 10 | return [ {'application/vnd.wapid+json' => 'from_plain_json'} ] | |||||||||||||||||||
| 38 | } | ||||||||||||||||||||
| 39 | |||||||||||||||||||||
| 40 | |||||||||||||||||||||
| 41 | 8 | ▶ | sub from_plain_json { | ||||||||||||||||||
|
Subroutine
from_plain_json: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 42 | 8 | my $self = shift; | |||||||||||||||||||
| 43 | 8 | my $data = $self->decode_json( $self->request->content ); | |||||||||||||||||||
| 44 | 8 | $self->update_resource($data, is_put_replace => 0); | |||||||||||||||||||
| 45 | 8 | return; | |||||||||||||||||||
| 46 | } | ||||||||||||||||||||
| 47 | |||||||||||||||||||||
| 48 | |||||||||||||||||||||
| 49 | 70 | ▶ | around 'allowed_methods' => sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.0
| |||||||||||||||||||||
| 50 | 70 | my $orig = shift; | |||||||||||||||||||
| 51 | 70 | my $self = shift; | |||||||||||||||||||
| 52 | |||||||||||||||||||||
| 53 | 70 | my $methods = $self->$orig(); | |||||||||||||||||||
| 54 | |||||||||||||||||||||
| 55 | 70 | ▶ | $methods = [ qw(PUT DELETE), @$methods ] if $self->writable; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 56 | |||||||||||||||||||||
| 57 | 70 | return $methods; | |||||||||||||||||||
| 58 | 25 | }; | |||||||||||||||||||
| 59 | |||||||||||||||||||||
| 60 | |||||||||||||||||||||
| 61 | 8 | ▶ | sub delete_resource { return $_[0]->item->delete } | ||||||||||||||||||
|
Subroutine
delete_resource: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 62 | |||||||||||||||||||||
| 63 | |||||||||||||||||||||
| 64 | 10 | ▶ | sub _do_update_resource { | ||||||||||||||||||
|
Subroutine
_do_update_resource: called CC=4 SCAR=14.0
| |||||||||||||||||||||
| 65 | 10 | my ($self, $item, $hal, $result_class) = @_; | |||||||||||||||||||
| 66 | |||||||||||||||||||||
| 67 | # provide a hook for richer behaviour, eg HAL | ||||||||||||||||||||
| 68 | 10 | my $_pre_update_resource_method = $self->_pre_update_resource_method; | |||||||||||||||||||
| 69 | 10 | ▶ | $self->$_pre_update_resource_method($item, $hal, $result_class) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 70 | if $_pre_update_resource_method; | ||||||||||||||||||||
| 71 | |||||||||||||||||||||
| 72 | # By default the DBIx::Class::Row update() call below will only update the | ||||||||||||||||||||
| 73 | # columns where %$hal contains different values to the ones in $item | ||||||||||||||||||||
| 74 | # This is usually a useful optimization but not always. So we provide | ||||||||||||||||||||
| 75 | # a way to disable it on individual resources. | ||||||||||||||||||||
| 76 | 10 | ▶ | if ($self->skip_dirty_check) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 77 | 0 | $item->make_column_dirty($_) for keys %$hal; | |||||||||||||||||||
| 78 | } | ||||||||||||||||||||
| 79 | |||||||||||||||||||||
| 80 | # Note that update() calls set_inflated_columns() | ||||||||||||||||||||
| 81 | 10 | $item->update($hal); | |||||||||||||||||||
| 82 | |||||||||||||||||||||
| 83 | # XXX discard_changes causes a refetch of the record for prefetch | ||||||||||||||||||||
| 84 | # perhaps worth trying to avoid the discard if not required | ||||||||||||||||||||
| 85 | 10 | $item->discard_changes(); | |||||||||||||||||||
| 86 | |||||||||||||||||||||
| 87 | 10 | return $item; | |||||||||||||||||||
| 88 | } | ||||||||||||||||||||
| 89 | |||||||||||||||||||||
| 90 | |||||||||||||||||||||
| 91 | 10 | ▶ | sub update_resource { | ||||||||||||||||||
|
Subroutine
update_resource: called CC=2 SCAR=9.3
Pod
undocumented
| |||||||||||||||||||||
| 92 | 10 | my ($self, $hal, %opts) = @_; | |||||||||||||||||||
| 93 | 10 | my $is_put_replace = delete $opts{is_put_replace}; | |||||||||||||||||||
| 94 | 0 | ▶ | croak "update_resource: invalid options: @{[ keys %opts ]}" | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 95 | if %opts; | ||||||||||||||||||||
| 96 | |||||||||||||||||||||
| 97 | 10 | my $schema = $self->item->result_source->schema; | |||||||||||||||||||
| 98 | # XXX perhaps the transaction wrapper belongs higher in the stack | ||||||||||||||||||||
| 99 | # but it has to be below the auth layer which switches schemas | ||||||||||||||||||||
| 100 | 10 | ▶ | $schema->txn_do(sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=10 SCAR=34.3
| |||||||||||||||||||||
| 101 | |||||||||||||||||||||
| 102 | 10 | my $item; | |||||||||||||||||||
| 103 | 10 | ▶ | if ($is_put_replace) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 104 | # PUT == http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html#sec9.6 | ||||||||||||||||||||
| 105 | |||||||||||||||||||||
| 106 | # Using delete() followed by create() is a strict implementation | ||||||||||||||||||||
| 107 | # of treating PUT on an item as a REPLACE, but it might not be ideal. | ||||||||||||||||||||
| 108 | # Specifically it requires any FKs to be DEFERRED and it'll less | ||||||||||||||||||||
| 109 | # efficient than a simple UPDATE. There's also a concern that if | ||||||||||||||||||||
| 110 | # the REST API only has a partial view of the resource, ie not all | ||||||||||||||||||||
| 111 | # columns, then do we want the original deleted if the 'hidden' | ||||||||||||||||||||
| 112 | # fields can't be set? | ||||||||||||||||||||
| 113 | # So this could me made optional on a per-resource-class basis, | ||||||||||||||||||||
| 114 | # and/or via a request parameter. | ||||||||||||||||||||
| 115 | |||||||||||||||||||||
| 116 | # we require PK fields to at least be defined | ||||||||||||||||||||
| 117 | # XXX we ought to check that they match the URL since a PUT is | ||||||||||||||||||||
| 118 | # required to store the entity "under the supplied Request-URI". | ||||||||||||||||||||
| 119 | # XXX throw proper exception | ||||||||||||||||||||
| 120 | defined $hal->{$_} or die "missing PK '$_'\n" | ||||||||||||||||||||
| 121 | 0 | ▶ | for $self->set->result_source->primary_columns; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 122 | |||||||||||||||||||||
| 123 | 0 | my $old_item = $self->item; # XXX might already be gone since the find() | |||||||||||||||||||
| 124 | 0 | ▶ | $old_item->delete if $old_item; # XXX might already be gone since the find() | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 125 | |||||||||||||||||||||
| 126 | 0 | my $links = delete $hal->{_links}; | |||||||||||||||||||
| 127 | 0 | my $meta = delete $hal->{_meta}; | |||||||||||||||||||
| 128 | 0 | ▶ | my $embedded = delete $hal->{_embedded} && die "_embedded not supported here (yet?)\n"; | ||||||||||||||||||
|
Condition0%0/2
delete $hal->{'_embedded'} && die("_embedded not supported here (yet?)\n")
MC/DC0%0/1
delete $hal->{'_embedded'} && die("_embedded not supported here (yet?)\n")
delete $hal->{'_embedded'}
Truth table0%0/2
A
A= delete $hal->{'_embedded'}
| |||||||||||||||||||||
| 129 | |||||||||||||||||||||
| 130 | 0 | $item = $self->set->create($hal); # handles deflation | |||||||||||||||||||
| 131 | |||||||||||||||||||||
| 132 | 0 | ▶ | $self->response->header('Location' => $self->path_for_item($item)) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 133 | unless $old_item; # set Location and thus 201 if Created not modified | ||||||||||||||||||||
| 134 | } | ||||||||||||||||||||
| 135 | else { | ||||||||||||||||||||
| 136 | 10 | $item = $self->_do_update_resource($self->item, $hal, $self->item->result_class); | |||||||||||||||||||
| 137 | } | ||||||||||||||||||||
| 138 | |||||||||||||||||||||
| 139 | 10 | $self->item($item); | |||||||||||||||||||
| 140 | |||||||||||||||||||||
| 141 | # called here because create_path() is too late for WM | ||||||||||||||||||||
| 142 | # and we need it to happen inside the transaction for rollback=1 to work | ||||||||||||||||||||
| 143 | # XXX requires 'self' prefetch to get any others | ||||||||||||||||||||
| 144 | 10 | ▶ | $self->render_item_into_body() if grep {defined $_->{self}} @{$self->prefetch||[]}; | ||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$self->prefetch || []
MC/DC0%0/1
$self->prefetch || []
$self->prefetch
Truth table50%1/2
A
A= $self->prefetch
| |||||||||||||||||||||
| 145 | |||||||||||||||||||||
| 146 | 10 | ▶ | $schema->txn_rollback if $self->param('rollback'); # XXX | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 147 | 10 | }); | |||||||||||||||||||
| 148 | 10 | return; | |||||||||||||||||||
| 149 | } | ||||||||||||||||||||
| 150 | |||||||||||||||||||||
| 151 | 25 | 1; | |||||||||||||||||||
| 152 | |||||||||||||||||||||
| 153 | __END__ | ||||||||||||||||||||