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

(-)a/Koha/REST/V1/Biblios/ItemGroups.pm (+222 lines)
Line 0 Link Here
1
package Koha::REST::V1::Biblios::ItemGroups;
2
3
# This file is part of Koha.
4
#
5
# Koha is free software; you can redistribute it and/or modify it
6
# under the terms of the GNU General Public License as published by
7
# the Free Software Foundation; either version 3 of the License, or
8
# (at your option) any later version.
9
#
10
# Koha is distributed in the hope that it will be useful, but
11
# WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
# GNU General Public License for more details.
14
#
15
# You should have received a copy of the GNU General Public License
16
# along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18
use Modern::Perl;
19
20
use Mojo::Base 'Mojolicious::Controller';
21
22
use Koha::Biblio::ItemGroups;
23
24
use Scalar::Util qw(blessed);
25
use Try::Tiny;
26
27
=head1 NAME
28
29
Koha::REST::V1::Biblios::ItemGroups - Koha REST API for handling item groups (V1)
30
31
=head1 API
32
33
=head2 Methods
34
35
=cut
36
37
=head3 list
38
39
Controller function that handles listing Koha::Biblio::ItemGroup objects
40
41
=cut
42
43
sub list {
44
    my $c = shift->openapi->valid_input or return;
45
    my $biblio_id = $c->validation->param('biblio_id');
46
47
    my $biblio=Koha::Biblios->find( $biblio_id);
48
49
    return try {
50
#my $item_groups_set = Koha::Biblio::ItemGroups->new;
51
        my $item_groups_set = $biblio->item_groups;
52
        my $item_groups     = $c->objects->search( $item_groups_set );
53
        return $c->render(
54
            status  => 200,
55
            openapi => $item_groups
56
        );
57
    }
58
    catch {
59
        $c->unhandled_exception($_);
60
    };
61
}
62
63
=head3 get
64
65
Controller function that handles retrieving a single Koha::Biblio::ItemGroup
66
67
=cut
68
69
sub get {
70
    my $c = shift->openapi->valid_input or return;
71
72
    try {
73
        my $item_group_id = $c->validation->param('item_group_id');
74
        my $biblio_id = $c->validation->param('biblio_id');
75
76
        my $item_group = $c->objects->find( Koha::Biblio::ItemGroups->new, $item_group_id );
77
78
        if ( $item_group && $item_group->{biblio_id} eq $biblio_id ) {
79
            return $c->render(
80
                status  => 200,
81
                openapi => $item_group
82
            );
83
        }
84
        else {
85
            return $c->render(
86
                status  => 404,
87
                openapi => {
88
                    error => 'Item group not found'
89
                }
90
            );
91
        }
92
    }
93
    catch {
94
        $c->unhandled_exception($_);
95
    };
96
}
97
98
=head3 add
99
100
Controller function to handle adding a Koha::Biblio::ItemGroup object
101
102
=cut
103
104
sub add {
105
    my $c = shift->openapi->valid_input or return;
106
107
    return try {
108
        my $item_group_data = $c->validation->param('body');
109
        # biblio_id comes from the path
110
        $item_group_data->{biblio_id} = $c->validation->param('biblio_id');
111
112
        my $item_group = Koha::Biblio::ItemGroup->new_from_api($item_group_data);
113
        $item_group->store->discard_changes();
114
115
        $c->res->headers->location( $c->req->url->to_string . '/' . $item_group->id );
116
117
        return $c->render(
118
            status  => 201,
119
            openapi => $item_group->to_api
120
        );
121
    }
122
    catch {
123
        if ( blessed($_) ) {
124
            my $to_api_mapping = Koha::Biblio::ItemGroup->new->to_api_mapping;
125
126
            if (    $_->isa('Koha::Exceptions::Object::FKConstraint')
127
                and $_->broken_fk eq 'biblio_id' )
128
            {
129
                return $c->render(
130
                    status  => 404,
131
                    openapi => { error => "Biblio not found" }
132
                );
133
            }
134
        }
135
136
        $c->unhandled_exception($_);
137
    };
138
}
139
140
=head3 update
141
142
Controller function to handle updating a Koha::Biblio::ItemGroup object
143
144
=cut
145
146
sub update {
147
    my $c = shift->openapi->valid_input or return;
148
149
    return try {
150
        my $item_group_id = $c->validation->param('item_group_id');
151
        my $biblio_id     = $c->validation->param('biblio_id');
152
153
        my $item_group = Koha::Biblio::ItemGroups->find( $item_group_id );
154
155
        unless ( $item_group && $item_group->biblio_id eq $biblio_id ) {
156
            return $c->render(
157
                status  => 404,
158
                openapi => {
159
                    error => 'Item group not found'
160
                }
161
            );
162
        }
163
164
        my $item_group_data = $c->validation->param('body');
165
        $item_group->set_from_api( $item_group_data )->store->discard_changes();
166
167
        return $c->render(
168
            status  => 200,
169
            openapi => $item_group->to_api
170
        );
171
    }
172
    catch {
173
        if ( blessed($_) ) {
174
            my $to_api_mapping = Koha::Biblio::ItemGroup->new->to_api_mapping;
175
176
            if ( $_->isa('Koha::Exceptions::Object::FKConstraint') ) {
177
                return $c->render(
178
                    status  => 409,
179
                    openapi => {
180
                        error => "Given " . $to_api_mapping->{ $_->broken_fk } . " does not exist"
181
                    }
182
                );
183
            }
184
        }
185
186
        $c->unhandled_exception($_);
187
    };
188
}
189
190
=head3 delete
191
192
Controller function that handles deleting a Koha::Biblio::ItemGroup object
193
194
=cut
195
196
sub delete {
197
198
    my $c = shift->openapi->valid_input or return;
199
200
    my $item_group_id = $c->validation->param('item_group_id');
201
    my $biblio_id     = $c->validation->param('biblio_id');
202
203
    my $item_group = Koha::Biblio::ItemGroups->find(
204
        { item_group_id => $item_group_id, biblio_id => $biblio_id } );
205
206
    if ( not defined $item_group ) {
207
        return $c->render(
208
            status  => 404,
209
            openapi => { error => "Item group not found" }
210
        );
211
    }
212
213
    return try {
214
        $item_group->delete;
215
        return $c->render( status => 204, openapi => '' );
216
    }
217
    catch {
218
        $c->unhandled_exception($_);
219
    };
220
}
221
222
1;
(-)a/Koha/REST/V1/Biblios/ItemGroups/Items.pm (+159 lines)
Line 0 Link Here
1
package Koha::REST::V1::Biblios::ItemGroups::Items;
2
3
# This file is part of Koha.
4
#
5
# Koha is free software; you can redistribute it and/or modify it
6
# under the terms of the GNU General Public License as published by
7
# the Free Software Foundation; either version 3 of the License, or
8
# (at your option) any later version.
9
#
10
# Koha is distributed in the hope that it will be useful, but
11
# WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
# GNU General Public License for more details.
14
#
15
# You should have received a copy of the GNU General Public License
16
# along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18
use Modern::Perl;
19
20
use Mojo::Base 'Mojolicious::Controller';
21
22
use Koha::Biblio::ItemGroup::Items;
23
24
use Scalar::Util qw(blessed);
25
use Try::Tiny;
26
27
=head1 NAME
28
29
Koha::REST::V1::Biblios::ItemGroups::Items - Koha REST API for handling item group items (V1)
30
31
=head1 API
32
33
=head2 Methods
34
35
=head3 add
36
37
Controller function to handle linking an item to a Koha::Biblio::ItemGroup object
38
39
=cut
40
41
sub add {
42
    my $c = shift->openapi->valid_input or return;
43
44
    return try {
45
46
        my $item_group = Koha::Biblio::ItemGroups->find(
47
            $c->validation->param('item_group_id')
48
        );
49
50
        unless ( $item_group ) {
51
            return $c->render(
52
                status  => 404,
53
                openapi => {
54
                    error => 'Item group not found'
55
                }
56
            );
57
        }
58
59
        unless ( $item_group->biblio_id eq $c->validation->param('biblio_id') ) {
60
            return $c->render(
61
                status  => 409,
62
                openapi => {
63
                    error => 'Item group does not belong to passed biblio_id'
64
                }
65
            );
66
        }
67
68
        # All good, add the item
69
        my $body    = $c->validation->param('body');
70
        my $item_id = $body->{item_id};
71
72
        $item_group->add_item({ item_id => $item_id });
73
74
        $c->res->headers->location( $c->req->url->to_string . '/' . $item_id );
75
76
        my $embed = $c->stash('koha.embed');
77
78
        return $c->render(
79
            status  => 201,
80
            openapi => $item_group->to_api({ embed => $embed })
81
        );
82
    }
83
    catch {
84
        if ( blessed($_) ) {
85
86
            if ( $_->isa('Koha::Exceptions::Object::FKConstraint') ) {
87
                if ( $_->broken_fk eq 'itemnumber' ) {
88
                    return $c->render(
89
                        status  => 409,
90
                        openapi => {
91
                            error => "Given item_id does not exist"
92
                        }
93
                    );
94
                }
95
                elsif ( $_->broken_fk eq 'biblio_id' ) {
96
                    return $c->render(
97
                        status  => 409,
98
                        openapi => {
99
                            error => "Given item_id does not belong to the item group's biblio"
100
                        }
101
                    );
102
                }
103
            }
104
            elsif ( $_->isa('Koha::Exceptions::Object::DuplicateID') ) {
105
106
                return $c->render(
107
                    status  => 409,
108
                    openapi => {
109
                        error => "The given item_id is already linked to the item group"
110
                    }
111
                );
112
            }
113
        }
114
115
        $c->unhandled_exception($_);
116
    };
117
}
118
119
=head3 delete
120
121
Controller function that handles unlinking an item from a Koha::Biblio::ItemGroup object
122
123
=cut
124
125
sub delete {
126
    my $c = shift->openapi->valid_input or return;
127
128
    my $item_group_id = $c->validation->param('item_group_id');
129
    my $item_id       = $c->validation->param('item_id');
130
131
    my $item_link = Koha::Biblio::ItemGroup::Items->find(
132
        {
133
            item_id       => $item_id,
134
            item_group_id => $item_group_id
135
        }
136
    );
137
138
    unless ( $item_link ) {
139
        return $c->render(
140
            status  => 404,
141
            openapi => {
142
                error => 'No such item group <-> item relationship'
143
            }
144
        );
145
    }
146
147
    return try {
148
        $item_link->delete;
149
        return $c->render(
150
            status  => 204,
151
            openapi => q{}
152
        );
153
    }
154
    catch {
155
        $c->unhandled_exception($_);
156
    };
157
}
158
159
1;
(-)a/api/v1/swagger/definitions/item_group.yaml (+37 lines)
Line 0 Link Here
1
---
2
type: object
3
properties:
4
  item_group_id:
