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

(-)a/Koha/REST/V1/Biblios/Volumes.pm (+244 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 = Koha::Biblio::Volumes->find($c->validation->param('volume_id'));
82
        my $embed = $c->stash('koha.embed');
83
84
        if ( $volume ) {
85
            return $c->render(
86
                status  => 200,
87
                openapi => $volume->to_api({ embed => $embed })
88
            );
89
        }
90
        else {
91
            return $c->render(
92
                status  => 404,
93
                openapi => {
94
                    error => 'Volume not found'
95
                }
96
            );
97
        }
98
    }
99
    catch {
100
        return $c->render(
101
            status  => 500,
102
            openapi => {
103
                error => "Something went wrong, check the logs ($_)."
104
            }
105
        );
106
    };
107
}
108
109
=head3 add
110
111
Controller function to handle adding a Koha::Biblio::Volume object
112
113
=cut
114
115
sub add {
116
    my $c = shift->openapi->valid_input or return;
117
118
    return try {
119
        my $volume_data = $c->validation->param('body');
120
        # biblio_id comes from the path
121
        $volume_data->{biblio_id} = $c->validation->param('biblio_id');
122
123
        my $volume = Koha::Biblio::Volume->new_from_api($volume_data);
124
        $volume->store->discard_changes();
125
126
        $c->res->headers->location( $c->req->url->to_string . '/' . $volume->id );
127
128
        return $c->render(
129
            status  => 201,
130
            openapi => $volume->to_api
131
        );
132
    }
133
    catch {
134
        if ( blessed($_) ) {
135
            my $to_api_mapping = Koha::Biblio::Volume->new->to_api_mapping;
136
137
            if ( $_->isa('Koha::Exceptions::Object::FKConstraint') ) {
138
                return $c->render(
139
                    status  => 409,
140
                    openapi => {
141
                        error => "Given " . $to_api_mapping->{ $_->broken_fk } . " does not exist"
142
                    }
143
                );
144
            }
145
        }
146
147
        return $c->render(
148
            status  => 500,
149
            openapi => {
150
                error => "Unhandled exception ($_)"
151
            }
152
        );
153
    };
154
}
155
156
=head3 update
157
158
Controller function to handle updating a Koha::Biblio::Volume object
159
160
=cut
161
162
sub update {
163
    my $c = shift->openapi->valid_input or return;
164
165
    return try {
166
        my $volume_id = $c->validation->param('volume_id');
167
        my $volume = Koha::Biblio::Volumes->find( $volume_id );
168
169
        unless ( $volume ) {
170
            return $c->render(
171
                status  => 404,
172
                openapi => {
173
                    error => 'Volume not found'
174
                }
175
            );
176
        }
177
178
        my $volume_data = $c->validation->param('body');
179
        $volume->set_from_api( $volume_data )->store->discard_changes();
180
181
        return $c->render(
182
            status  => 200,
183
            openapi => $volume->to_api
184
        );
185
    }
186
    catch {
187
        if ( blessed($_) ) {
188
            my $to_api_mapping = Koha::Biblio::Volume->new->to_api_mapping;
189
190
            if ( $_->isa('Koha::Exceptions::Object::FKConstraint') ) {
191
                return $c->render(
192
                    status  => 409,
193
                    openapi => {
194
                        error => "Given " . $to_api_mapping->{ $_->broken_fk } . " does not exist"
195
                    }
196
                );
197
            }
198
        }
199
200
        return $c->render(
201
            status  => 500,
202
            openapi => {
203
                error => "Unhandled exception ($_)"
204
            }
205
        );
206
    };
207
}
208
209
=head3 delete
210
211
Controller function that handles deleting a Koha::Biblio::Volume object
212
213
=cut
214
215
sub delete {
216
217
    my $c = shift->openapi->valid_input or return;
218
219
    my $volume = Koha::Biblio::Volumes->find( $c->validation->param( 'volume_id' ) );
220
221
    if ( not defined $volume ) {
222
        return $c->render( status => 404, openapi => { error => "Volume not found" } );
223
    }
224
225
    return try {
226
        $volume->delete;
227
        return $c->render( status => 204, openapi => '');
228
    }
229
    catch {
230
        unless ( blessed $_ && $_->can('rethrow') ) {
231
            return $c->render(
232
                status  => 500,
233
                openapi => { error => "Something went wrong, check Koha logs for details." }
234
            );
235
        }
236
237
        return $c->render(
238
            status  => 500,
239
            openapi => { error => "$_" }
240
        );
241
    };
242
}
243
244
1;
(-)a/Koha/REST/V1/Biblios/Volumes/Items.pm (-1 / +166 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
123
    my $c = shift->openapi->valid_input or return;
124
125
    my $volume_id = $c->validation->param('volume_id');
126
    my $item_id   = $c->validation->param('item_id');
127
128
    my $item_link = Koha::Biblio::Volumes::Items->find(
129
        {
130
            itemnumber => $item_id,
131
            volume_id  => $volume_id
132
        }
133
    );
134
135
    unless ( $item_link ) {
136
        return $c->render(
137
            status  => 404,
138
            openapi => {
139
                error => 'No such volume <-> item relationship'
140
            }
141
        );
142
    }
143
144
    return try {
145
        $item_link->delete;
146
        return $c->render(
147
            status  => 204,
148
            openapi => ''
149
        );
150
    }
151
    catch {
152
        unless ( blessed $_ && $_->can('rethrow') ) {
153
            return $c->render(
154
                status  => 500,
155
                openapi => { error => "Something went wrong, check Koha logs for details." }
156
            );
157
        }
158
159
        return $c->render(
160
            status  => 500,
161
            openapi => { error => "$_" }
162
        );
163
    };
164
}
165
166
1;

Return to bug 24857