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.yaml (+2 lines)
Lines 37-42 invoice: Link Here
37
  $ref: definitions/invoice.yaml
37
  $ref: definitions/invoice.yaml
38
item:
38
item:
39
  $ref: definitions/item.yaml
39
  $ref: definitions/item.yaml
40
item_group:
41
  $ref: definitions/item_group.yaml
40
library:
42
library:
41
  $ref: definitions/library.yaml
43
  $ref: definitions/library.yaml
42
order:
44
order:
(-)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.yaml (+8 lines)
Lines 21-26 Link Here
21
  $ref: paths/biblios.yaml#/~1biblios~1{biblio_id}~1items
21
  $ref: paths/biblios.yaml#/~1biblios~1{biblio_id}~1items
22
"/biblios/{biblio_id}/pickup_locations":
22
"/biblios/{biblio_id}/pickup_locations":
23
  $ref: paths/biblios.yaml#/~1biblios~1{biblio_id}~1pickup_locations
23
  $ref: paths/biblios.yaml#/~1biblios~1{biblio_id}~1pickup_locations
24
"/biblios/{biblio_id}/item_groups":
25
  $ref: paths/biblios_item_groups.yaml#/~1biblios~1{biblio_id}~1item_groups
26
"/biblios/{biblio_id}/item_groups/{item_group_id}":
27
  $ref: paths/biblios_item_groups.yaml#/~1biblios~1{biblio_id}~1item_groups~1{item_group_id}
28
"/biblios/{biblio_id}/item_groups/{item_group_id}/items":
29
  $ref: paths/biblios_item_groups.yaml#/~1biblios~1{biblio_id}~1item_groups~1{item_group_id}~1items
30
"/biblios/{biblio_id}/item_groups/{item_group_id}/items/{item_id}":
31
  $ref: paths/biblios_item_groups.yaml#/~1biblios~1{biblio_id}~1item_groups~1{item_group_id}~1items~1{item_id}
24
"/cash_registers/{cash_register_id}/cashups":
32
"/cash_registers/{cash_register_id}/cashups":
25
  $ref: paths/cash_registers.yaml#/~1cash_registers~1{cash_register_id}~1cashups
33
  $ref: paths/cash_registers.yaml#/~1cash_registers~1{cash_register_id}~1cashups