5
    type: integer
6
    readOnly: true
7
    description: Internal identifier for the item group
8
  biblio_id:
9
    type: integer
10
    readOnly: true
11
    description: Internal identifier for the parent bibliographic record
12
  description:
13
    type: string
14
    description: Item group description
15
  display_order:
16
    type: integer
17
    description: Item group description
18
  creation_date:
19
    type: string
20
    format: date-time
21
    readOnly: true
22
    description: Date and time the item group was created
23
  modification_date:
24
    type: string
25
    format: date-time
26
    readOnly: true
27
    description: Date and time the item group was last modified
28
  items:
29
    type:
30
    - array
31
    - 'null'
32
    readOnly: true
33
    description: A list of items that belong to the volume (x-koha-embed)
34
additionalProperties: false
35
required:
36
- item_group_id
37
- biblio_id
(-)a/api/v1/swagger/paths/biblios_item_groups.yaml (+388 lines)
Line 0 Link Here
1
---
2
"/biblios/{biblio_id}/item_groups":
3
  get:
4
    x-mojo-to: Biblios::ItemGroups#list
5
    operationId: listItemGroups
6
    tags:
7
    - item_groups
8
    summary: List item_groups
9
    parameters:
10
    - name: biblio_id
11
      in: path
12
      description: Internal identifier for the parent bibliographic record
