From c71098457ebfb63b717ab77f199542e33ed3e677 Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Tue, 24 May 2022 11:15:16 +0200 Subject: [PATCH] Bug 32030: eHoldings - Koha classes Signed-off-by: Jonathan Field Signed-off-by: Martin Renvoize Signed-off-by: Kyle M Hall Signed-off-by: Tomas Cohen Arazi --- Koha/ERM/EHolding.pm | 45 +++++++ Koha/ERM/EHoldings.pm | 49 +++++++ Koha/REST/V1/ERM/EHoldings.pm | 233 ++++++++++++++++++++++++++++++++++ 3 files changed, 327 insertions(+) create mode 100644 Koha/ERM/EHolding.pm create mode 100644 Koha/ERM/EHoldings.pm create mode 100644 Koha/REST/V1/ERM/EHoldings.pm diff --git a/Koha/ERM/EHolding.pm b/Koha/ERM/EHolding.pm new file mode 100644 index 0000000000..1af43cda8b --- /dev/null +++ b/Koha/ERM/EHolding.pm @@ -0,0 +1,45 @@ +package Koha::ERM::EHolding; + +# This file is part of Koha. +# +# Koha is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# Koha is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Koha; if not, see . + +use Modern::Perl; + +use Koha::Database; + +use base qw(Koha::Object); + +use Koha::ERM::Packages; +use Koha::Acquisition::Booksellers; + +=head1 NAME + +Koha::ERM::EHolding - Koha ERM EHolding Object class + +=head1 API + +=head2 Class Methods + +=head2 Internal methods + +=head3 _type + +=cut + +sub _type { + return 'ErmEholding'; +} + +1; diff --git a/Koha/ERM/EHoldings.pm b/Koha/ERM/EHoldings.pm new file mode 100644 index 0000000000..dd6295914b --- /dev/null +++ b/Koha/ERM/EHoldings.pm @@ -0,0 +1,49 @@ +package Koha::ERM::EHoldings; + +# This file is part of Koha. +# +# Koha is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# Koha is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Koha; if not, see . + +use Modern::Perl; + + +use Koha::Database; + +use Koha::ERM::EHolding; + +use base qw(Koha::Objects); + +=head1 NAME + +Koha::ERM::EHoldings - Koha ERM EHolding Object set class + +=head1 API + +=head2 Class Methods + +=cut + +=head3 type + +=cut + +sub _type { + return 'ErmEholding'; +} + +sub object_class { + return 'Koha::ERM::EHolding'; +} + +1; diff --git a/Koha/REST/V1/ERM/EHoldings.pm b/Koha/REST/V1/ERM/EHoldings.pm new file mode 100644 index 0000000000..01acab0b14 --- /dev/null +++ b/Koha/REST/V1/ERM/EHoldings.pm @@ -0,0 +1,233 @@ +package Koha::REST::V1::ERM::EHoldings; + +# This file is part of Koha. +# +# Koha is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# Koha is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Koha; if not, see . + +use Modern::Perl; + +use Mojo::Base 'Mojolicious::Controller'; + +use Koha::ERM::EHoldings; + +use Scalar::Util qw( blessed ); +use Try::Tiny qw( catch try ); + +=head1 API + +=head2 Methods + +=head3 list + +=cut + +sub list { + my $c = shift->openapi->valid_input or return; + + return try { + my $eholdings_set = Koha::ERM::EHoldings->new; + my $eholdings = $c->objects->search( $eholdings_set ); + return $c->render( status => 200, openapi => $eholdings ); + } + catch { + $c->unhandled_exception($_); + }; + +} + +=head3 get + +Controller function that handles retrieving a single Koha::ERM::EHolding object + +=cut + +sub get { + my $c = shift->openapi->valid_input or return; + + return try { + my $eholding_id = $c->validation->param('eholding_id'); + my $eholding = $c->objects->find( Koha::ERM::EHoldings->search, $eholding_id ); + + unless ($eholding) { + return $c->render( + status => 404, + openapi => { error => "eHolding not found" } + ); + } + + return $c->render( + status => 200, + openapi => $eholding + ); + } + catch { + $c->unhandled_exception($_); + }; +} + +=head3 add + +Controller function that handles adding a new Koha::ERM::EHolding object + +=cut + +sub add { + my $c = shift->openapi->valid_input or return; + + return try { + Koha::Database->new->schema->txn_do( + sub { + + my $body = $c->validation->param('body'); + + my $eholding = Koha::ERM::EHolding->new_from_api($body)->store; + + $c->res->headers->location($c->req->url->to_string . '/' . $eholding->eholding_id); + return $c->render( + status => 201, + openapi => $eholding->to_api + ); + } + ); + } + catch { + + my $to_api_mapping = Koha::ERM::EHolding->new->to_api_mapping; + + if ( blessed $_ ) { + if ( $_->isa('Koha::Exceptions::Object::DuplicateID') ) { + return $c->render( + status => 409, + openapi => { error => $_->error, conflict => $_->duplicate_id } + ); + } + elsif ( $_->isa('Koha::Exceptions::Object::FKConstraint') ) { + return $c->render( + status => 400, + openapi => { + error => "Given " + . $to_api_mapping->{ $_->broken_fk } + . " does not exist" + } + ); + } + elsif ( $_->isa('Koha::Exceptions::BadParameter') ) { + return $c->render( + status => 400, + openapi => { + error => "Given " + . $to_api_mapping->{ $_->parameter } + . " does not exist" + } + ); + } + } + + $c->unhandled_exception($_); + }; +} + +=head3 update + +Controller function that handles updating a Koha::ERM::EHolding object + +=cut + +sub update { + my $c = shift->openapi->valid_input or return; + + my $eholding_id = $c->validation->param('eholding_id'); + my $eholding = Koha::ERM::EHoldings->find( $eholding_id ); + + unless ($eholding) { + return $c->render( + status => 404, + openapi => { error => "eHolding not found" } + ); + } + + return try { + Koha::Database->new->schema->txn_do( + sub { + + my $body = $c->validation->param('body'); + + $eholding->set_from_api($body)->store; + + $c->res->headers->location($c->req->url->to_string . '/' . $eholding->eholding_id); + return $c->render( + status => 200, + openapi => $eholding->to_api + ); + } + ); + } + catch { + my $to_api_mapping = Koha::ERM::EHolding->new->to_api_mapping; + + if ( blessed $_ ) { + if ( $_->isa('Koha::Exceptions::Object::FKConstraint') ) { + return $c->render( + status => 400, + openapi => { + error => "Given " + . $to_api_mapping->{ $_->broken_fk } + . " does not exist" + } + ); + } + elsif ( $_->isa('Koha::Exceptions::BadParameter') ) { + return $c->render( + status => 400, + openapi => { + error => "Given " + . $to_api_mapping->{ $_->parameter } + . " does not exist" + } + ); + } + } + + $c->unhandled_exception($_); + }; +}; + +=head3 delete + +=cut + +sub delete { + my $c = shift->openapi->valid_input or return; + + my $eholding = Koha::ERM::EHoldings->find( $c->validation->param('eholding_id') ); + unless ($eholding) { + return $c->render( + status => 404, + openapi => { error => "eHolding not found" } + ); + } + + return try { + $eholding->delete; + return $c->render( + status => 204, + openapi => q{} + ); + } + catch { + $c->unhandled_exception($_); + }; +} + +1; -- 2.39.5