From 477fa63fad94943ab6bc09891a091e2cffc613e5 Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Tue, 31 Dec 2019 10:35:15 -0300 Subject: [PATCH] Bug 24321: Clean /cities Signed-off-by: Josef Moravec Signed-off-by: Kyle M Hall Signed-off-by: Martin Renvoize --- Koha/REST/V1/Cities.pm | 89 +--------------------------------- t/db_dependent/api/v1/cities.t | 16 +++--- 2 files changed, 10 insertions(+), 95 deletions(-) diff --git a/Koha/REST/V1/Cities.pm b/Koha/REST/V1/Cities.pm index bbc54defcb..e458fe3fc6 100644 --- a/Koha/REST/V1/Cities.pm +++ b/Koha/REST/V1/Cities.pm @@ -25,7 +25,7 @@ use Try::Tiny; =head1 API -=head2 Class Methods +=head2 Methods =head3 list @@ -36,7 +36,7 @@ sub list { return try { my $cities_set = Koha::Cities->new; - my $cities = $c->objects->search( $cities_set, \&_to_model, \&_to_api ); + my $cities = $c->objects->search( $cities_set ); return $c->render( status => 200, openapi => $cities ); } catch { @@ -160,89 +160,4 @@ sub delete { }; } -=head3 _to_api - -Helper function that maps a hashref of Koha::City attributes into REST api -attribute names. - -=cut - -sub _to_api { - my $city = shift; - - # Rename attributes - foreach my $column ( keys %{ $Koha::REST::V1::Cities::to_api_mapping } ) { - my $mapped_column = $Koha::REST::V1::Cities::to_api_mapping->{$column}; - if ( exists $city->{ $column } - && defined $mapped_column ) - { - # key /= undef - $city->{ $mapped_column } = delete $city->{ $column }; - } - elsif ( exists $city->{ $column } - && !defined $mapped_column ) - { - # key == undef => to be deleted - delete $city->{ $column }; - } - } - - return $city; -} - -=head3 _to_model - -Helper function that maps REST api objects into Koha::Cities -attribute names. - -=cut - -sub _to_model { - my $city = shift; - - foreach my $attribute ( keys %{ $Koha::REST::V1::Cities::to_model_mapping } ) { - my $mapped_attribute = $Koha::REST::V1::Cities::to_model_mapping->{$attribute}; - if ( exists $city->{ $attribute } - && defined $mapped_attribute ) - { - # key /= undef - $city->{ $mapped_attribute } = delete $city->{ $attribute }; - } - elsif ( exists $city->{ $attribute } - && !defined $mapped_attribute ) - { - # key == undef => to be deleted - delete $city->{ $attribute }; - } - } - - return $city; -} - -=head2 Global variables - -=head3 $to_api_mapping - -=cut - -our $to_api_mapping = { - cityid => 'city_id', - city_country => 'country', - city_name => 'name', - city_state => 'state', - city_zipcode => 'postal_code' -}; - -=head3 $to_model_mapping - -=cut - -our $to_model_mapping = { - city_id => 'cityid', - country => 'city_country', - name => 'city_name', - postal_code => 'city_zipcode', - state => 'city_state' -}; - 1; diff --git a/t/db_dependent/api/v1/cities.t b/t/db_dependent/api/v1/cities.t index c97e907cc4..17d90515d5 100644 --- a/t/db_dependent/api/v1/cities.t +++ b/t/db_dependent/api/v1/cities.t @@ -71,7 +71,7 @@ subtest 'list() tests' => sub { # One city created, should get returned $t->get_ok("//$userid:$password@/api/v1/cities") ->status_is(200) - ->json_is( [Koha::REST::V1::Cities::_to_api( $city->TO_JSON )] ); + ->json_is( [$city->to_api] ); my $another_city = $builder->build_object( { class => 'Koha::Cities', value => { city_country => $city->city_country } } ); @@ -80,21 +80,21 @@ subtest 'list() tests' => sub { # Two cities created, they should both be returned $t->get_ok("//$userid:$password@/api/v1/cities") ->status_is(200) - ->json_is([Koha::REST::V1::Cities::_to_api($city->TO_JSON), - Koha::REST::V1::Cities::_to_api($another_city->TO_JSON), - Koha::REST::V1::Cities::_to_api($city_with_another_country->TO_JSON) + ->json_is([$city->to_api, + $another_city->to_api, + $city_with_another_country->to_api ] ); # Filtering works, two cities sharing city_country $t->get_ok("//$userid:$password@/api/v1/cities?country=" . $city->city_country ) ->status_is(200) - ->json_is([ Koha::REST::V1::Cities::_to_api($city->TO_JSON), - Koha::REST::V1::Cities::_to_api($another_city->TO_JSON) + ->json_is([ $city->to_api, + $another_city->to_api ]); $t->get_ok("//$userid:$password@/api/v1/cities?name=" . $city->city_name ) ->status_is(200) - ->json_is( [Koha::REST::V1::Cities::_to_api($city->TO_JSON)] ); + ->json_is( [$city->to_api] ); # Warn on unsupported query parameter $t->get_ok("//$userid:$password@/api/v1/cities?city_blah=blah" ) @@ -137,7 +137,7 @@ subtest 'get() tests' => sub { $t->get_ok( "//$userid:$password@/api/v1/cities/" . $city->cityid ) ->status_is(200) - ->json_is(Koha::REST::V1::Cities::_to_api($city->TO_JSON)); + ->json_is($city->to_api); $t->get_ok( "//$unauth_userid:$password@/api/v1/cities/" . $city->cityid ) ->status_is(403); -- 2.39.5