13
      required: true
14
      type: string
15
    - "$ref": "../swagger.yaml#/parameters/match"
16
    - "$ref": "../swagger.yaml#/parameters/order_by"
17
    - "$ref": "../swagger.yaml#/parameters/page"
18
    - "$ref": "../swagger.yaml#/parameters/per_page"
19
    - "$ref": "../swagger.yaml#/parameters/q_param"
20
    - "$ref": "../swagger.yaml#/parameters/q_body"
21
    - "$ref": "../swagger.yaml#/parameters/q_header"
22
    produces:
23
    - application/yaml
24
    responses:
25
      '200':
26
        description: A list of item_groups
27
        schema:
28
          type: array
29
          items:
30
            "$ref": "../swagger.yaml#/definitions/item_group"
31
      '401':
32
        description: Authentication required
33
        schema:
34
          "$ref": "../swagger.yaml#/definitions/error"
35
      '403':
36
        description: Access forbidden
37
        schema:
38
          "$ref": "../swagger.yaml#/definitions/error"
39
      '500':
40
        description: Internal server error
41
        schema:
42
          "$ref": "../swagger.yaml#/definitions/error"
43
      '503':
44
        description: Under maintenance
45
        schema:
46
          "$ref": "../swagger.yaml#/definitions/error"
47
    x-koha-authorization:
48
      permissions:
49
        catalogue: "manage_item_groups"
50
    x-koha-embed:
51
    - items
52
  post:
53
    x-mojo-to: Biblios::ItemGroups#add
54
    operationId: addItemGroup
55
    tags:
56
    - item_groups
57
    summary: Add item group
58
    parameters:
59
    - name: biblio_id
60
      in: path
61
      description: Internal identifier for the parent bibliographic record
62
      required: true
63
      type: string
64
    - name: body
65
      in: body
66
      description: A JSON object representing an item group
67
      required: true
68
      schema:
