From 6c4fedc3de1aa507b6b21a04282b960fee96d7c2 Mon Sep 17 00:00:00 2001 From: Matt Blenkinsop Date: Thu, 27 Apr 2023 10:50:28 +0000 Subject: [PATCH] Bug 34587: API controllers and objects Signed-off-by: Jessica Zairo Signed-off-by: Michaela Sieber Signed-off-by: Nick Clemens Signed-off-by: Tomas Cohen Arazi --- Koha/ERM/CounterFile.pm | 56 +++++ Koha/ERM/CounterFiles.pm | 52 +++++ Koha/ERM/CounterLog.pm | 42 ++++ Koha/ERM/CounterLogs.pm | 52 +++++ Koha/ERM/MonthlyUsage.pm | 42 ++++ Koha/ERM/MonthlyUsages.pm | 52 +++++ Koha/ERM/UsageDataProvider.pm | 42 ++++ Koha/ERM/UsageDataProviders.pm | 52 +++++ Koha/ERM/UsageTitle.pm | 68 ++++++ Koha/ERM/UsageTitles.pm | 52 +++++ Koha/ERM/YearlyUsage.pm | 42 ++++ Koha/ERM/YearlyUsages.pm | 52 +++++ Koha/REST/V1/ERM/CounterFiles.pm | 107 +++++++++ Koha/REST/V1/ERM/UsageDataProviders.pm | 306 +++++++++++++++++++++++++ Koha/REST/V1/ERM/UsageTitles.pm | 52 +++++ 15 files changed, 1069 insertions(+) create mode 100644 Koha/ERM/CounterFile.pm create mode 100644 Koha/ERM/CounterFiles.pm create mode 100644 Koha/ERM/CounterLog.pm create mode 100644 Koha/ERM/CounterLogs.pm create mode 100644 Koha/ERM/MonthlyUsage.pm create mode 100644 Koha/ERM/MonthlyUsages.pm create mode 100644 Koha/ERM/UsageDataProvider.pm create mode 100644 Koha/ERM/UsageDataProviders.pm create mode 100644 Koha/ERM/UsageTitle.pm create mode 100644 Koha/ERM/UsageTitles.pm create mode 100644 Koha/ERM/YearlyUsage.pm create mode 100644 Koha/ERM/YearlyUsages.pm create mode 100644 Koha/REST/V1/ERM/CounterFiles.pm create mode 100644 Koha/REST/V1/ERM/UsageDataProviders.pm create mode 100644 Koha/REST/V1/ERM/UsageTitles.pm diff --git a/Koha/ERM/CounterFile.pm b/Koha/ERM/CounterFile.pm new file mode 100644 index 0000000000..b6efb017c8 --- /dev/null +++ b/Koha/ERM/CounterFile.pm @@ -0,0 +1,56 @@ +package Koha::ERM::CounterFile; + +# 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 base qw(Koha::Object); + +use Koha::ERM::CounterLogs; + +=head1 NAME + +Koha::ERM::CounterFile - Koha ErmCounterFile Object class + +=head1 API + +=head2 Class Methods + +=cut + +=head3 counter_logs + +Return the counter logs for this data provider + +=cut + +sub counter_logs { + my ( $self ) = @_; + my $counter_logs_rs = $self->_result->erm_counter_logs; + return Koha::ERM::CounterLogs->_new_from_dbic($counter_logs_rs); +} + +=head2 Internal methods + +=head3 _type + +=cut + +sub _type { + return 'ErmCounterFile'; +} + +1; diff --git a/Koha/ERM/CounterFiles.pm b/Koha/ERM/CounterFiles.pm new file mode 100644 index 0000000000..e234c627eb --- /dev/null +++ b/Koha/ERM/CounterFiles.pm @@ -0,0 +1,52 @@ +package Koha::ERM::CounterFiles; + +# 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::CounterFile; + +use base qw(Koha::Objects); + +=head1 NAME + +Koha::ERM::CounterFiles- Koha ErmCounterFile Object set class + +=head1 API + +=head2 Class Methods + +=cut + +=head3 type + +=cut + +sub _type { + return 'ErmCounterFile'; +} + +=head3 object_class + +=cut + +sub object_class { + return 'Koha::ERM::CounterFile'; +} + +1; diff --git a/Koha/ERM/CounterLog.pm b/Koha/ERM/CounterLog.pm new file mode 100644 index 0000000000..150fca5ec7 --- /dev/null +++ b/Koha/ERM/CounterLog.pm @@ -0,0 +1,42 @@ +package Koha::ERM::CounterLog; + +# 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 base qw(Koha::Object); + +=head1 NAME + +Koha::ERM::CounterLog - Koha ErmCounterLog Object class + +=head1 API + +=head2 Class Methods + +=cut + +=head2 Internal methods + +=head3 _type + +=cut + +sub _type { + return 'ErmCounterLog'; +} + +1; diff --git a/Koha/ERM/CounterLogs.pm b/Koha/ERM/CounterLogs.pm new file mode 100644 index 0000000000..a95d29991d --- /dev/null +++ b/Koha/ERM/CounterLogs.pm @@ -0,0 +1,52 @@ +package Koha::ERM::CounterLogs; + +# 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::CounterLog; + +use base qw(Koha::Objects); + +=head1 NAME + +Koha::ERM::CounterLogs- Koha ErmCounterLog Object set class + +=head1 API + +=head2 Class Methods + +=cut + +=head3 type + +=cut + +sub _type { + return 'ErmCounterLog'; +} + +=head3 object_class + +=cut + +sub object_class { + return 'Koha::ERM::CounterLog'; +} + +1; diff --git a/Koha/ERM/MonthlyUsage.pm b/Koha/ERM/MonthlyUsage.pm new file mode 100644 index 0000000000..b1ce0ada73 --- /dev/null +++ b/Koha/ERM/MonthlyUsage.pm @@ -0,0 +1,42 @@ +package Koha::ERM::MonthlyUsage; + +# 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 base qw(Koha::Object); + +=head1 NAME + +Koha::ERM::MonthlyUsage - Koha ErmMonthlyUsage Object class + +=head1 API + +=head2 Class Methods + +=cut + +=head2 Internal methods + +=head3 _type + +=cut + +sub _type { + return 'ErmUsageMus'; +} + +1; diff --git a/Koha/ERM/MonthlyUsages.pm b/Koha/ERM/MonthlyUsages.pm new file mode 100644 index 0000000000..ed62b97c77 --- /dev/null +++ b/Koha/ERM/MonthlyUsages.pm @@ -0,0 +1,52 @@ +package Koha::ERM::MonthlyUsages; + +# 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::MonthlyUsage; + +use base qw(Koha::Objects); + +=head1 NAME + +Koha::ERM::MonthlyUsages- Koha ErmMonthlyUsage Object set class + +=head1 API + +=head2 Class Methods + +=cut + +=head3 type + +=cut + +sub _type { + return 'ErmUsageMus'; +} + +=head3 object_class + +=cut + +sub object_class { + return 'Koha::ERM::MonthlyUsage'; +} + +1; diff --git a/Koha/ERM/UsageDataProvider.pm b/Koha/ERM/UsageDataProvider.pm new file mode 100644 index 0000000000..f9a1588d71 --- /dev/null +++ b/Koha/ERM/UsageDataProvider.pm @@ -0,0 +1,42 @@ +package Koha::ERM::UsageDataProvider; + +# 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 base qw(Koha::Object); + +=head1 NAME + +Koha::ERM::UsageDataProvider - Koha ErmUsageDataProvider Object class + +=head1 API + +=head2 Class Methods + +=cut + +=head2 Internal methods + +=head3 _type + +=cut + +sub _type { + return 'ErmUsageDataProvider'; +} + +1; diff --git a/Koha/ERM/UsageDataProviders.pm b/Koha/ERM/UsageDataProviders.pm new file mode 100644 index 0000000000..24986991d4 --- /dev/null +++ b/Koha/ERM/UsageDataProviders.pm @@ -0,0 +1,52 @@ +package Koha::ERM::UsageDataProviders; + +# 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::UsageDataProvider; + +use base qw(Koha::Objects); + +=head1 NAME + +Koha::ERM::UsageDataProviders- Koha ErmUsageDataProvider Object set class + +=head1 API + +=head2 Class Methods + +=cut + +=head3 type + +=cut + +sub _type { + return 'ErmUsageDataProvider'; +} + +=head3 object_class + +=cut + +sub object_class { + return 'Koha::ERM::UsageDataProvider'; +} + +1; diff --git a/Koha/ERM/UsageTitle.pm b/Koha/ERM/UsageTitle.pm new file mode 100644 index 0000000000..78ef910fd9 --- /dev/null +++ b/Koha/ERM/UsageTitle.pm @@ -0,0 +1,68 @@ +package Koha::ERM::UsageTitle; + +# 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 base qw(Koha::Object); + +use Koha::ERM::YearlyUsage; +use Koha::ERM::YearlyUsages; +use Koha::ERM::MonthlyUsage; +use Koha::ERM::MonthlyUsages; + +=head1 NAME + +Koha::ERM::UsageTitle - Koha ErmUsageTitle Object class + +=head1 API + +=head2 Class Methods +=head3 erm_usage_muses + +Method to embed erm_usage_muses to titles for report formatting + +=cut + +sub erm_usage_muses { + my ( $self ) = @_; + my $usage_mus_rs = $self->_result->erm_usage_muses; + return Koha::ERM::MonthlyUsages->_new_from_dbic($usage_mus_rs); +} + +=head3 erm_usage_yuses + +Method to embed erm_usage_yuses to titles for report formatting + +=cut + +sub erm_usage_yuses { + my ( $self ) = @_; + my $usage_yus_rs = $self->_result->erm_usage_yuses; + return Koha::ERM::YearlyUsages->_new_from_dbic($usage_yus_rs); +} + +=head2 Internal methods + +=head3 _type + +=cut + +sub _type { + return 'ErmUsageTitle'; +} + +1; diff --git a/Koha/ERM/UsageTitles.pm b/Koha/ERM/UsageTitles.pm new file mode 100644 index 0000000000..235e8e2cc2 --- /dev/null +++ b/Koha/ERM/UsageTitles.pm @@ -0,0 +1,52 @@ +package Koha::ERM::UsageTitles; + +# 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::UsageTitle; + +use base qw(Koha::Objects); + +=head1 NAME + +Koha::ERM::UsageTitles- Koha ErmUsageTitle Object set class + +=head1 API + +=head2 Class Methods + +=cut + +=head3 type + +=cut + +sub _type { + return 'ErmUsageTitle'; +} + +=head3 object_class + +=cut + +sub object_class { + return 'Koha::ERM::UsageTitle'; +} + +1; diff --git a/Koha/ERM/YearlyUsage.pm b/Koha/ERM/YearlyUsage.pm new file mode 100644 index 0000000000..86804b038f --- /dev/null +++ b/Koha/ERM/YearlyUsage.pm @@ -0,0 +1,42 @@ +package Koha::ERM::YearlyUsage; + +# 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 base qw(Koha::Object); + +=head1 NAME + +Koha::ERM::YearlyUsage - Koha ErmYearlyUsage Object class + +=head1 API + +=head2 Class Methods + +=cut + +=head2 Internal methods + +=head3 _type + +=cut + +sub _type { + return 'ErmUsageYus'; +} + +1; diff --git a/Koha/ERM/YearlyUsages.pm b/Koha/ERM/YearlyUsages.pm new file mode 100644 index 0000000000..502d96042e --- /dev/null +++ b/Koha/ERM/YearlyUsages.pm @@ -0,0 +1,52 @@ +package Koha::ERM::YearlyUsages; + +# 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::YearlyUsage; + +use base qw(Koha::Objects); + +=head1 NAME + +Koha::ERM::YearlyUsages- Koha ErmYearlyUsage Object set class + +=head1 API + +=head2 Class Methods + +=cut + +=head3 type + +=cut + +sub _type { + return 'ErmUsageYus'; +} + +=head3 object_class + +=cut + +sub object_class { + return 'Koha::ERM::YearlyUsage'; +} + +1; diff --git a/Koha/REST/V1/ERM/CounterFiles.pm b/Koha/REST/V1/ERM/CounterFiles.pm new file mode 100644 index 0000000000..d7e700dc95 --- /dev/null +++ b/Koha/REST/V1/ERM/CounterFiles.pm @@ -0,0 +1,107 @@ +package Koha::REST::V1::ERM::CounterFiles; + +# 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::CounterFiles; + +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 $counter_files_set = Koha::ERM::CounterFiles->new; + my $counter_files = $c->objects->search($counter_files_set); + return $c->render( status => 200, openapi => $counter_files ); + } + catch { + $c->unhandled_exception($_); + }; + +} + +=head3 get + +Controller function that handles retrieving a single Koha::ERM::CounterFile object + +=cut + +sub get { + my $c = shift->openapi->valid_input or return; + + return try { + my $counter_file_id = $c->validation->param('erm_counter_files_id'); + my $counter_file = $c->objects->find( Koha::ERM::CounterFiles->search, $counter_file_id ); + + unless ($counter_file) { + return $c->render( + status => 404, + openapi => { error => "Counter file not found" } + ); + } + + $c->render_file( + 'data' => $counter_file->file_content, + 'filename' => $counter_file->filename . '.csv' + ); + } + catch { + $c->unhandled_exception($_); + }; +} + +=head3 delete + +=cut + +sub delete { + my $c = shift->openapi->valid_input or return; + + my $counter_file_id = $c->validation->param('erm_counter_files_id'); + my $counter_file = Koha::ERM::CounterFiles->find($counter_file_id); + unless ($counter_file) { + return $c->render( + status => 404, + openapi => { error => "Counter file not found" } + ); + } + + return try { + $counter_file->delete; + return $c->render( + status => 204, + openapi => q{} + ); + } + catch { + $c->unhandled_exception($_); + }; +} + +1; diff --git a/Koha/REST/V1/ERM/UsageDataProviders.pm b/Koha/REST/V1/ERM/UsageDataProviders.pm new file mode 100644 index 0000000000..ea969e7946 --- /dev/null +++ b/Koha/REST/V1/ERM/UsageDataProviders.pm @@ -0,0 +1,306 @@ +package Koha::REST::V1::ERM::UsageDataProviders; + +# 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::UsageDataProviders; + +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 $usage_data_providers_set = Koha::ERM::UsageDataProviders->new; + my $usage_data_providers = + $c->objects->search($usage_data_providers_set); + if ( $c->validation->output->{"x-koha-embed"}[0] + && $c->validation->output->{"x-koha-embed"}[0] eq 'counter_files' ) + { + foreach my $provider (@$usage_data_providers) { + my $title_dates = _get_earliest_and_latest_dates( 'TR', + $provider->{erm_usage_data_provider_id} ); + $provider->{earliest_title} = + $title_dates->{earliest_date} + ? $title_dates->{earliest_date} + : ''; + $provider->{latest_title} = + $title_dates->{latest_date} + ? $title_dates->{latest_date} + : ''; + + my $platform_dates = _get_earliest_and_latest_dates( 'PR', + $provider->{erm_usage_data_provider_id} ); + $provider->{earliest_platform} = + $platform_dates->{earliest_date} + ? $platform_dates->{earliest_date} + : ''; + $provider->{latest_platform} = + $platform_dates->{latest_date} + ? $platform_dates->{latest_date} + : ''; + + my $item_dates = _get_earliest_and_latest_dates( 'IR', + $provider->{erm_usage_data_provider_id} ); + $provider->{earliest_item} = + $item_dates->{earliest_date} + ? $item_dates->{earliest_date} + : ''; + $provider->{latest_item} = + $item_dates->{latest_date} ? $item_dates->{latest_date} : ''; + + my $database_dates = _get_earliest_and_latest_dates( 'DR', + $provider->{erm_usage_data_provider_id} ); + $provider->{earliest_database} = + $database_dates->{earliest_date} + ? $database_dates->{earliest_date} + : ''; + $provider->{latest_database} = + $database_dates->{latest_date} + ? $database_dates->{latest_date} + : ''; + } + } + + return $c->render( status => 200, openapi => $usage_data_providers ); + } + catch { + $c->unhandled_exception($_); + }; + +} + +=head3 get + +Controller function that handles retrieving a single Koha::ERM::UsageDataProvider object + +=cut + +sub get { + my $c = shift->openapi->valid_input or return; + + return try { + my $usage_data_provider_id = + $c->validation->param('erm_usage_data_provider_id'); + my $usage_data_provider = + $c->objects->find( Koha::ERM::UsageDataProviders->search, + $usage_data_provider_id ); + + unless ($usage_data_provider) { + return $c->render( + status => 404, + openapi => { error => "Usage data provider not found" } + ); + } + + return $c->render( + status => 200, + openapi => $usage_data_provider + ); + } + catch { + $c->unhandled_exception($_); + }; +} + +=head3 add + +Controller function that handles adding a new Koha::ERM::UsageDataProvider 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 $usage_data_provider = + Koha::ERM::UsageDataProvider->new_from_api($body)->store; + + $c->res->headers->location( $c->req->url->to_string . '/' + . $usage_data_provider->erm_usage_data_provider_id ); + return $c->render( + status => 201, + openapi => $usage_data_provider->to_api + ); + } + ); + } + catch { + + my $to_api_mapping = Koha::ERM::UsageDataProvider->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" + } + ); + } + elsif ( $_->isa('Koha::Exceptions::PayloadTooLarge') ) { + return $c->render( + status => 413, + openapi => { error => $_->error } + ); + } + } + + $c->unhandled_exception($_); + }; +} + +=head3 update + +Controller function that handles updating a Koha::ERM::UsageDataProvider object + +=cut + +sub update { + my $c = shift->openapi->valid_input or return; + + my $usage_data_provider_id = + $c->validation->param('erm_usage_data_provider_id'); + my $usage_data_provider = + Koha::ERM::UsageDataProviders->find($usage_data_provider_id); + + unless ($usage_data_provider) { + return $c->render( + status => 404, + openapi => { error => "Usage data provider not found" } + ); + } + + return try { + Koha::Database->new->schema->txn_do( + sub { + + my $body = $c->validation->param('body'); + + $usage_data_provider->set_from_api($body)->store; + + $c->res->headers->location( $c->req->url->to_string . '/' + . $usage_data_provider->erm_usage_data_provider_id ); + return $c->render( + status => 200, + openapi => $usage_data_provider->to_api + ); + } + ); + } + catch { + my $to_api_mapping = Koha::ERM::UsageDataProvider->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" + } + ); + } + elsif ( $_->isa('Koha::Exceptions::PayloadTooLarge') ) { + return $c->render( + status => 413, + openapi => { error => $_->error } + ); + } + } + + $c->unhandled_exception($_); + }; +} + +=head3 delete + +=cut + +sub delete { + my $c = shift->openapi->valid_input or return; + + my $usage_data_provider_id = + $c->validation->param('erm_usage_data_provider_id'); + my $usage_data_provider = + Koha::ERM::UsageDataProviders->find($usage_data_provider_id); + unless ($usage_data_provider) { + return $c->render( + status => 404, + openapi => { error => "Usage data provider not found" } + ); + } + + return try { + $usage_data_provider->delete; + return $c->render( + status => 204, + openapi => q{} + ); + } + catch { + $c->unhandled_exception($_); + }; +} + +1; diff --git a/Koha/REST/V1/ERM/UsageTitles.pm b/Koha/REST/V1/ERM/UsageTitles.pm new file mode 100644 index 0000000000..b246364364 --- /dev/null +++ b/Koha/REST/V1/ERM/UsageTitles.pm @@ -0,0 +1,52 @@ +package Koha::REST::V1::ERM::UsageTitles; + +# 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::UsageTitles; +use Koha::ERM::UsageDataProvider; +use Koha::ERM::UsageDataProviders; + +use Clone qw( clone ); +use Scalar::Util qw( blessed ); +use Try::Tiny qw( catch try ); +use JSON; + +=head1 API + +=head2 Methods + +=head3 list + +=cut + +sub list { + my $c = shift->openapi->valid_input or return; + + return try { + my $usage_titles_set = Koha::ERM::UsageTitles->new; + my $usage_titles = $c->objects->search($usage_titles_set); + return $c->render( status => 200, openapi => $usage_titles ); + } + catch { + $c->unhandled_exception($_); + }; +} + +1; -- 2.20.1