Catalyst-Controller-DBIC-API

 view release on metacpan or  search on metacpan

lib/Catalyst/Controller/DBIC/API.pm  view on Meta::CPAN

    }
}


sub list_format_output {
    my ( $self, $c ) = @_;

    my $rs = $c->req->current_result_set->search;
    $rs->result_class( $self->result_class ) if $self->result_class;

    try {
        my $output    = {};
        my $formatted = [];

        foreach my $row ( $rs->all ) {
            push( @$formatted, $self->row_format_output( $c, $row ) );
        }

        $output->{ $self->data_root } = $formatted;

        if ( $c->req->has_search_total_entries ) {
            $output->{ $self->total_entries_arg } =
                $c->req->search_total_entries + 0;
        }

        $c->stash->{ $self->stash_key } = $output;
    }
    catch {
        $c->log->error($_);
        $self->push_error( $c,
            { message => 'a database error has occured.' } );
        $c->detach();
    }
}


sub row_format_output {

    #my ($self, $c, $row) = @_;
    my ( $self, undef, $row ) = @_;
    return $row;    # passthrough by default
}


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

    if ( $c->req->count_objects != 1 ) {
        $c->log->error($_);
        $self->push_error( $c,
            { message => 'No objects on which to operate' } );
        $c->detach();
    }
    else {
        $c->stash->{ $self->stash_key }->{ $self->item_root } =
            $self->each_object_inflate( $c, $c->req->get_object(0)->[0] );
    }
}


sub update_or_create {
    my ( $self, $c ) = @_;

    if ( $c->req->has_objects ) {
        $self->validate_objects($c);
        $self->transact_objects( $c, sub { $self->save_objects( $c, @_ ) } );
    }
    else {
        $c->log->error($_);
        $self->push_error( $c,
            { message => 'No objects on which to operate' } );
        $c->detach();
    }
}


sub transact_objects {
    my ( $self, $c, $coderef ) = @_;

    try {
        $self->stored_result_source->schema->txn_do( $coderef,
            $c->req->objects );
    }
    catch {
        $c->log->error($_);
        $self->push_error( $c,
            { message => 'a database error has occured.' } );
        $c->detach();
    }
}


sub validate_objects {
    my ( $self, $c ) = @_;

    try {
        foreach my $obj ( $c->req->all_objects ) {
            $obj->[1] = $self->validate_object( $c, $obj );
        }
    }
    catch {
        my $err = $_;
        $c->log->error($err);
        $err =~ s/\s+at\s+.+\n$//g;
        $self->push_error( $c, { message => $err } );
        $c->detach();
    }
}


sub validate_object {
    my ( $self, $c, $obj ) = @_;
    my ( $object, $params ) = @$obj;

    my %values;
    my %requires_map = map { $_ => 1 } @{
          ( $object->in_storage )
        ? []
        : $c->stash->{create_requires} || $self->create_requires
    };

lib/Catalyst/Controller/DBIC/API.pm  view on Meta::CPAN

                        "Multiple values for '${key}': ${\Data::Dumper::Dumper($value)}";
                }
            }

            # check exists so we don't just end up with hash of undefs
            # check defined to account for default values being used
            $values{$key} = $value
                if exists $params->{$key} || defined $value;
        }
    }

    unless ( keys %values || !$object->in_storage ) {
        die 'No valid keys passed';
    }

    return \%values;
}


sub delete {
    my ( $self, $c ) = @_;

    if ( $c->req->has_objects ) {
        $self->transact_objects( $c,
            sub { $self->delete_objects( $c, @_ ) } );
        $c->req->clear_objects;
    }
    else {
        $c->log->error($_);
        $self->push_error( $c,
            { message => 'No objects on which to operate' } );
        $c->detach();
    }
}


sub save_objects {
    my ( $self, $c, $objects ) = @_;

    foreach my $obj (@$objects) {
        $self->save_object( $c, $obj );
    }
}


sub save_object {
    my ( $self, $c, $obj ) = @_;

    my ( $object, $params ) = @$obj;

    if ( $object->in_storage ) {
        $self->update_object_from_params( $c, $object, $params );
    }
    else {
        $self->insert_object_from_params( $c, $object, $params );
    }

}


sub update_object_from_params {
    my ( $self, $c, $object, $params ) = @_;

    $params = {%$params, %{$object->ident_condition}};

    my $updated_object =
        DBIx::Class::ResultSet::RecursiveUpdate::Functions::recursive_update(
        resultset => $c->req->current_result_set,
        # unknown_params_ok => 1,
        updates => $params,
    );

    # replace request object with updated one for response
    my $vals = $c->req->get_object(0)->[1];
    $c->req->clear_objects;
    $c->req->add_object( [ $updated_object, $vals ] );
}


sub insert_object_from_params {

    #my ($self, $c, $object, $params) = @_;
    my ( $self, undef, $object, $params ) = @_;

    my %rels;
    while ( my ( $key, $value ) = each %{$params} ) {
        if ( ref($value) && !( reftype($value) eq reftype(JSON::MaybeXS::true) ) ) {
            $rels{$key} = $value;
        }

        # accessor = colname
        elsif ( $object->can($key) ) {
            $object->$key($value);
        }

        # accessor != colname
        else {
            my $accessor =
                $object->result_source->column_info($key)->{accessor};
            $object->$accessor($value);
        }
    }

    $object->insert;

    while ( my ( $k, $v ) = each %rels ) {
        if (reftype($v) eq 'ARRAY') {
            foreach my $next_v ( @$v ) {
                $object->create_related($k, $next_v);
            }
        }
        else {
            $object->create_related($k => $v);
        }
    }
}


sub delete_objects {
    my ( $self, $c, $objects ) = @_;



( run in 1.749 second using v1.01-cache-2.11-cpan-97f6503c9c8 )