69
        type: object
70
        properties:
71
          description:
72
            type: string
73
            description: ItemGroup description
74
          display_order:
75
            type: integer
76
            description: Position in waiting queue
77
    produces:
78
    - application/yaml
79
    responses:
80
      '201':
81
        description: A successfully created item group
82
        schema:
83
          "$ref": "../swagger.yaml#/definitions/item_group"
84
      '400':
85
        description: Bad parameter
86
        schema:
87
          "$ref": "../swagger.yaml#/definitions/error"
88
      '401':
89
        description: Authentication required
90
        schema:
91
          "$ref": "../swagger.yaml#/definitions/error"
92
      '403':
93
        description: Access forbidden
94
        schema:
95
          "$ref": "../swagger.yaml#/definitions/error"
96
      '404':
97
        description: Resource not found
98
        schema:
99
          "$ref": "../swagger.yaml#/definitions/error"
100
      '409':
101
        description: Conflict in creating resource
102
        schema:
103
          "$ref": "../swagger.yaml#/definitions/error"
104
      '500':
105
        description: Internal server error
106
        schema:
107
          "$ref": "../swagger.yaml#/definitions/error"
108
      '503':
109
        description: Under maintenance
110
        schema:
111
          "$ref": "../swagger.yaml#/definitions/error"
112
    x-koha-authorization:
113
      permissions:
114
        catalogue: "manage_item_groups"
115
"/biblios/{biblio_id}/item_groups/{item_group_id}":
116
  get:
117
    x-mojo-to: Biblios::ItemGroups#get
118
    operationId: getItemGroup
119
    tags:
120
    - item_groups
121
    summary: Get item group
122
    parameters:
123
    - name: biblio_id
124
      in: path
125
      description: Internal identifier for the parent bibliographic record
126
      required: true
127
      type: string
128
    - name: item_group_id
129
      in: path
130
      description: Internal identifier for the item_group
131
      required: true
132
      type: string
133
    produces:
134
    - application/yaml
135
    responses:
136
      '200':
137
        description: An item group
138
        schema:
139
          "$ref": "../swagger.yaml#/definitions/item_group"
140
      '400':
141
        description: Missing or wrong parameters
142
        schema:
143
          "$ref": "../swagger.yaml#/definitions/error"
144
      '404':
145
        description: ItemGroup not found
146
        schema:
147
          "$ref": "../swagger.yaml#/definitions/error"
148
      '500':
149
        description: Internal server error
150
        schema:
151
          "$ref": "../swagger.yaml#/definitions/error"
152
      '503':
153
        description: Under maintenance
154
        schema:
155
          "$ref": "../swagger.yaml#/definitions/error"
156
    x-koha-embed:
157
    - items
158
    x-koha-authorization:
159
      permissions:
160
        catalogue: "manage_item_groups"
161
  put:
162
    x-mojo-to: Biblios::ItemGroups#update
163
    operationId: updateItemGroup
164
    tags:
165
    - item_groups
166
    summary: Update item group
167
    parameters:
168
    - name: biblio_id
169
      in: path
170
      description: Internal identifier for the parent bibliographic record
171
      required: true
172
      type: string
173
    - name: item_group_id
174
      in: path
175
      description: Internal identifier for the item group
176
      required: true
177
      type: string
178
    - name: body
179
      in: body
180
      description: A JSON object with the new values for the item group
181
      required: true
182
      schema:
183
        type: object
184
        properties:
185
          description:
186
            type: string
187
            description: ItemGroup description
188
          display_order:
189
            type: integer
190
            description: Position in waiting queue
191
    produces:
192
    - application/yaml
193
    responses:
194
      '200':
195
        description: The updated item group
196
        schema:
197
          "$ref": "../swagger.yaml#/definitions/item_group"
198
      '400':
199
        description: Bad request
200
        schema:
201
          "$ref": "../swagger.yaml#/definitions/error"
202
      '401':
203
        description: Authentication required
204
        schema:
205
          "$ref": "../swagger.yaml#/definitions/error"
206
      '403':
207
        description: Access forbidden
208
        schema:
209
          "$ref": "../swagger.yaml#/definitions/error"
210
      '404':
211
        description: ItemGroup not found
212
        schema:
213
          "$ref": "../swagger.yaml#/definitions/error"
214
      '500':
215
        description: Internal error
216
        schema:
217
          "$ref": "../swagger.yaml#/definitions/error"
218
      '503':
219
        description: Under maintenance
220
        schema:
221
          "$ref": "../swagger.yaml#/definitions/error"
222
    x-koha-authorization:
223
      permissions:
224
        catalogue: "manage_item_groups"
225
    x-koha-embed:
226
    - items
227
  delete:
228
    x-mojo-to: Biblios::ItemGroups#delete
