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

(-)a/Koha/REST/V1/Biblios/Volumes.pm (+252 lines)
Line 0 Link Here
1
package Koha::REST::V1::Biblios::Volumes;
2
3
# This file is part of Koha.
4
#
5
# Koha is free software; you can redistribute it and/or modify it under the
6
# terms of the GNU General Public License as published by the Free Software
7
# Foundation; either version 3 of the License, or (at your option) any later
8
# version.
9
#
10
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
11
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12
# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
13
#
14
# You should have received a copy of the GNU General Public License along
15
# with Koha; if not, write to the Free Software Foundation, Inc.,
16
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17
18
use Modern::Perl;
19
20
use Mojo::Base 'Mojolicious::Controller';
21
22
use Koha::Biblio::Volumes;
23
24
use Scalar::Util qw(blessed);
25
use Try::Tiny;
26
27
=head1 NAME
28
29
Koha::REST::V1::Biblios::Volumes - Koha REST API for handling volumes (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::Volume objects
40
41
=cut
42
43
sub list {
44
    my $c = shift->openapi->valid_input or return;
45
46
    return try {
47
        my $volumes_set = Koha::Biblio::Volumes->new;
48
        my $volumes     = $c->objects->search( $volumes_set );
49
        return $c->render(
50
            status  => 200,
51
            openapi => $volumes
52
        );
53
    }
54
    catch {
55
        unless ( blessed $_ && $_->can('rethrow') ) {
56
            return $c->render(
57
                status  => 500,
58
                openapi => {
59
                    error =>
60
                      "Something went wrong, check Koha logs for details."
61
                }
62
            );
63
        }
64
        return $c->render(
65
            status  => 500,
66
            openapi => { error => "$_" }
67
        );
68
    };
69
}
70
71
=head3 get
72
73
Controller function that handles retrieving a single Koha::Biblio::Volume
74
75
=cut
76
77
sub get {
78
    my $c = shift->openapi->valid_input or return;
79
80
    try {
81
        my $volume_id = $c->validation->param('volume_id');
82
        my $biblio_id = $c->validation->param('biblio_id');
83
84
        my $volume = Koha::Biblio::Volumes->find( $volume_id );
85
        my $embed = $c->stash('koha.embed');
86
87
        if ( $volume && $volume->biblionumber eq $biblio_id ) {
88
            return $c->render(
89
                status  => 200,
90
                openapi => $volume->to_api({ embed => $embed })
91
            );
92
        }
93
        else {
94
            return $c->render(
95
                status  => 404,
96
                openapi => {
97
                    error => 'Volume not found'
98
                }
99
            );
100
        }
101
    }
102
    catch {
103
        return $c->render(
104
            status  => 500,
105
            openapi => {
106
                error => "Something went wrong, check the logs ($_)."
107
            }
108
        );
109
    };
110
}
111
112
=head3 add
113
114
Controller function to handle adding a Koha::Biblio::Volume object
115
116
=cut
117
118
sub add {
119
    my $c = shift->openapi->valid_input or return;
120
121
    return try {
122
        my $volume_data = $c->validation->param('body');
123
        # biblio_id comes from the path
124
        $volume_data->{biblio_id} = $c->validation->param('biblio_id');
125
126
        my $volume = Koha::Biblio::Volume->new_from_api($volume_data);
127
        $volume->store->discard_changes();
128
129
        $c->res->headers->location( $c->req->url->to_string . '/' . $volume->id );
130
131
        return $c->render(
132
            status  => 201,
133
            openapi => $volume->to_api
134
        );
135
    }
136
    catch {
137
        if ( blessed($_) ) {
138
            my $to_api_mapping = Koha::Biblio::Volume->new->to_api_mapping;
139
140
            if ( $_->isa('Koha::Exceptions::Object::FKConstraint') ) {
141
                return $c->render(
142
                    status  => 409,
143
                    openapi => {
144
                        error => "Given " . $to_api_mapping->{ $_->broken_fk } . " does not exist"
145
                    }
146
                );
147
            }
148
        }
149
150
        return $c->render(
151
            status  => 500,
152
            openapi => {
153
                error => "Unhandled exception ($_)"
154
            }
155
        );
156
    };
157
}
158
159
=head3 update
160
161
Controller function to handle updating a Koha::Biblio::Volume object
162
163
=cut
164
165
sub update {
166
    my $c = shift->openapi->valid_input or return;
167
168
    return try {
169
        my $volume_id = $c->validation->param('volume_id');
170
        my $biblio_id = $c->validation->param('biblio_id');
171
172
        my $volume = Koha::Biblio::Volumes->find( $volume_id );
173
174
        unless ( $volume && $volume->biblionumber eq $biblio_id ) {
175
            return $c->render(
176
                status  => 404,
177
                openapi => {
178
                    error => 'Volume not found'
179
                }
180
            );
181
        }
182
183
        my $volume_data = $c->validation->param('body');
184
        $volume->set_from_api( $volume_data )->store->discard_changes();
185
186
        return $c->render(
187
            status  => 200,
188
            openapi => $volume->to_api
189
        );
190
    }
191
    catch {
192
        if ( blessed($_) ) {
193
            my $to_api_mapping = Koha::Biblio::Volume->new->to_api_mapping;
194
195
            if ( $_->isa('Koha::Exceptions::Object::FKConstraint') ) {
196
                return $c->render(
197
                    status  => 409,
198
                    openapi => {
199
                        error => "Given " . $to_api_mapping->{ $_->broken_fk } . " does not exist"
200
                    }
201
                );
202
            }
203
        }
204
205
        return $c->render(
206
            status  => 500,
207
            openapi => {
208
                error => "Unhandled exception ($_)"
209
            }
210
        );
211
    };
212
}
213
214
=head3 delete
215
216
Controller function that handles deleting a Koha::Biblio::Volume object
217
218
=cut
219
220
sub delete {
221
222
    my $c = shift->openapi->valid_input or return;
223
224
    my $volume_id = $c->validation->param( 'volume_id' );
225
    my $biblio_id = $c->validation->param( 'biblio_id' );
226
227
    my $volume = Koha::Biblio::Volumes->find({ id => $volume_id, biblionumber => $biblio_id });
228
229
    if ( not defined $volume ) {
230
        return $c->render( status => 404, openapi => { error => "Volume not found" } );
231
    }
232
233
    return try {
234
        $volume->delete;
235
        return $c->render( status => 204, openapi => '');
236
    }
237
    catch {
238
        unless ( blessed $_ && $_->can('rethrow') ) {
239
            return $c->render(
240
                status  => 500,
241
                openapi => { error => "Something went wrong, check Koha logs for details." }
242
            );
243
        }
244
245
        return $c->render(
246
            status  => 500,
247
            openapi => { error => "$_" }
248
        );
249
    };
250
}
251
252
1;
(-)a/Koha/REST/V1/Biblios/Volumes/Items.pm (-1 / +165 lines)
Line 0 Link Here
0
- 
1
package Koha::REST::V1::Biblios::Volumes::Items;
2
3
# This file is part of Koha.
4
#
5
# Koha is free software; you can redistribute it and/or modify it under the
6
# terms of the GNU General Public License as published by the Free Software
7
# Foundation; either version 3 of the License, or (at your option) any later
8
# version.
9
#
10
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
11
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12
# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
13
#
14
# You should have received a copy of the GNU General Public License along
15
# with Koha; if not, write to the Free Software Foundation, Inc.,
16
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17
18
use Modern::Perl;
19
20
use Mojo::Base 'Mojolicious::Controller';
21
22
use Koha::Biblio::Volume::Items;
23
24
use Scalar::Util qw(blessed);
25
use Try::Tiny;
26
27
=head1 NAME
28
29
Koha::REST::V1::Biblios::Volumes::Items - Koha REST API for handling volume items (V1)
30
31
=head1 API
32
33
=head2 Methods
34
35
=head3 add
36
37
Controller function to handle adding a Koha::Biblio::Volume object
38
39
=cut
40
41
sub add {
42
    my $c = shift->openapi->valid_input or return;
43
44
    return try {
45
46
        my $volume = Koha::Biblio::Volumes->find(
47
            $c->validation->param('volume_id')
48
        );
49
50
        unless ( $volume ) {
51
            return $c->render(
52
                status  => 404,
53
                openapi => {
54
                    error => 'Volume not found'
55
                }
56
            );
57
        }
58
59
        # All good, add the item
60
        my $body    = $c->validation->param('body');
61
        my $item_id = $body->{item_id};
62
63
        $volume->add_item({ item_id => $item_id });
64
65
        $c->res->headers->location( $c->req->url->to_string . '/' . $item_id );
66
67
        my $embed = $c->stash('koha.embed');
68
69
        return $c->render(
70
            status  => 201,
71
            openapi => $volume->to_api({ embed => $embed })
72
        );
73
    }
74
    catch {
75
        if ( blessed($_) ) {
76
77
            if ( $_->isa('Koha::Exceptions::Object::FKConstraint') ) {
78
                if ( $_->broken_fk eq 'itemnumber' ) {
79
                    return $c->render(
80
                        status  => 409,
81
                        openapi => {
82
                            error => "Given item_id does not exist"
83
                        }
84
                    );
85
                }
86
                elsif ( $_->broken_fk eq 'biblionumber' ) {
87
                    return $c->render(
88
                        status  => 409,
89
                        openapi => {
90
                            error => "Given item_id does not belong to the volume's biblio"
91
                        }
92
                    );
93
                }
94
            }
95
            elsif ( $_->isa('Koha::Exceptions::Object::DuplicateID') ) {
96
97
                return $c->render(
98
                    status  => 409,
99
                    openapi => {
100
                        error => "The given item_id is already linked to the volume"
101
                    }
102
                );
103
            }
104
        }
105
106
        return $c->render(
107
            status  => 500,
108
            openapi => {
109
                error => "Unhandled exception ($_)"
110
            }
111
        );
112
    };
113
}
114
115
=head3 delete
116
117
Controller function that handles deleting a Koha::Biblio::Volume object
118
119
=cut
120
121
sub delete {
122
    my $c = shift->openapi->valid_input or return;
123
124
    my $volume_id = $c->validation->param('volume_id');
125
    my $item_id   = $c->validation->param('item_id');
126
127
    my $item_link = Koha::Biblio::Volume::Items->find(
128
        {
129
            itemnumber => $item_id,
130
            volume_id  => $volume_id
131
        }
132
    );
133
134
    unless ( $item_link ) {
135
        return $c->render(
136
            status  => 404,
137
            openapi => {
138
                error => 'No such volume <-> item relationship'
139
            }
140
        );
141
    }
142
143
    return try {
144
        $item_link->delete;
145
        return $c->render(
146
            status  => 204,
147
            openapi => ''
148
        );
149
    }
150
    catch {
151
        unless ( blessed $_ && $_->can('rethrow') ) {
152
            return $c->render(
153
                status  => 500,
154
                openapi => { error => "Something went wrong, check Koha logs for details." }
155
            );
156
        }
157
158
        return $c->render(
159
            status  => 500,
160
            openapi => { error => "$_" }
161
        );
162
    };
163
}
164
165
1;

Return to bug 24857