Bug 33053: Handle invalid biblio_id more robustly
[koha.git] / Koha / REST / V1 / Biblios / ItemGroups.pm
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
109         my $biblio = Koha::Biblios->find( $c->param('biblio_id') );
110         return $c->render(
111             status  => 404,
112             openapi => { error => 'Object not found' }
113         ) unless $biblio;
114
115         my $item_group_data = $c->req->json;
116         # biblio_id comes from the path
117         $item_group_data->{biblio_id} = $biblio->id;
118
119         my $item_group = Koha::Biblio::ItemGroup->new_from_api($item_group_data);
120         $item_group->store->discard_changes();
121
122         $c->res->headers->location( $c->req->url->to_string . '/' . $item_group->id );
123
124         return $c->render(
125             status  => 201,
126             openapi => $item_group->to_api
127         );
128     }
129     catch {
130         if ( blessed($_) ) {
131             my $to_api_mapping = Koha::Biblio::ItemGroup->new->to_api_mapping;
132
133             if (    $_->isa('Koha::Exceptions::Object::FKConstraint')
134                 and $_->broken_fk eq 'biblio_id' )
135             {
136                 return $c->render(
137                     status  => 404,
138                     openapi => { error => "Biblio not found" }
139                 );
140             }
141         }
142
143         $c->unhandled_exception($_);
144     };
145 }
146
147 =head3 update
148
149 Controller function to handle updating a Koha::Biblio::ItemGroup object
150
151 =cut
152
153 sub update {
154     my $c = shift->openapi->valid_input or return;
155
156     return try {
157         my $item_group_id = $c->validation->param('item_group_id');
158         my $biblio_id     = $c->validation->param('biblio_id');
159
160         my $item_group = Koha::Biblio::ItemGroups->find( $item_group_id );
161
162         unless ( $item_group && $item_group->biblio_id eq $biblio_id ) {
163             return $c->render(
164                 status  => 404,
165                 openapi => {
166                     error => 'Item group not found'
167                 }
168             );
169         }
170
171         my $item_group_data = $c->validation->param('body');
172         $item_group->set_from_api( $item_group_data )->store->discard_changes();
173
174         return $c->render(
175             status  => 200,
176             openapi => $item_group->to_api
177         );
178     }
179     catch {
180         if ( blessed($_) ) {
181             my $to_api_mapping = Koha::Biblio::ItemGroup->new->to_api_mapping;
182
183             if ( $_->isa('Koha::Exceptions::Object::FKConstraint') ) {
184                 return $c->render(
185                     status  => 409,
186                     openapi => {
187                         error => "Given " . $to_api_mapping->{ $_->broken_fk } . " does not exist"
188                     }
189                 );
190             }
191         }
192
193         $c->unhandled_exception($_);
194     };
195 }
196
197 =head3 delete
198
199 Controller function that handles deleting a Koha::Biblio::ItemGroup object
200
201 =cut
202
203 sub delete {
204
205     my $c = shift->openapi->valid_input or return;
206
207     my $item_group_id = $c->validation->param('item_group_id');
208     my $biblio_id     = $c->validation->param('biblio_id');
209
210     my $item_group = Koha::Biblio::ItemGroups->find(
211         { item_group_id => $item_group_id, biblio_id => $biblio_id } );
212
213     if ( not defined $item_group ) {
214         return $c->render(
215             status  => 404,
216             openapi => { error => "Item group not found" }
217         );
218     }
219
220     return try {
221         $item_group->delete;
222         return $c->render( status => 204, openapi => '' );
223     }
224     catch {
225         $c->unhandled_exception($_);
226     };
227 }
228
229 1;