229
    operationId: deleteItemGroup
230
    tags:
231
    - item_groups
232
    summary: Delete item group
233
    parameters:
234
    - name: biblio_id
235
      in: path
236
      description: Internal identifier for the parent bibliographic record
237
      required: true
238
      type: string
239
    - name: item_group_id
240
      in: path
241
      description: Internal identifier for the item group
242
      required: true
243
      type: string
244
    produces:
245
    - application/yaml
246
    responses:
247
      '204':
248
        description: ItemGroup deleted
249
        schema:
250
          type: string
251
      '401':
252
        description: Authentication required
253
        schema:
254
          "$ref": "../swagger.yaml#/definitions/error"
255
      '403':
256
        description: Access forbidden
257
        schema:
258
          "$ref": "../swagger.yaml#/definitions/error"
259
      '404':
260
        description: ItemGroup not found
261
        schema:
262
          "$ref": "../swagger.yaml#/definitions/error"
263
      '500':
264
        description: Internal error
265
        schema:
266
          "$ref": "../swagger.yaml#/definitions/error"
267
      '503':
268
        description: Under maintenance
269
        schema:
270
          "$ref": "../swagger.yaml#/definitions/error"
271
    x-koha-authorization:
272
      permissions:
273
        catalogue: "manage_item_groups"
274
"/biblios/{biblio_id}/item_groups/{item_group_id}/items":
275
  post:
276
    x-mojo-to: Biblios::ItemGroups::Items#add
277
    operationId: addItemGroupItem
278
    tags:
279
    - item_groups
280
    summary: Add item to item group
281
    parameters:
282
    - name: biblio_id
283
      in: path
284
      description: Internal identifier for the parent bibliographic record
285
      required: true
286
      type: string
287
    - name: item_group_id
288
      in: path
289
      description: Internal identifier for the item group
290
      required: true
291
      type: string
292
    - name: body
293
      in: body
294
      description: A JSON object containing an item_id
295
      required: true
296
      schema:
297
        type: object
298
        properties:
299
          item_id:
300
            type: integer
301
            description: Internal identifier for an item to be linked
302
    produces:
303
    - application/yaml
304
    responses:
305
      '201':
306
        description: Item linked to item group
307
      '400':
308
        description: Bad request
309
        schema:
310
          "$ref": "../swagger.yaml#/definitions/error"
311
      '401':
312
        description: Authentication required
313
        schema:
314
          "$ref": "../swagger.yaml#/definitions/error"
315
      '403':
316
        description: Access forbidden
317
        schema:
318
          "$ref": "../swagger.yaml#/definitions/error"
319
      '409':
320
        description: Request conflicts
321
        schema:
322
          "$ref": "../swagger.yaml#/definitions/error"
323
      '500':
324
        description: Internal error
325
        schema:
326
          "$ref": "../swagger.yaml#/definitions/error"
327
      '503':
328
        description: Under maintenance
329
        schema:
330
          "$ref": "../swagger.yaml#/definitions/error"
331
    x-koha-authorization:
332
      permissions:
333
        catalogue: "manage_item_groups"
334
    x-koha-embed:
335
    - items
336
"/biblios/{biblio_id}/item_groups/{item_group_id}/items/{item_id}":
337
  delete:
338
    x-mojo-to: Biblios::ItemGroups::Items#delete
339
    operationId: deleteItemGroupItems
340
    tags:
341
    - item_groups
342
    summary: Delete item from item group
343
    parameters:
344
    - name: biblio_id
345
      in: path
346
      description: Internal identifier for the parent bibliographic record
347
      required: true
348
      type: string
349
    - name: item_group_id
350
      in: path
351
      description: Internal identifier for the item group
352
      required: true
353
      type: string
354
    - name: item_id
355
      in: path
356
      description: Internal identifier for the item
357
      required: true
358
      type: string
359
    produces:
360
    - application/yaml
361
    responses:
362
      '204':
363
        description: Item unlinked from item group
364
        schema:
365
          type: string
366
      '401':
367
        description: Authentication required
368
        schema:
369
          "$ref": "../swagger.yaml#/definitions/error"
370
      '403':
371
        description: Access forbidden
372
        schema:
373
          "$ref": "../swagger.yaml#/definitions/error"
374
      '404':
375
        description: Item not linked to item group
376
        schema:
377
          "$ref": "../swagger.yaml#/definitions/error"
378
      '500':
379
        description: Internal error
380
        schema:
381
          "$ref": "../swagger.yaml#/definitions/error"
382
      '503':
383
        description: Under maintenance
384
        schema:
385
          "$ref": "../swagger.yaml#/definitions/error"
386
    x-koha-authorization:
387
      permissions:
388
        catalogue: "manage_item_groups"
