From d9ebb06539dbbd07c34ca78fbf37add62e21818d Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Fri, 20 May 2022 11:27:25 +0200 Subject: [PATCH] Bug 32030: ERM - Package - 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/Package.pm | 44 +++++++ Koha/ERM/Packages.pm | 49 ++++++++ Koha/REST/V1/ERM/Packages.pm | 233 +++++++++++++++++++++++++++++++++++ 3 files changed, 326 insertions(+) create mode 100644 Koha/ERM/Package.pm create mode 100644 Koha/ERM/Packages.pm create mode 100644 Koha/REST/V1/ERM/Packages.pm diff --git a/Koha/ERM/Package.pm b/Koha/ERM/Package.pm new file mode 100644 index 0000000000..359e0906d3 --- /dev/null +++ b/Koha/ERM/Package.pm @@ -0,0 +1,44 @@ +package Koha::ERM::Package; + +# 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); + +=head1 NAME + +Koha::ERM::Package - Koha ERM Package Object class + +=head1 API + +=head2 Class Methods + +=cut + +=head2 Internal methods + +=head3 _type + +=cut + +sub _type { + return 'ErmPackage'; +} + +1; diff --git a/Koha/ERM/Packages.pm b/Koha/ERM/Packages.pm new file mode 100644 index 0000000000..07dd4c303f --- /dev/null +++ b/Koha/ERM/Packages.pm @@ -0,0 +1,49 @@ +package Koha::ERM::Packages; + +# 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::Package; + +use base qw(Koha::Objects); + +=head1 NAME + +Koha::ERM::Packages - Koha ERM Package Object set class + +=head1 API + +=head2 Class Methods + +=cut + +=head3 type + +=cut + +sub _type { + return 'ErmPackage'; +} + +sub object_class { + return 'Koha::ERM::Package'; +} + +1; diff --git a/Koha/REST/V1/ERM/Packages.pm b/Koha/REST/V1/ERM/Packages.pm new file mode 100644 index 0000000000..3e16a92ca8 --- /dev/null +++ b/Koha/REST/V1/ERM/Packages.pm @@ -0,0 +1,233 @@ +package Koha::REST::V1::ERM::Packages; + +# 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::Packages; + +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 $packages_set = Koha::ERM::Packages->new; + my $packages = $c->objects->search( $packages_set ); + return $c->render( status => 200, openapi => $packages ); + } + catch { + $c->unhandled_exception($_); + }; + +} + +=head3 get + +Controller function that handles retrieving a single Koha::ERM::Package object + +=cut + +sub get { + my $c = shift->openapi->valid_input or return; + + return try { + my $package_id = $c->validation->param('package_id'); + my $package = $c->objects->find( Koha::ERM::Packages->search, $package_id ); + + unless ($package) { + return $c->render( + status => 404, + openapi => { error => "Package not found" } + ); + } + + return $c->render( + status => 200, + openapi => $package + ); + } + catch { + $c->unhandled_exception($_); + }; +} + +=head3 add + +Controller function that handles adding a new Koha::ERM::Package 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 $package = Koha::ERM::Package->new_from_api($body)->store; + + $c->res->headers->location($c->req->url->to_string . '/' . $package->package_id); + return $c->render( + status => 201, + openapi => $package->to_api + ); + } + ); + } + catch { + + my $to_api_mapping = Koha::ERM::Package->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::Package object + +=cut + +sub update { + my $c = shift->openapi->valid_input or return; + + my $package_id = $c->validation->param('package_id'); + my $package = Koha::ERM::Packages->find( $package_id ); + + unless ($package) { + return $c->render( + status => 404, + openapi => { error => "Package not found" } + ); + } + + return try { + Koha::Database->new->schema->txn_do( + sub { + + my $body = $c->validation->param('body'); + + $package->set_from_api($body)->store; + + $c->res->headers->location($c->req->url->to_string . '/' . $package->package_id); + return $c->render( + status => 200, + openapi => $package->to_api + ); + } + ); + } + catch { + my $to_api_mapping = Koha::ERM::Package->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 $package = Koha::ERM::Packages->find( $c->validation->param('package_id') ); + unless ($package) { + return $c->render( + status => 404, + openapi => { error => "Package not found" } + ); + } + + return try { + $package->delete; + return $c->render( + status => 204, + openapi => q{} + ); + } + catch { + $c->unhandled_exception($_); + }; +} + +1; -- 2.39.5