1 package Koha::REST::V1::Biblios::ItemGroups;
3 # This file is part of Koha.
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.
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.
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>.
20 use Mojo::Base 'Mojolicious::Controller';
22 use Koha::Biblio::ItemGroups;
24 use Scalar::Util qw(blessed);
29 Koha::REST::V1::Biblios::ItemGroups - Koha REST API for handling item groups (V1)
39 Controller function that handles listing Koha::Biblio::ItemGroup objects
44 my $c = shift->openapi->valid_input or return;
45 my $biblio_id = $c->validation->param('biblio_id');
47 my $biblio=Koha::Biblios->find( $biblio_id);
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 );
55 openapi => $item_groups
59 $c->unhandled_exception($_);
65 Controller function that handles retrieving a single Koha::Biblio::ItemGroup
70 my $c = shift->openapi->valid_input or return;
73 my $item_group_id = $c->validation->param('item_group_id');
74 my $biblio_id = $c->validation->param('biblio_id');
76 my $item_group = $c->objects->find( Koha::Biblio::ItemGroups->new, $item_group_id );
78 if ( $item_group && $item_group->{biblio_id} eq $biblio_id ) {
81 openapi => $item_group
88 error => 'Item group not found'
94 $c->unhandled_exception($_);
100 Controller function to handle adding a Koha::Biblio::ItemGroup object
105 my $c = shift->openapi->valid_input or return;
109 my $biblio = Koha::Biblios->find( $c->param('biblio_id') );
112 openapi => { error => 'Object not found' }
115 my $item_group_data = $c->req->json;
116 # biblio_id comes from the path
117 $item_group_data->{biblio_id} = $biblio->id;
119 my $item_group = Koha::Biblio::ItemGroup->new_from_api($item_group_data);
120 $item_group->store->discard_changes();
122 $c->res->headers->location( $c->req->url->to_string . '/' . $item_group->id );
126 openapi => $item_group->to_api
131 my $to_api_mapping = Koha::Biblio::ItemGroup->new->to_api_mapping;
133 if ( $_->isa('Koha::Exceptions::Object::FKConstraint')
134 and $_->broken_fk eq 'biblio_id' )
138 openapi => { error => "Biblio not found" }
143 $c->unhandled_exception($_);
149 Controller function to handle updating a Koha::Biblio::ItemGroup object
154 my $c = shift->openapi->valid_input or return;
157 my $item_group_id = $c->validation->param('item_group_id');
158 my $biblio_id = $c->validation->param('biblio_id');
160 my $item_group = Koha::Biblio::ItemGroups->find( $item_group_id );
162 unless ( $item_group && $item_group->biblio_id eq $biblio_id ) {
166 error => 'Item group not found'
171 my $item_group_data = $c->validation->param('body');
172 $item_group->set_from_api( $item_group_data )->store->discard_changes();
176 openapi => $item_group->to_api
181 my $to_api_mapping = Koha::Biblio::ItemGroup->new->to_api_mapping;
183 if ( $_->isa('Koha::Exceptions::Object::FKConstraint') ) {
187 error => "Given " . $to_api_mapping->{ $_->broken_fk } . " does not exist"
193 $c->unhandled_exception($_);
199 Controller function that handles deleting a Koha::Biblio::ItemGroup object
205 my $c = shift->openapi->valid_input or return;
207 my $item_group_id = $c->validation->param('item_group_id');
208 my $biblio_id = $c->validation->param('biblio_id');
210 my $item_group = Koha::Biblio::ItemGroups->find(
211 { item_group_id => $item_group_id, biblio_id => $biblio_id } );
213 if ( not defined $item_group ) {
216 openapi => { error => "Item group not found" }
222 return $c->render( status => 204, openapi => '' );
225 $c->unhandled_exception($_);