26
"/cashups/{cashup_id}":
34
"/cashups/{cashup_id}":
(-)a/api/v1/swagger/paths/biblios_item_groups.yaml (+400 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": "../parameters.yaml#/match"
16
    - "$ref": "../parameters.yaml#/order_by"
17
    - "$ref": "../parameters.yaml#/page"
18
    - "$ref": "../parameters.yaml#/per_page"
19
    - "$ref": "../parameters.yaml#/q_param"
20
    - "$ref": "../parameters.yaml#/q_body"
21
    - "$ref": "../parameters.yaml#/q_header"
22
    consumes:
23
    - application/yaml
24
    produces:
25
    - application/yaml
26
    responses:
27
      '200':
28
        description: A list of item_groups
29
        schema:
30
          type: array
31
          items:
32
            "$ref": "../definitions.yaml#/item_group"
33
      '401':
34
        description: Authentication required
35
        schema:
36
          "$ref": "../definitions.yaml#/error"
37
      '403':
38
        description: Access forbidden
39
        schema:
40
          "$ref": "../definitions.yaml#/error"
41
      '500':
42
        description: Internal server error
43
        schema:
44
          "$ref": "../definitions.yaml#/error"
45
      '503':
46
        description: Under maintenance
47
        schema:
48
          "$ref": "../definitions.yaml#/error"
49
    x-koha-authorization:
50
      permissions:
51
        catalogue: "manage_item_groups"
52
    x-koha-embed:
53
    - items
54
  post:
55
    x-mojo-to: Biblios::ItemGroups#add
56
    operationId: addItemGroup
57
    tags:
58
    - item_groups
59
    summary: Add item group
60
    parameters:
61
    - name: biblio_id
62
      in: path
63
      description: Internal identifier for the parent bibliographic record
64
      required: true
65
      type: string
66
    - name: body
67
      in: body
68
      description: A JSON object representing an item group
69
      required: true
70
      schema:
71
        type: object
72
        properties:
73
          description:
74
            type: string
75
            description: ItemGroup description
76
          display_order:
77
            type: integer
78
            description: Position in waiting queue
79
    consumes:
80
    - application/yaml
81
    produces:
82
    - application/yaml
83
    responses:
84
      '201':
85
        description: A successfully created item group
86
        schema:
87
          "$ref": "../definitions.yaml#/item_group"
88
      '400':
89
        description: Bad parameter
90
        schema:
91
          "$ref": "../definitions.yaml#/error"
92
      '401':
93
        description: Authentication required
94
        schema:
95
          "$ref": "../definitions.yaml#/error"
96
      '403':
97
        description: Access forbidden
98
        schema:
99
          "$ref": "../definitions.yaml#/error"
100
      '404':
101
        description: Resource not found
102
        schema:
103
          "$ref": "../definitions.yaml#/error"
104
      '409':
105
        description: Conflict in creating resource
106
        schema:
107
          "$ref": "../definitions.yaml#/error"
108
      '500':
109
        description: Internal server error
110
        schema:
111
          "$ref": "../definitions.yaml#/error"
112
      '503':
113
        description: Under maintenance
114
        schema:
115
          "$ref": "../definitions.yaml#/error"
116
    x-koha-authorization:
117
      permissions:
118
        catalogue: "manage_item_groups"
119
"/biblios/{biblio_id}/item_groups/{item_group_id}":
120
  get:
121
    x-mojo-to: Biblios::ItemGroups#get
122
    operationId: getItemGroup
123
    tags:
124
    - item_groups
125
    summary: Get item group
126
    parameters:
127
    - name: biblio_id
128
      in: path
129
      description: Internal identifier for the parent bibliographic record
130
      required: true
131
      type: string
132
    - name: item_group_id
133
      in: path
134
      description: Internal identifier for the item_group
135
      required: true
136
      type: string
137
    consumes:
138
    - application/yaml
139
    produces:
140
    - application/yaml
141
    responses:
142
      '200':
143
        description: An item group
144
        schema:
145
          "$ref": "../definitions.yaml#/item_group"
146
      '400':
147
        description: Missing or wrong parameters
148
        schema:
149
          "$ref": "../definitions.yaml#/error"
150
      '404':
151
        description: ItemGroup not found
152
        schema:
153
          "$ref": "../definitions.yaml#/error"
154
      '500':
155
        description: Internal server error
156
        schema:
157
          "$ref": "../definitions.yaml#/error"
158
      '503':
159
        description: Under maintenance
160
        schema:
161
          "$ref": "../definitions.yaml#/error"
162
    x-koha-embed:
163
    - items
164
    x-koha-authorization:
165
      permissions:
166
        catalogue: "manage_item_groups"
167
  put:
168
    x-mojo-to: Biblios::ItemGroups#update
169
    operationId: updateItemGroup
170
    tags:
171
    - item_groups
172
    summary: Update item group
173
    parameters:
174
    - name: biblio_id
175
      in: path
176
      description: Internal identifier for the parent bibliographic record
177
      required: true
178
      type: string
179
    - name: item_group_id
180
      in: path
181
      description: Internal identifier for the item group
182
      required: true
183
      type: string
184
    - name: body
185
      in: body
186
      description: A JSON object with the new values for the item group
187
      required: true
188
      schema:
189
        type: object
190
        properties:
191
          description:
192
            type: string
193
            description: ItemGroup description
194
          display_order:
195
            type: integer
196
            description: Position in waiting queue
197
    consumes:
198
    - application/yaml
199
    produces:
200
    - application/yaml
201
    responses:
202
      '200':
203
        description: The updated item group
204
        schema:
205
          "$ref": "../definitions.yaml#/item_group"
206
      '400':
207
        description: Bad request
208
        schema:
209
          "$ref": "../definitions.yaml#/error"
210
      '401':
211
        description: Authentication required
212
        schema:
213
          "$ref": "../definitions.yaml#/error"
214
      '403':
215
        description: Access forbidden
216
        schema:
217
          "$ref": "../definitions.yaml#/error"
218
      '404':
219
        description: ItemGroup not found
220
        schema:
221
          "$ref": "../definitions.yaml#/error"
222
      '500':
223
        description: Internal error
224
        schema:
225
          "$ref": "../definitions.yaml#/error"
226
      '503':
227
        description: Under maintenance
228
        schema:
229
          "$ref": "../definitions.yaml#/error"
230
    x-koha-authorization:
231
      permissions:
232
        catalogue: "manage_item_groups"
233
    x-koha-embed:
234
    - items
235
  delete:
236
    x-mojo-to: Biblios::ItemGroups#delete
237
    operationId: deleteItemGroup
238
    tags:
239
    - item_groups
240
    summary: Delete item group
241
    parameters:
242
    - name: biblio_id
243
      in: path
244
      description: Internal identifier for the parent bibliographic record
245
      required: true
246
      type: string
247
    - name: item_group_id
248
      in: path
249
      description: Internal identifier for the item group
250
      required: true
251
      type: string
252
    produces:
253
    - application/yaml
254
    responses:
255
      '204':
256
        description: ItemGroup deleted
257
        schema:
258
          type: string
259
      '401':
260
        description: Authentication required
261
        schema:
262
          "$ref": "../definitions.yaml#/error"
263
      '403':
264
        description: Access forbidden
265
        schema:
266
          "$ref": "../definitions.yaml#/error"
267
      '404':
268
        description: ItemGroup not found
269
        schema:
270
          "$ref": "../definitions.yaml#/error"
271
      '500':
272
        description: Internal error
273
        schema:
274
          "$ref": "../definitions.yaml#/error"
275
      '503':
276
        description: Under maintenance
277
        schema:
278
          "$ref": "../definitions.yaml#/error"
279
    x-koha-authorization:
280
      permissions:
281
        catalogue: "manage_item_groups"
282
"/biblios/{biblio_id}/item_groups/{item_group_id}/items":
283
  post:
284
    x-mojo-to: Biblios::ItemGroups::Items#add
285
    operationId: addItemGroupItem
286
    tags:
287
    - item_groups
288
    summary: Add item to item group
289
    parameters:
290
    - name: biblio_id
291
      in: path
292
      description: Internal identifier for the parent bibliographic record
293
      required: true
294
      type: string
295
    - name: item_group_id
296
      in: path
297
      description: Internal identifier for the item group
298
      required: true
299
      type: string
300
    - name: body
301
      in: body
302
      description: A JSON object containing an item_id
303
      required: true
304
      schema:
305
        type: object
306
        properties:
307
          item_id:
308
            type: integer
309
            description: Internal identifier for an item to be linked
310
    consumes:
311
    - application/yaml
312
    produces:
313
    - application/yaml
314
    responses:
315
      '201':
316
        description: Item linked to item group
317
      '400':
318
        description: Bad request
319
        schema:
320
          "$ref": "../definitions.yaml#/error"
321
      '401':
322
        description: Authentication required
323
        schema:
324
          "$ref": "../definitions.yaml#/error"
325
      '403':
326
        description: Access forbidden
327
        schema:
328
          "$ref": "../definitions.yaml#/error"
329
      '409':
330
        description: Request conflicts
331
        schema:
332
          "$ref": "../definitions.yaml#/error"
333
      '500':
334
        description: Internal error
335
        schema:
336
          "$ref": "../definitions.yaml#/error"
337
      '503':
338
        description: Under maintenance
339
        schema:
340
          "$ref": "../definitions.yaml#/error"
341
    x-koha-authorization:
342
      permissions:
343
        catalogue: "manage_item_groups"
344
    x-koha-embed:
345
    - items
346
"/biblios/{biblio_id}/item_groups/{item_group_id}/items/{item_id}":
347
  delete:
348
    x-mojo-to: Biblios::ItemGroups::Items#delete
349
    operationId: deleteItemGroupItems
350
    tags:
351
    - item_groups
352
    summary: Delete item from item group
353
    parameters:
354
    - name: biblio_id
355
      in: path
356
      description: Internal identifier for the parent bibliographic record
357
      required: true
358
      type: string
359
    - name: item_group_id
360
      in: path
361
      description: Internal identifier for the item group
362
      required: true
363
      type: string
364
    - name: item_id
365
      in: path
366
      description: Internal identifier for the item
367
      required: true
368
      type: string
369
    consumes:
370
    - application/yaml
371
    produces:
372
    - application/yaml
373
    responses:
374
      '204':
375
        description: Item unlinked from item group
376
        schema:
377
          type: string
378
      '401':
379
        description: Authentication required
380
        schema:
381
          "$ref": "../definitions.yaml#/error"
382
      '403':
383
        description: Access forbidden
384
        schema:
385
          "$ref": "../definitions.yaml#/error"
386
      '404':
387
        description: Item not linked to item group
388
        schema:
389
          "$ref": "../definitions.yaml#/error"
390
      '500':
391
        description: Internal error
392
        schema:
393
          "$ref": "../definitions.yaml#/error"
394
      '503':
395
        description: Under maintenance
396
        schema:
397
          "$ref": "../definitions.yaml#/error"
398
    x-koha-authorization:
399
      permissions:
400
        catalogue: "manage_item_groups"
(-)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;
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;
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