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

(-)a/Koha/REST/V1/Cities.pm (-87 / +2 lines)
Lines 25-31 use Try::Tiny; Link Here
25
25
26
=head1 API
26
=head1 API
27
27
28
=head2 Class Methods
28
=head2 Methods
29
29
30
=head3 list
30
=head3 list
31
31
Lines 36-42 sub list { Link Here
36
36
37
    return try {
37
    return try {
38
        my $cities_set = Koha::Cities->new;
38
        my $cities_set = Koha::Cities->new;
39
        my $cities = $c->objects->search( $cities_set, \&_to_model, \&_to_api );
39
        my $cities = $c->objects->search( $cities_set );
40
        return $c->render( status => 200, openapi => $cities );
40
        return $c->render( status => 200, openapi => $cities );
41
    }
41
    }
42
    catch {
42
    catch {
Lines 160-248 sub delete { Link Here
160
    };
160
    };
161
}
161
}
162
162
163
=head3 _to_api
164
165
Helper function that maps a hashref of Koha::City attributes into REST api
166
attribute names.
167
168
=cut
169
170
sub _to_api {
171
    my $city    = shift;
172
173
    # Rename attributes
174
    foreach my $column ( keys %{ $Koha::REST::V1::Cities::to_api_mapping } ) {
175
        my $mapped_column = $Koha::REST::V1::Cities::to_api_mapping->{$column};
176
        if (    exists $city->{ $column }
177
             && defined $mapped_column )
178
        {
179
            # key /= undef
180
            $city->{ $mapped_column } = delete $city->{ $column };
181
        }
182
        elsif (    exists $city->{ $column }
183
                && !defined $mapped_column )
184
        {
185
            # key == undef => to be deleted
186
            delete $city->{ $column };
187
        }
188
    }
189
190
    return $city;
191
}
192
193
=head3 _to_model
194
195
Helper function that maps REST api objects into Koha::Cities
196
attribute names.
197
198
=cut
199
200
sub _to_model {
201
    my $city = shift;
202
203
    foreach my $attribute ( keys %{ $Koha::REST::V1::Cities::to_model_mapping } ) {
204
        my $mapped_attribute = $Koha::REST::V1::Cities::to_model_mapping->{$attribute};
205
        if (    exists $city->{ $attribute }
206
             && defined $mapped_attribute )
207
        {
208
            # key /= undef
209
            $city->{ $mapped_attribute } = delete $city->{ $attribute };
210
        }
211
        elsif (    exists $city->{ $attribute }
212
                && !defined $mapped_attribute )
213
        {
214
            # key == undef => to be deleted
215
            delete $city->{ $attribute };
216
        }
217
    }
218
219
    return $city;
220
}
221
222
=head2 Global variables
223
224
=head3 $to_api_mapping
225
226
=cut
227
228
our $to_api_mapping = {
229
    cityid       => 'city_id',
230
    city_country => 'country',
231
    city_name    => 'name',
232
    city_state   => 'state',
233
    city_zipcode => 'postal_code'
234
};
235
236
=head3 $to_model_mapping
237
238
=cut
239
240
our $to_model_mapping = {
241
    city_id     => 'cityid',
242
    country     => 'city_country',
243
    name        => 'city_name',
244
    postal_code => 'city_zipcode',
245
    state       => 'city_state'
246
};
247
248
1;
163
1;
(-)a/t/db_dependent/api/v1/cities.t (-9 / +8 lines)
Lines 71-77 subtest 'list() tests' => sub { Link Here
71
    # One city created, should get returned
71
    # One city created, should get returned
72
    $t->get_ok("//$userid:$password@/api/v1/cities")
72
    $t->get_ok("//$userid:$password@/api/v1/cities")
73
      ->status_is(200)
73
      ->status_is(200)
74
      ->json_is( [Koha::REST::V1::Cities::_to_api( $city->TO_JSON )] );
74
      ->json_is( [$city->to_api] );
75
75
76
    my $another_city = $builder->build_object(
76
    my $another_city = $builder->build_object(
77
        { class => 'Koha::Cities', value => { city_country => $city->city_country } } );
77
        { class => 'Koha::Cities', value => { city_country => $city->city_country } } );
Lines 80-100 subtest 'list() tests' => sub { Link Here
80
    # Two cities created, they should both be returned
80
    # Two cities created, they should both be returned
81
    $t->get_ok("//$userid:$password@/api/v1/cities")
81
    $t->get_ok("//$userid:$password@/api/v1/cities")
82
      ->status_is(200)
82
      ->status_is(200)
83
      ->json_is([Koha::REST::V1::Cities::_to_api($city->TO_JSON),
83
      ->json_is([$city->to_api,
84
                 Koha::REST::V1::Cities::_to_api($another_city->TO_JSON),
84
                 $another_city->to_api,
85
                 Koha::REST::V1::Cities::_to_api($city_with_another_country->TO_JSON)
85
                 $city_with_another_country->to_api
86
                 ] );
86
                 ] );
87
87
88
    # Filtering works, two cities sharing city_country
88
    # Filtering works, two cities sharing city_country
89
    $t->get_ok("//$userid:$password@/api/v1/cities?country=" . $city->city_country )
89
    $t->get_ok("//$userid:$password@/api/v1/cities?country=" . $city->city_country )
90
      ->status_is(200)
90
      ->status_is(200)
91
      ->json_is([ Koha::REST::V1::Cities::_to_api($city->TO_JSON),
91
      ->json_is([ $city->to_api,
92
                  Koha::REST::V1::Cities::_to_api($another_city->TO_JSON)
92
                  $another_city->to_api
93
                  ]);
93
                  ]);
94
94
95
    $t->get_ok("//$userid:$password@/api/v1/cities?name=" . $city->city_name )
95
    $t->get_ok("//$userid:$password@/api/v1/cities?name=" . $city->city_name )
96
      ->status_is(200)
96
      ->status_is(200)
97
      ->json_is( [Koha::REST::V1::Cities::_to_api($city->TO_JSON)] );
97
      ->json_is( [$city->to_api] );
98
98
99
    # Warn on unsupported query parameter
99
    # Warn on unsupported query parameter
100
    $t->get_ok("//$userid:$password@/api/v1/cities?city_blah=blah" )
100
    $t->get_ok("//$userid:$password@/api/v1/cities?city_blah=blah" )
Lines 137-143 subtest 'get() tests' => sub { Link Here
137
137
138
    $t->get_ok( "//$userid:$password@/api/v1/cities/" . $city->cityid )
138
    $t->get_ok( "//$userid:$password@/api/v1/cities/" . $city->cityid )
139
      ->status_is(200)
139
      ->status_is(200)
140
      ->json_is(Koha::REST::V1::Cities::_to_api($city->TO_JSON));
140
      ->json_is($city->to_api);
141
141
142
    $t->get_ok( "//$unauth_userid:$password@/api/v1/cities/" . $city->cityid )
142
    $t->get_ok( "//$unauth_userid:$password@/api/v1/cities/" . $city->cityid )
143
      ->status_is(403);
143
      ->status_is(403);
144
- 

Return to bug 24321