Bug 24321: Clean /cities
Signed-off-by: Josef Moravec <josef.moravec@gmail.com> Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com> Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
This commit is contained in:
parent
ee14689d51
commit
477fa63fad
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
Loading…
Reference in New Issue