1 package Koha::REST::V1::Preservation::Trains;
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::Preservation::Trains;
23 use Koha::Preservation::Train::Items;
25 use Scalar::Util qw( blessed );
34 Controller function that handles listing the items from a train
39 my $c = shift->openapi->valid_input or return;
42 my $trains_set = Koha::Preservation::Trains->new;
43 my $trains = $c->objects->search( $trains_set );
44 return $c->render( status => 200, openapi => $trains );
47 $c->unhandled_exception($_);
53 Controller function that handles retrieving a single Koha::Preservation::Train object
58 my $c = shift->openapi->valid_input or return;
61 my $train_id = $c->validation->param('train_id');
62 my $train = $c->objects->find( Koha::Preservation::Trains->search, $train_id );
67 openapi => { error => "Train not found" }
77 $c->unhandled_exception($_);
83 Controller function that handles adding a new Koha::Preservation::Train object
88 my $c = shift->openapi->valid_input or return;
91 Koha::Database->new->schema->txn_do(
94 my $body = $c->validation->param('body');
96 my $train = Koha::Preservation::Train->new_from_api($body)->store;
98 $c->res->headers->location($c->req->url->to_string . '/' . $train->train_id);
101 openapi => $train->to_api
108 my $to_api_mapping = Koha::Preservation::Train->new->to_api_mapping;
111 if ( $_->isa('Koha::Exceptions::Object::DuplicateID') ) {
114 openapi => { error => $_->error, conflict => $_->duplicate_id }
117 elsif ( $_->isa('Koha::Exceptions::Object::FKConstraint') ) {
122 . $to_api_mapping->{ $_->broken_fk }
127 elsif ( $_->isa('Koha::Exceptions::BadParameter') ) {
132 . $to_api_mapping->{ $_->parameter }
137 elsif ( $_->isa('Koha::Exceptions::PayloadTooLarge') ) {
140 openapi => { error => $_->error }
145 $c->unhandled_exception($_);
151 Controller function that handles updating a Koha::Preservation::Train object
156 my $c = shift->openapi->valid_input or return;
158 my $train_id = $c->validation->param('train_id');
159 my $train = Koha::Preservation::Trains->find( $train_id );
164 openapi => { error => "Train not found" }
169 Koha::Database->new->schema->txn_do(
172 my $body = $c->validation->param('body');
174 $train->set_from_api($body)->store;
176 $c->res->headers->location($c->req->url->to_string . '/' . $train->train_id);
179 openapi => $train->to_api
185 my $to_api_mapping = Koha::Preservation::Train->new->to_api_mapping;
188 if ( $_->isa('Koha::Exceptions::Object::FKConstraint') ) {
193 . $to_api_mapping->{ $_->broken_fk }
198 elsif ( $_->isa('Koha::Exceptions::BadParameter') ) {
203 . $to_api_mapping->{ $_->parameter }
208 elsif ( $_->isa('Koha::Exceptions::PayloadTooLarge') ) {
211 openapi => { error => $_->error }
216 $c->unhandled_exception($_);
222 Controller function that handles deleting a Koha::Preservation::Train object
227 my $c = shift->openapi->valid_input or return;
229 my $train = Koha::Preservation::Trains->find( $c->validation->param('train_id') );
233 openapi => { error => "Train not found" }
245 $c->unhandled_exception($_);
251 Controller function that handles getting an item from a train
256 my $c = shift->openapi->valid_input or return;
258 my $train_id = $c->validation->param('train_id');
259 my $train = Koha::Preservation::Trains->find( $train_id );
264 openapi => { error => "Train not found" }
268 my $train_item_id = $c->validation->param('train_item_id');
270 my $train_item = $c->objects->find(Koha::Preservation::Train::Items->search, { train_item_id => $train_item_id, train_id => $train_id });
272 unless ($train_item) {
275 openapi => { error => "Item not found" }
280 Koha::Database->new->schema->txn_do(
282 return $c->render( status => 200, openapi => $train_item );
287 $c->unhandled_exception($_);
293 Controller function that handles adding items in batch to a train
298 my $c = shift->openapi->valid_input or return;
300 my $train_id = $c->validation->param('train_id');
301 my $train = Koha::Preservation::Trains->find( $train_id );
306 openapi => { error => "Train not found" }
310 my $body = $c->validation->param('body');
312 Koha::Database->new->schema->txn_do(
314 my $attributes = delete $body->{attributes} // [];
315 my $train_item = $train->add_item($body);
316 $train_item->attributes($attributes);
317 return $c->render( status => 201, openapi => $train_item );
323 if ( $_->isa('Koha::Exceptions::Preservation::MissingSettings') ) {
326 openapi => { error => "MissingSettings", parameter => $_->parameter }
328 } elsif ( $_->isa('Koha::Exceptions::Preservation::ItemNotFound') ) {
331 openapi => { error => "Item not found" }
333 } elsif ( $_->isa('Koha::Exceptions::Object::DuplicateID') ) {
336 openapi => { error => $_->error, conflict => $_->duplicate_id }
338 } elsif ( $_->isa('Koha::Exceptions::Preservation::ItemNotInWaitingList') ) {
341 openapi => { error => 'Item not in waiting list' }
346 $c->unhandled_exception($_);
352 Controller function that handles updating an item from a train
357 my $c = shift->openapi->valid_input or return;
359 my $train_id = $c->validation->param('train_id');
360 my $train = Koha::Preservation::Trains->find( $train_id );
365 openapi => { error => "Train not found" }
369 my $train_item_id = $c->validation->param('train_item_id');
371 my $train_item = Koha::Preservation::Train::Items->search({ train_item_id => $train_item_id, train_id => $train_id })->single;
373 unless ($train_item) {
376 openapi => { error => "Item not found" }
381 Koha::Database->new->schema->txn_do(
383 my $body = $c->validation->param('body');
384 my $attributes = delete $body->{attributes} // [];
386 $train_item->set_from_api($body)->store;
387 $train_item->attributes($attributes);
388 return $c->render( status => 200, openapi => $train_item );
393 $c->unhandled_exception($_);
400 Controller function that handles removing an item from a train
405 my $c = shift->openapi->valid_input or return;
407 my $train_id = $c->validation->param('train_id');
408 my $train = Koha::Preservation::Trains->find( $train_id );
413 openapi => { error => "Train not found" }
417 my $train_item_id = $c->validation->param('train_item_id');
419 my $train_item = $train->items->find($train_item_id);
421 unless ($train_item) {
424 openapi => { error => "Train item not found" }
437 $c->unhandled_exception($_);