(-)a/api/v1/swagger/swagger.yaml (+10 lines)
Lines 42-47 definitions: Link Here
42
    $ref: ./definitions/invoice.yaml
42
    $ref: ./definitions/invoice.yaml
43
  item:
43
  item:
44
    $ref: ./definitions/item.yaml
44
    $ref: ./definitions/item.yaml
45
  item_group:
46
    $ref: ./definitions/item_group.yaml
45
  library:
47
  library:
46
    $ref: ./definitions/library.yaml
48
    $ref: ./definitions/library.yaml
47
  order:
49
  order:
Lines 101-106 paths: Link Here
101
    $ref: "./paths/biblios.yaml#/~1biblios~1{biblio_id}~1items"
103
    $ref: "./paths/biblios.yaml#/~1biblios~1{biblio_id}~1items"
102
  "/biblios/{biblio_id}/pickup_locations":
104
  "/biblios/{biblio_id}/pickup_locations":
103
    $ref: "./paths/biblios.yaml#/~1biblios~1{biblio_id}~1pickup_locations"
105
    $ref: "./paths/biblios.yaml#/~1biblios~1{biblio_id}~1pickup_locations"
106
  "/biblios/{biblio_id}/item_groups":
107
    $ref: "./paths/biblios_item_groups.yaml#/~1biblios~1{biblio_id}~1item_groups"
108
  "/biblios/{biblio_id}/item_groups/{item_group_id}":
109
    $ref: "./paths/biblios_item_groups.yaml#/~1biblios~1{biblio_id}~1item_groups~1{item_group_id}"
110
  "/biblios/{biblio_id}/item_groups/{item_group_id}/items":
111
    $ref: "./paths/biblios_item_groups.yaml#/~1biblios~1{biblio_id}~1item_groups~1{item_group_id}~1items"
112
  "/biblios/{biblio_id}/item_groups/{item_group_id}/items/{item_id}":
113
    $ref: "./paths/biblios_item_groups.yaml#/~1biblios~1{biblio_id}~1item_groups~1{item_group_id}~1items~1{item_id}"
104
  "/cash_registers/{cash_register_id}/cashups":
114
  "/cash_registers/{cash_register_id}/cashups":
105
    $ref: "./paths/cash_registers.yaml#/~1cash_registers~1{cash_register_id}~1cashups"
115
    $ref: "./paths/cash_registers.yaml#/~1cash_registers~1{cash_register_id}~1cashups"
106
  "/cashups/{cashup_id}":
116
  "/cashups/{cashup_id}":
