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

(-)a/Koha/REST/V1/Library.pm (-122 / +4 lines)
Lines 1-4 Link Here
1
package Koha::REST::V1::Library;
1
package Koha::REST::V1::Libraries;
2
2
3
# This file is part of Koha.
3
# This file is part of Koha.
4
#
4
#
Lines 45-51 sub list { Link Here
45
45
46
    return try {
46
    return try {
47
        my $libraries_set = Koha::Libraries->new;
47
        my $libraries_set = Koha::Libraries->new;
48
        my $libraries     = $c->objects->search( $libraries_set, \&_to_model, \&_to_api );
48
        my $libraries     = $c->objects->search( $libraries_set );
49
        return $c->render( status => 200, openapi => $libraries );
49
        return $c->render( status => 200, openapi => $libraries );
50
    }
50
    }
51
    catch {
51
    catch {
Lines 95-101 sub add { Link Here
95
    my $c = shift->openapi->valid_input or return;
95
    my $c = shift->openapi->valid_input or return;
96
96
97
    return try {
97
    return try {
98
        my $library = Koha::Library->new( _to_model( $c->validation->param('body') ) );
98
        my $library = Koha::Library->new_from_api( $c->validation->param('body') );
99
        $library->store;
99
        $library->store;
100
        $c->res->headers->location( $c->req->url->to_string . '/' . $library->branchcode );
100
        $c->res->headers->location( $c->req->url->to_string . '/' . $library->branchcode );
101
101
Lines 146-152 sub update { Link Here
146
146
147
    return try {
147
    return try {
148
        my $params = $c->req->json;
148
        my $params = $c->req->json;
149
        $library->set( _to_model($params) );
149
        $library->set_from_api( $params );
150
        $library->store();
150
        $library->store();
151
        return $c->render(
151
        return $c->render(
152
            status  => 200,
152
            status  => 200,
Lines 203-324 sub delete { Link Here
203
    };
203
    };
204
}
204
}
205
205
206
=head3 _to_api
207
208
Helper function that maps a hashref of Koha::Library attributes into REST api
209
attribute names.
210
211
=cut
212
213
sub _to_api {
214
    my $library = shift;
215
216
    # Rename attributes
217
    foreach my $column ( keys %{ $Koha::REST::V1::Library::to_api_mapping } ) {
218
        my $mapped_column = $Koha::REST::V1::Library::to_api_mapping->{$column};
219
        if (    exists $library->{ $column }
220
             && defined $mapped_column )
221
        {
222
            # key /= undef
223
            $library->{ $mapped_column } = delete $library->{ $column };
224
        }
225
        elsif (    exists $library->{ $column }
226
                && !defined $mapped_column )
227
        {
228
            # key == undef => to be deleted
229
            delete $library->{ $column };
230
        }
231
    }
232
233
    return $library;
234
}
235
236
=head3 _to_model
237
238
Helper function that maps REST api objects into Koha::Library
239
attribute names.
240
241
=cut
242
243
sub _to_model {
244
    my $library = shift;
245
246
    foreach my $attribute ( keys %{ $Koha::REST::V1::Library::to_model_mapping } ) {
247
        my $mapped_attribute = $Koha::REST::V1::Library::to_model_mapping->{$attribute};
248
        if (    exists $library->{ $attribute }
249
             && defined $mapped_attribute )
250
        {
251
            # key /= undef
252
            $library->{ $mapped_attribute } = delete $library->{ $attribute };
253
        }
254
        elsif (    exists $library->{ $attribute }
255
                && !defined $mapped_attribute )
256
        {
257
            # key == undef => to be deleted
258
            delete $library->{ $attribute };
259
        }
260
    }
261
262
    if ( exists $library->{pickup_location} ) {
263
        $library->{pickup_location} = ( $library->{pickup_location} ) ? 1 : 0;
264
    }
265
266
    return $library;
267
}
268
269
270
=head2 Global variables
271
272
=head3 $to_api_mapping
273
274
=cut
275
276
our $to_api_mapping = {
277
    branchcode       => 'library_id',
278
    branchname       => 'name',
279
    branchaddress1   => 'address1',
280
    branchaddress2   => 'address2',
281
    branchaddress3   => 'address3',
282
    branchzip        => 'postal_code',
283
    branchcity       => 'city',
284
    branchstate      => 'state',
285
    branchcountry    => 'country',
286
    branchphone      => 'phone',
287
    branchfax        => 'fax',
288
    branchemail      => 'email',
289
    branchreplyto    => 'reply_to_email',
290
    branchreturnpath => 'return_path_email',
291
    branchurl        => 'url',
292
    issuing          => undef,
293
    branchip         => 'ip',
294
    branchprinter    => undef,
295
    branchnotes      => 'notes',
296
    marcorgcode      => 'marc_org_code',
297
};
298
299
=head3 $to_model_mapping
300
301
=cut
302
303
our $to_model_mapping = {
304
    library_id        => 'branchcode',
305
    name              => 'branchname',
306
    address1          => 'branchaddress1',
307
    address2          => 'branchaddress2',
308
    address3          => 'branchaddress3',
309
    postal_code       => 'branchzip',
310
    city              => 'branchcity',
311
    state             => 'branchstate',
312
    country           => 'branchcountry',
313
    phone             => 'branchphone',
314
    fax               => 'branchfax',
315
    email             => 'branchemail',
316
    reply_to_email    => 'branchreplyto',
317
    return_path_email => 'branchreturnpath',
318
    url               => 'branchurl',
319
    ip                => 'branchip',
320
    notes             => 'branchnotes',
321
    marc_org_code     => 'marcorgcode',
322
};
323
324
1;
206
1;
(-)a/api/v1/swagger/paths/libraries.json (-6 / +18 lines)
Lines 1-8 Link Here
1
{
1
{
2
  "/libraries": {
2
  "/libraries": {
3
    "get": {
3
    "get": {
4
      "x-mojo-to": "Library#list",
4
      "x-mojo-to": "Libraries#list",
5
      "operationId": "listLibrary",
5
      "operationId": "listLibraries",
6
      "tags": [
6
      "tags": [
7
        "library"
7
        "library"
8
      ],
8
      ],
Lines 125-130 Link Here
125
          "description": "Case insensitive 'starts-with' search on OPAC info",
125
          "description": "Case insensitive 'starts-with' search on OPAC info",
126
          "required": false,
126
          "required": false,
127
          "type": "string"
127
          "type": "string"
128
        },
129
        {
130
          "$ref": "../parameters.json#/match"
131
        },
132
        {
133
          "$ref": "../parameters.json#/order_by"
134
        },
135
        {
136
          "$ref": "../parameters.json#/page"
137
        },
138
        {
139
          "$ref": "../parameters.json#/per_page"
128
        }
140
        }
129
      ],
141
      ],
130
      "produces": [
142
      "produces": [
Lines 160-166 Link Here
160
      }
172
      }
161
    },
173
    },
162
    "post": {
174
    "post": {
163
      "x-mojo-to": "Library#add",
175
      "x-mojo-to": "Libraries#add",
164
      "operationId": "addLibrary",
176
      "operationId": "addLibrary",
165
      "tags": [
177
      "tags": [
166
        "library"
178
        "library"
Lines 232-238 Link Here
232
  },
244
  },
233
  "/libraries/{library_id}": {
245
  "/libraries/{library_id}": {
234
    "get": {
246
    "get": {
235
      "x-mojo-to": "Library#get",
247
      "x-mojo-to": "Libraries#get",
236
      "operationId": "getLibrary",
248
      "operationId": "getLibrary",
237
      "tags": [
249
      "tags": [
238
        "library"
250
        "library"
Lines 266-272 Link Here
266
      }
278
      }
267
    },
279
    },
268
    "put": {
280
    "put": {
269
      "x-mojo-to": "Library#update",
281
      "x-mojo-to": "Libraries#update",
270
      "operationId": "updateLibrary",
282
      "operationId": "updateLibrary",
271
      "tags": [
283
      "tags": [
272
        "library"
284
        "library"
Lines 342-348 Link Here
342
      }
354
      }
343
    },
355
    },
344
    "delete": {
356
    "delete": {
345
      "x-mojo-to": "Library#delete",
357
      "x-mojo-to": "Libraries#delete",
346
      "operationId": "deleteLibrary",
358
      "operationId": "deleteLibrary",
347
      "tags": [
359
      "tags": [
348
        "library"
360
        "library"
(-)a/t/db_dependent/api/v1/libraries.t (-4 / +3 lines)
Lines 121-127 subtest 'get() tests' => sub { Link Here
121
121
122
    $t->get_ok( "//$userid:$password@/api/v1/libraries/" . $library->branchcode )
122
    $t->get_ok( "//$userid:$password@/api/v1/libraries/" . $library->branchcode )
123
      ->status_is( 200, 'SWAGGER3.2.2' )
123
      ->status_is( 200, 'SWAGGER3.2.2' )
124
      ->json_is( '' => Koha::REST::V1::Library::_to_api( $library->TO_JSON ), 'SWAGGER3.3.2' );
124
      ->json_is( '' => $library->to_api, 'SWAGGER3.3.2' );
125
125
126
    my $non_existent_code = $library->branchcode;
126
    my $non_existent_code = $library->branchcode;
127
    $library->delete;
127
    $library->delete;
Lines 155-161 subtest 'add() tests' => sub { Link Here
155
    my $unauth_userid = $unauthorized_patron->userid;
155
    my $unauth_userid = $unauthorized_patron->userid;
156
156
157
    my $library_obj = $builder->build_object({ class => 'Koha::Libraries' });
157
    my $library_obj = $builder->build_object({ class => 'Koha::Libraries' });
158
    my $library     = Koha::REST::V1::Library::_to_api( $library_obj->TO_JSON );
158
    my $library     = $library_obj->to_api;
159
    $library_obj->delete;
159
    $library_obj->delete;
160
160
161
    # Unauthorized attempt to write
161
    # Unauthorized attempt to write
Lines 245-251 subtest 'update() tests' => sub { Link Here
245
      );
245
      );
246
246
247
    my $deleted_library = $builder->build_object( { class => 'Koha::Libraries' } );
247
    my $deleted_library = $builder->build_object( { class => 'Koha::Libraries' } );
248
    my $library_with_updated_field = Koha::REST::V1::Library::_to_api( $deleted_library->TO_JSON );
248
    my $library_with_updated_field = $deleted_library->to_api;
249
    $library_with_updated_field->{library_id} = $library_id;
249
    $library_with_updated_field->{library_id} = $library_id;
250
    $deleted_library->delete;
250
    $deleted_library->delete;
251
251
252
- 

Return to bug 24321