View | Details | Raw Unified | Return to bug 37902
Collapse All | Expand All

(-)a/Koha/Object.pm (-23 / +37 lines)
Lines 832-876 Returns the passed params, converted from API naming into the model. Link Here
832
832
833
=cut
833
=cut
834
834
835
sub attributes_from_api {
835
sub _recursive_fixup {
836
    my ( $self, $from_api_params ) = @_;
836
    my ( $self, $key, $value, $column_info ) = @_;
837
838
    my $from_api_mapping = $self->from_api_mapping;
839
837
840
    my $params;
838
    if ( ref($value) && ref($value) eq 'HASH' ) {
841
    my $columns_info = $self->_result->result_source->columns_info;
839
        my $hash;
842
    my $dtf          = $self->_result->result_source->storage->datetime_parser;
840
        for my $k ( keys %$value ) {
841
            $hash->{$k} = $self->_recursive_fixup( $key, $value->{$k}, $column_info );
842
        }
843
        return $hash;
843
844
844
    while (my ($key, $value) = each %{ $from_api_params } ) {
845
    } elsif ( ref($value) && ref($value) eq 'ARRAY' ) {
845
        my $koha_field_name =
846
        return [ map { $self->_recursive_fixup( $key, $_, $column_info ) } @$value ];
846
          exists $from_api_mapping->{$key}
847
    } else {
847
          ? $from_api_mapping->{$key}
848
        if ( $column_info->{is_boolean} ) {
848
          : $key;
849
849
850
        if ( $columns_info->{$koha_field_name}->{is_boolean} ) {
851
            # TODO: Remove when D8 is formally deprecated
850
            # TODO: Remove when D8 is formally deprecated
852
            # Handle booleans gracefully
851
            # Handle booleans gracefully
853
            $value = ( $value ) ? 1 : 0;
852
            $value = ($value) ? 1 : 0;
854
        }
853
        } elsif ( _date_or_datetime_column_type( $column_info->{data_type} ) ) {
855
        elsif ( _date_or_datetime_column_type( $columns_info->{$koha_field_name}->{data_type} ) ) {
854
            if ( defined $value ) {
856
            if (defined $value) {
857
                try {
855
                try {
858
                    if ( $columns_info->{$koha_field_name}->{data_type} eq 'date' ) {
856
                    my $dtf = $self->_result->result_source->storage->datetime_parser;
857
                    if ( $column_info->{data_type} eq 'date' ) {
859
                        my $dt = DateTime::Format::MySQL->parse_date($value);
858
                        my $dt = DateTime::Format::MySQL->parse_date($value);
860
                        $value = $dtf->format_date($dt);
859
                        $value = $dtf->format_date($dt);
861
                    }
860
                    } else {
862
                    else {
863
                        my $dt = Koha::DateTime::Format::RFC3339->parse_datetime($value);
861
                        my $dt = Koha::DateTime::Format::RFC3339->parse_datetime($value);
864
                        $value = $dtf->format_datetime($dt);
862
                        $value = $dtf->format_datetime($dt);
865
                    }
863
                    }
866
                }
864
                } catch {
867
                catch {
868
                    Koha::Exceptions::BadParameter->throw( parameter => $key );
865
                    Koha::Exceptions::BadParameter->throw( parameter => $key );
869
                };
866
                };
870
            }
867
            }
871
        }
868
        }
869
        return $value;
870
    }
871
}
872
873
sub attributes_from_api {
874
    my ( $self, $from_api_params ) = @_;
875
876
    my $from_api_mapping = $self->from_api_mapping;
877
878
    my $params;
879
    my $columns_info = $self->_result->result_source->columns_info;
880
881
    while ( my ( $key, $value ) = each %{$from_api_params} ) {
882
        my $koha_field_name =
883
            exists $from_api_mapping->{$key}
884
            ? $from_api_mapping->{$key}
885
            : $key;
872
886
873
        $params->{$koha_field_name} = $value;
887
        $params->{$koha_field_name} = $self->_recursive_fixup( $key, $value, $columns_info->{$koha_field_name} );
874
    }
888
    }
875
889
876
    return $params;
890
    return $params;
(-)a/Koha/REST/Plugin/Objects.pm (-6 / +8 lines)
Lines 274-286 controller, and thus shouldn't be called twice in it. Link Here
274
274
275
            # Apply the mapping function to the passed params
275
            # Apply the mapping function to the passed params
276
            if ( defined $filtered_params ) {
276
            if ( defined $filtered_params ) {
277
                $filtered_params =
277
                $filtered_params = $c->build_query_params( $filtered_params, $reserved_params );
278
                  $c->build_query_params( $filtered_params, $reserved_params );
278
            }
279
              }
280
279
281
            $filtered_params =
280
            if ($query_params) {
282
              $c->merge_q_params( $filtered_params, $query_params,
281
                $filtered_params = $c->merge_q_params(
283
                $result_set );
282
                    $filtered_params, $query_params,
283
                    $result_set
284
                );
285
            }
284
286
285
            $filtered_params =
287
            $filtered_params =
286
                  $result_set->attributes_from_api($filtered_params);
288
                  $result_set->attributes_from_api($filtered_params);
(-)a/t/db_dependent/Koha/Object.t (-2 / +23 lines)
Lines 763-769 subtest 'attributes_from_api() tests' => sub { Link Here
763
763
764
    subtest 'date and date-time handling tests' => sub {
764
    subtest 'date and date-time handling tests' => sub {
765
765
766
        plan tests => 12;
766
        plan tests => 13;
767
767
768
        my $patron = Koha::Patron->new();
768
        my $patron = Koha::Patron->new();
769
769
Lines 803-808 subtest 'attributes_from_api() tests' => sub { Link Here
803
            'Given an rfc3339 formatted date string, a date field is converted into an SQL formatted date string'
803
            'Given an rfc3339 formatted date string, a date field is converted into an SQL formatted date string'
804
        );
804
        );
805
805
806
        $attrs = $patron->attributes_from_api(
807
            {
808
                updated_on    => { '>' => '2019-12-27T14:53:00Z' },
809
                last_seen     => [ { '>' => '2019-12-27T14:53:00Z' }, { '=' => '2019-12-31T23:59:00Z' } ],
810
                date_of_birth => [ { '>' => '2019-12-27' },           { '=' => '2019-12-31' } ],
811
            }
812
        );
813
814
        is_deeply(
815
            $attrs,
816
            {
817
                lastseen => [
818
                    {
819
                        '>' => '2019-12-27 14:53:00',
820
                    },
821
                    { '=' => '2019-12-31 23:59:00' }
822
                ],
823
                updated_on => { '>' => '2019-12-27 14:53:00' },
824
                dateofbirth => [ { '>', '2019-12-27'}, { '=' => '2019-12-31' } ]
825
            }
826
        );
827
806
        $attrs = $patron->attributes_from_api(
828
        $attrs = $patron->attributes_from_api(
807
            {
829
            {
808
                last_seen      => undef,
830
                last_seen      => undef,
809
- 

Return to bug 37902