Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions lib/WebAPI/DBIC.pm
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,13 @@ Also see L</prefetch>.

=head2 GET Item - Optional Parameters

=head3 join

The Join parameter behaves in the same way as the prefetch parameter excepting in one important
fashion; use of the join command will only return the primary resource or the specific fields
requested. It should primarily be used for filtering or sorting the primary resource on a
secondary joined resource. see L</prefetch> for information on the format of the join command.

=head3 prefetch

Prefetch is a mechanism in DBIx::Class by which related resultsets can be returned
Expand Down
20 changes: 9 additions & 11 deletions lib/WebAPI/DBIC/Resource/HAL/Role/DBIC.pm
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,12 @@ use JSON::MaybeXS qw(JSON);

use Moo::Role;


requires 'get_url_for_item_relationship';
requires 'render_item_as_plain_hash';
requires 'path_for_item';
requires 'add_params_to_url';
requires 'prefetch';


sub render_item_as_hal_hash {
my ($self, $item) = @_;

Expand Down Expand Up @@ -53,13 +51,19 @@ sub render_item_as_hal_hash {
return $data;
}


sub _render_prefetch {
my ($self, $item, $data, $prefetch) = @_;

while (my ($rel, $sub_rel) = each %{$prefetch}){
next if $rel eq 'self';

# Prevent joins calling the DB for no cached
# relations in the join statement
if ($self->param('join')){
next unless
defined $item->{related_resultsets}{$rel} &&
defined $item->{related_resultsets}{$rel}->get_cache;
}
my $subitem = $item->$rel();

# XXX perhaps render_item_as_hal_hash but requires cloned WM, eg without prefetch
Expand All @@ -69,21 +73,18 @@ sub _render_prefetch {
# See http://blog.stateless.co/post/13296666138/json-linking-with-hal
if (not defined $subitem) {
$data->{_embedded}{$rel} = undef; # show an explicit null from a prefetch
}
elsif ($subitem->isa('DBIx::Class::ResultSet')) { # one-to-many rel
} elsif ($subitem->isa('DBIx::Class::ResultSet')){ # one-to-many rel
my $rel_set_resource = $self->web_machine_resource(
set => $subitem,
prefetch => ref $sub_rel eq 'ARRAY' ? $sub_rel : [$sub_rel],
);
$data->{_embedded}{$rel} = $rel_set_resource->render_set_as_list_of_hal($subitem);
}
else {
} else {
$data->{_embedded}{$rel} = $self->render_item_as_plain_hash($subitem);
}
}
}


sub render_set_as_list_of_hal {
my ($self, $set, $render_method) = @_;
$render_method ||= 'render_item_as_hal_hash';
Expand All @@ -93,7 +94,6 @@ sub render_set_as_list_of_hal {
return $set_data;
}


sub render_set_as_hal {
my ($self, $set) = @_;

Expand Down Expand Up @@ -123,7 +123,6 @@ sub render_set_as_hal {
return $data;
}


sub _hal_page_links {
my ($self, $set, $base, $page_items, $total_items) = @_;

Expand Down Expand Up @@ -161,5 +160,4 @@ sub _hal_page_links {
return @link_kvs;
}


1;
11 changes: 2 additions & 9 deletions lib/WebAPI/DBIC/Resource/HAL/Role/ItemWritable.pm
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,9 @@ use Devel::Dwarn;

use Moo::Role;


requires 'decode_json';
requires 'request';

requires '_pre_update_resource_method';


around '_build_content_types_accepted' => sub {
my $orig = shift;
my $self = shift;
Expand All @@ -29,14 +25,12 @@ around '_build_content_types_accepted' => sub {

sub from_hal_json {
my $self = shift;
$self->_pre_update_resource_method( "_do_update_embedded_resources_hal" );
my $data = $self->decode_json( $self->request->content );
$self->update_resource($data, is_put_replace => 0);
return;
}


sub _do_update_embedded_resources_hal {
before '_do_update_resource' => sub {
my ($self, $item, $hal, $result_class) = @_;

my $links = delete $hal->{_links};
Expand Down Expand Up @@ -83,7 +77,6 @@ sub _do_update_embedded_resources_hal {
}

return;
}

};

1;
16 changes: 15 additions & 1 deletion lib/WebAPI/DBIC/Resource/JSONAPI/Role/DBIC.pm
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,14 @@ sub render_jsonapi_prefetch_rel {
$item_edit_rel_hooks->{$relname} = sub {
my ($jsonapi_obj, $row) = @_;

# Prevent joins calling the DB for no cached
# relations in the join statement
if ($self->param('join')){
return unless
defined $row->{related_resultsets}{$relname} &&
defined $row->{related_resultsets}{$relname}->get_cache;
}

my $subitem = $row->$relname();

my $compound_links_for_rel = $compound_links->{$rel_typename} ||= {};
Expand Down Expand Up @@ -118,7 +126,6 @@ sub render_jsonapi_response { # return top-level document hashref
next if $self->param('distinct');

while (my ($relname, $sub_rel) = each %{$prefetch}){
#warn "prefetch $prefetch - $relname, $sub_rel";
$self->render_jsonapi_prefetch_rel($set, $relname, $sub_rel, $top_links, $compound_links, $item_edit_rel_hooks);
}
}
Expand Down Expand Up @@ -180,6 +187,13 @@ sub _render_prefetch_jsonapi {
while (my ($rel, $sub_rel) = each %{$prefetch}){
next if $rel eq 'self';

# Prevent joins calling the DB for no cached
# relations in the join statement
if ($self->param('join')){
next unless
defined $item->{related_resultsets}{$rel} &&
defined $item->{related_resultsets}{$rel}->get_cache;
}
my $subitem = $item->$rel();

if (not defined $subitem) {
Expand Down
11 changes: 2 additions & 9 deletions lib/WebAPI/DBIC/Resource/JSONAPI/Role/ItemWritable.pm
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,9 @@ use Devel::Dwarn;

use Moo::Role;


requires 'decode_json';
requires 'request';

requires '_pre_update_resource_method';


around '_build_content_types_accepted' => sub {
my $orig = shift;
Expand All @@ -26,17 +23,14 @@ around '_build_content_types_accepted' => sub {
return $types;
};


sub from_jsonapi_json {
my $self = shift;
$self->_pre_update_resource_method( "_do_update_embedded_resources_jsonapi" );
my $data = $self->decode_json( $self->request->content );
$self->update_resource($data, is_put_replace => 0);
return;
}


sub _do_update_embedded_resources_jsonapi {
before '_do_update_resource' => sub {
my ($self, $item, $jsonapi, $result_class) = @_;

my $links = delete $jsonapi->{_links};
Expand Down Expand Up @@ -83,7 +77,6 @@ sub _do_update_embedded_resources_jsonapi {
}

return;
}

};

1;
23 changes: 19 additions & 4 deletions lib/WebAPI/DBIC/Resource/Role/DBICParams.pm
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,11 @@ sub _handle_search_criteria_param {
return;
}

sub _handle_join_param {
my ($self) = shift;
$self->_handle_prefetch_param(@_);
}

sub _handle_prefetch_param {
my ($self, $value) = @_;

Expand All @@ -143,10 +148,20 @@ sub _handle_prefetch_param {
$self->prefetch( $prefetch ); # include self, even if deleted below
$prefetch = [grep { !defined $_->{self}} @$prefetch];

my $prefetch_or_join = $self->param('fields') ? 'join' : 'prefetch';
Dwarn { $prefetch_or_join => $prefetch } if $ENV{WEBAPI_DBIC_DEBUG};
$self->set( $self->set->search_rs(undef, { $prefetch_or_join => $prefetch }))
if scalar @$prefetch;
my $join_args;
if ($self->param('fields') || $self->param('join')){
$join_args = {
join => $prefetch,
collapse => 1,
};
} else {
$join_args = {
prefetch => $prefetch,
};
}

Dwarn $join_args if $ENV{WEBAPI_DBIC_DEBUG};
$self->set( $self->set->search_rs(undef, $join_args)) if scalar @$prefetch;

return;
}
Expand Down
5 changes: 0 additions & 5 deletions lib/WebAPI/DBIC/Resource/Role/ItemWritable.pm
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,6 @@ sub delete_resource { return $_[0]->item->delete }
sub _do_update_resource {
my ($self, $item, $hal, $result_class) = @_;

# provide a hook for richer behaviour, eg HAL
my $_pre_update_resource_method = $self->_pre_update_resource_method;
$self->$_pre_update_resource_method($item, $hal, $result_class)
if $_pre_update_resource_method;

# By default the DBIx::Class::Row update() call below will only update the
# columns where %$hal contains different values to the ones in $item
# This is usually a useful optimization but not always. So we provide
Expand Down
Loading