(-)a/t/db_dependent/api/v1/item_groups.t (-1 / +265 lines)
Line 0 Link Here
0
- 
1
#!/usr/bin/env perl
2
3
# This file is part of Koha.
4
#
5
# Koha is free software; you can redistribute it and/or modify it
6
# under the terms of the GNU General Public License as published by
7
# the Free Software Foundation; either version 3 of the License, or
8
# (at your option) any later version.
9
#
10
# Koha is distributed in the hope that it will be useful, but
11
# WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
# GNU General Public License for more details.
14
#
15
# You should have received a copy of the GNU General Public License
16
# along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18
use Modern::Perl;
19
20
use Test::More tests => 5;
21
use Test::Mojo;
22
use Test::Warn;
23
24
use t::lib::TestBuilder;
25
use t::lib::Mocks;
26
27
use List::Util qw(min);
28
29
use Koha::Biblio::ItemGroups;
30
use Koha::Libraries;
31
use Koha::Database;
32
33
my $schema  = Koha::Database->new->schema;
34
my $builder = t::lib::TestBuilder->new;
35
36
t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );
37
t::lib::Mocks::mock_preference( 'EnableItemGroups', 1 );
38
39
my $t = Test::Mojo->new('Koha::REST::V1');
40
41
subtest 'list() tests' => sub {
42
    plan tests => 9;
43
44
    $schema->storage->txn_begin;
45
46
    my $patron = $builder->build_object({
47
        class => 'Koha::Patrons',
48
        value => { flags => 4 }
49
    });
50
    my $password = 'thePassword123';
51
    $patron->set_password({ password => $password, skip_validation => 1 });
52
    my $userid = $patron->userid;
53
54
    my $biblio = $builder->build_sample_biblio();
55
    my $biblio_id = $biblio->id;
56
57
    $t->get_ok( "//$userid:$password@/api/v1/biblios/$biblio_id/item_groups" )
58
        ->status_is( 200, 'SWAGGER3.2.2' );
59
    my $response_count = scalar @{ $t->tx->res->json };
60
    is( $response_count, 0, 'Results count is 2');
61
62
    my $item_group_1 = Koha::Biblio::ItemGroup->new( { biblio_id => $biblio->id, display_order => 1, description => "Vol 1" } )->store();
63
64
    $t->get_ok( "//$userid:$password@/api/v1/biblios/$biblio_id/item_groups" )
65
        ->status_is( 200, 'SWAGGER3.2.2' );
66
    $response_count = scalar @{ $t->tx->res->json };
67
    is( $response_count, 1, 'Results count is 2');
68
69
    my $item_group_2 = Koha::Biblio::ItemGroup->new( { biblio_id => $biblio->id, display_order => 2, description => "Vol 2" } )->store();
70
71
    $t->get_ok( "//$userid:$password@/api/v1/biblios/$biblio_id/item_groups" )
72
      ->status_is( 200, 'SWAGGER3.2.2' );
73
74
    $response_count = scalar @{ $t->tx->res->json };
75
    is( $response_count, 2, 'Results count is 2');
76
77
    $schema->storage->txn_rollback;
78
};
79
80
subtest 'add() tests' => sub {
81
82
    plan tests => 6;
83
84
    $schema->storage->txn_begin;
85
86
    my $authorized_patron = $builder->build_object({
87
        class => 'Koha::Patrons',
88
        value => { flags => 1 }
89
    });
90
    my $password = 'thePassword123';
91
    $authorized_patron->set_password({ password => $password, skip_validation => 1 });
92
    my $auth_userid = $authorized_patron->userid;
93
94
    my $unauthorized_patron = $builder->build_object({
95
        class => 'Koha::Patrons',
96
        value => { flags => 0 }
97
    });
98
    $unauthorized_patron->set_password({ password => $password, skip_validation => 1 });
99
    my $unauth_userid = $unauthorized_patron->userid;
100
101
    my $biblio = $builder->build_sample_biblio();
102
    my $biblio_id  = $biblio->id;
103
    my $item_group = { description => 'Vol 1', display_order => 1 };
104
105
    # Unauthorized attempt
106
    $t->post_ok( "//$unauth_userid:$password@/api/v1/biblios/$biblio_id/item_groups" => json => $item_group )
107
      ->status_is(403);
108
109
    # Authorized attempt
110
    $t->post_ok( "//$auth_userid:$password@/api/v1/biblios/$biblio_id/item_groups" => json => $item_group )
111
      ->status_is( 201, 'SWAGGER3.2.1' );
112
113
    # Invalid biblio id
114
    {   # hide useless warnings
115
        local *STDERR;
116
        open STDERR, '>', '/dev/null';
117
        $t->post_ok( "//$auth_userid:$password@/api/v1/biblios/XXX/item_groups" => json => $item_group )
118
            ->status_is( 404 );
119
        close STDERR;
120
    }
121
122
    $schema->storage->txn_rollback;
123
};
124
125
subtest 'update() tests' => sub {
126
    plan tests => 9;
127
128
    $schema->storage->txn_begin;
129
130
    my $authorized_patron = $builder->build_object({
131
        class => 'Koha::Patrons',
132
        value => { flags => 1 }
133
    });
134
    my $password = 'thePassword123';
135
    $authorized_patron->set_password({ password => $password, skip_validation => 1 });
136
    my $auth_userid = $authorized_patron->userid;
137
138
    my $unauthorized_patron = $builder->build_object({
139
        class => 'Koha::Patrons',
140
        value => { flags => 0 }
141
    });
142
    $unauthorized_patron->set_password({ password => $password, skip_validation => 1 });
143
    my $unauth_userid = $unauthorized_patron->userid;
144
145
    my $biblio = $builder->build_sample_biblio();
146
    my $biblio_id = $biblio->id;
147
    my $item_group = Koha::Biblio::ItemGroup->new( { biblio_id => $biblio->id, display_order => 1, description => "Vol 1" } )->store();
148
    my $item_group_id = $item_group->id;
149
150
    # Unauthorized attempt
151
    $t->put_ok( "//$unauth_userid:$password@/api/v1/biblios/$biblio_id/item_groups/$item_group_id"
152
                    => json => { description => 'New unauthorized desc change' } )
153
      ->status_is(403);
154
155
    # Authorized attempt
156
    $t->put_ok( "//$auth_userid:$password@/api/v1/biblios/$biblio_id/item_groups/$item_group_id" => json => { description => "Vol A" } )
157
      ->status_is(200, 'SWAGGER3.2.1')
158
      ->json_has( '/description' => "Vol A", 'SWAGGER3.3.3' );
159
160
    # Invalid biblio id
161
    $t->put_ok( "//$auth_userid:$password@/api/v1/biblios/XXX/item_groups/$item_group_id" => json => { description => "Vol A" } )
162
        ->status_is(404);
163
164
    # Invalid item group id
165
    $t->put_ok( "//$auth_userid:$password@/api/v1/biblios/$biblio_id/item_groups/XXX" => json => { description => "Vol A" } )
166
        ->status_is(404);
167
168
    $schema->storage->txn_rollback;
169
};
170
171
subtest 'delete() tests' => sub {
172
    plan tests => 9;
173
174
    $schema->storage->txn_begin;
175
176
    my $authorized_patron = $builder->build_object({
177
        class => 'Koha::Patrons',
178
        value => { flags => 1 }
179
    });
180
    my $password = 'thePassword123';
181
    $authorized_patron->set_password({ password => $password, skip_validation => 1 });
182
    my $auth_userid = $authorized_patron->userid;
183
184
    my $unauthorized_patron = $builder->build_object({
185
        class => 'Koha::Patrons',
186
        value => { flags => 0 }
187
    });
188
    $unauthorized_patron->set_password({ password => $password, skip_validation => 1 });
189
    my $unauth_userid = $unauthorized_patron->userid;
190
191
    my $biblio = $builder->build_sample_biblio();
192
    my $biblio_id = $biblio->id;
193
    my $item_group = Koha::Biblio::ItemGroup->new( { biblio_id => $biblio->id, display_order => 1, description => "Vol 1" } )->store();
194
    my $item_groupid = $item_group->id;
195
196
    $t->delete_ok( "//$auth_userid:$password@/api/v1/biblios/$biblio_id/item_groups/$item_groupid" )
197
        ->status_is(204, 'SWAGGER3.2.4')
198
        ->content_is('', 'SWAGGER3.3.4');
199
200
    # Unauthorized attempt to delete
201
    $t->delete_ok( "//$unauth_userid:$password@/api/v1/biblios/$biblio_id/item_groups/$item_groupid" )
202
      ->status_is(403);
203
204
    $t->delete_ok( "//$auth_userid:$password@/api/v1/biblios/XXX/item_groups/$item_groupid" )
205
      ->status_is(404);
206
207
    $t->delete_ok( "//$auth_userid:$password@/api/v1/biblios/$biblio_id/item_groups/XXX" )
208
      ->status_is(404);
209
210
    $schema->storage->txn_rollback;
211
};
212
213
subtest 'volume items add() + delete() tests' => sub {
214
    plan tests => 14;
215
216
    $schema->storage->txn_begin;
217
218
    my $patron = $builder->build_object({
219
        class => 'Koha::Patrons',
220
        value => { flags => 4 }
221
    });
222
    my $password = 'thePassword123';
223
    $patron->set_password({ password => $password, skip_validation => 1 });
224
    my $userid = $patron->userid;
225
226
    my $biblio = $builder->build_sample_biblio();
227
    my $biblio_id = $biblio->id;
228
229
    my $item_group = Koha::Biblio::ItemGroup->new( { biblio_id => $biblio->id, display_order => 1, description => "Vol 1" } )->store();
230
    my $item_groupid = $item_group->id;
231
232
    my @items = $item_group->items->as_list;
233
    is( scalar(@items), 0, 'Item group has no items');
234
235
    my $item_1 = $builder->build_sample_item({ biblionumber => $biblio->biblionumber });
236
    my $item_1_id = $item_1->id;
237
238
    $t->post_ok( "//$userid:$password@/api/v1/biblios/XXX/item_groups/$item_groupid/items" => json => { item_id => $item_1->id } )
239
      ->status_is( 409 )
240
      ->json_is( { error => 'Item group does not belong to passed biblio_id' } );
241
242
    $t->post_ok( "//$userid:$password@/api/v1/biblios/$biblio_id/item_groups/$item_groupid/items" => json => { item_id => $item_1->id } )
243
      ->status_is( 201, 'SWAGGER3.2.1' );
244
245
    @items = $item_group->items;
246
    is( scalar(@items), 1, 'Item group now has one item');
247
248
    my $item_2 = $builder->build_sample_item({ biblionumber => $biblio->biblionumber });
249
    my $item_2_id = $item_2->id;
250
251
    $t->post_ok( "//$userid:$password@/api/v1/biblios/$biblio_id/item_groups/$item_groupid/items" => json => { item_id => $item_2->id } )
252
      ->status_is( 201, 'SWAGGER3.2.1' );
253
254
    @items = $item_group->items->as_list;
255
    is( scalar(@items), 2, 'Item group now has two items');
256
257
    $t->delete_ok( "//$userid:$password@/api/v1/biblios/$biblio_id/item_groups/$item_groupid/items/$item_1_id" )
258
        ->status_is(204, 'SWAGGER3.2.4')
259
        ->content_is('', 'SWAGGER3.3.4');
260
261
    @items = $item_group->items;
262
    is( scalar(@items), 1, 'Item group now has one item');
263
264
    $schema->storage->txn_rollback;
265
};

Return to bug 24857