From faa90b8aa7de21e65e25e27a06a379e98db25606 Mon Sep 17 00:00:00 2001 From: Matt Blenkinsop Date: Tue, 29 Oct 2024 17:12:09 +0000 Subject: [PATCH] Bug 38262: Update object classes Signed-off-by: David Nind --- Koha/Acquisition/Bookseller.pm | 2 +- Koha/Acquisition/Booksellers.pm | 2 +- Koha/AdditionalField.pm | 17 +++++++------- Koha/REST/V1/Acquisitions/Vendors.pm | 23 ++++++++++++++---- Koha/REST/V1/ExtendedAttributeTypes.pm | 1 + Koha/Schema/Result/Aqbookseller.pm | 32 ++++++++++++++++++++++++++ 6 files changed, 63 insertions(+), 14 deletions(-) diff --git a/Koha/Acquisition/Bookseller.pm b/Koha/Acquisition/Bookseller.pm index b845aba737..ad2b5d8cec 100644 --- a/Koha/Acquisition/Bookseller.pm +++ b/Koha/Acquisition/Bookseller.pm @@ -25,7 +25,7 @@ use Koha::Subscriptions; use C4::Contract qw( GetContracts ); -use base qw( Koha::Object ); +use base qw( Koha::Object::Mixin::AdditionalFields Koha::Object ); =head1 NAME diff --git a/Koha/Acquisition/Booksellers.pm b/Koha/Acquisition/Booksellers.pm index 1484396256..466270751d 100644 --- a/Koha/Acquisition/Booksellers.pm +++ b/Koha/Acquisition/Booksellers.pm @@ -19,7 +19,7 @@ use Modern::Perl; use Koha::Acquisition::Bookseller; -use base qw( Koha::Objects ); +use base qw( Koha::Object::Mixin::AdditionalFields Koha::Objects ); =head1 NAME diff --git a/Koha/AdditionalField.pm b/Koha/AdditionalField.pm index 160d248322..6b656087f3 100644 --- a/Koha/AdditionalField.pm +++ b/Koha/AdditionalField.pm @@ -55,14 +55,15 @@ sub to_api { my ( $self, $params ) = @_; my $table_to_resource = { - 'accountlines:credit' => 'credit', - 'accountlines:debit' => 'debit', - 'aqbasket' => 'basket', - 'aqinvoices' => 'invoice', - 'erm_licenses' => 'license', - 'erm_agreements' => 'agreement', - 'erm_packages' => 'package', - 'aqorders' => 'order', + 'accountlines:credit' => 'credit', + 'accountlines:debit' => 'debit', + 'aqbasket' => 'basket', + 'aqinvoices' => 'invoice', + 'erm_licenses' => 'license', + 'erm_agreements' => 'agreement', + 'erm_packages' => 'package', + 'aqorders' => 'order', + 'aqbooksellers:vendor' => 'vendor', }; my $json = $self->SUPER::to_api($params); diff --git a/Koha/REST/V1/Acquisitions/Vendors.pm b/Koha/REST/V1/Acquisitions/Vendors.pm index d17f6e1952..a656683b81 100644 --- a/Koha/REST/V1/Acquisitions/Vendors.pm +++ b/Koha/REST/V1/Acquisitions/Vendors.pm @@ -99,10 +99,11 @@ Controller function that handles adding a new Koha::Acquisition::Bookseller obje sub add { my $c = shift->openapi->valid_input or return; - my $vendor = $c->req->json; - my $contacts = delete $vendor->{contacts}; - my $interfaces = delete $vendor->{interfaces}; - my $aliases = delete $vendor->{aliases}; + my $vendor = $c->req->json; + my $contacts = delete $vendor->{contacts}; + my $interfaces = delete $vendor->{interfaces}; + my $aliases = delete $vendor->{aliases}; + my $extended_attributes = delete $vendor->{extended_attributes}; my $vendor_to_store = Koha::Acquisition::Bookseller->new_from_api( $c->req->json ); @@ -113,6 +114,12 @@ sub add { $vendor_to_store->aliases( $aliases || [] ); $vendor_to_store->interfaces( $interfaces || [] ); + if ( scalar(@$extended_attributes) > 0 ) { + my @extended_attributes = + map { { 'id' => $_->{field_id}, 'value' => $_->{value} } } @{$extended_attributes}; + $vendor_to_store->extended_attributes( \@extended_attributes ); + } + $c->res->headers->location( $c->req->url->to_string . '/' . $vendor_to_store->id ); return $c->render( status => 201, @@ -143,6 +150,8 @@ sub update { my $contacts = exists $vendor_update->{contacts} ? delete $vendor_update->{contacts} : undef; my $interfaces = exists $vendor_update->{interfaces} ? delete $vendor_update->{interfaces} : undef; my $aliases = exists $vendor_update->{aliases} ? delete $vendor_update->{aliases} : undef; + my $extended_attributes = + exists $vendor_update->{extended_attributes} ? delete $vendor_update->{extended_attributes} : undef; $vendor->set_from_api($vendor_update); $vendor->store(); @@ -151,6 +160,12 @@ sub update { $vendor->aliases( $aliases || [] ) if defined $aliases; $vendor->interfaces( $interfaces || [] ) if defined $interfaces; + if ( scalar(@$extended_attributes) > 0 ) { + my @extended_attributes = + map { { 'id' => $_->{field_id}, 'value' => $_->{value} } } @{$extended_attributes}; + $vendor->extended_attributes( \@extended_attributes ); + } + return $c->render( status => 200, openapi => $c->objects->to_api($vendor), diff --git a/Koha/REST/V1/ExtendedAttributeTypes.pm b/Koha/REST/V1/ExtendedAttributeTypes.pm index 844582c5a4..97d2cb916a 100644 --- a/Koha/REST/V1/ExtendedAttributeTypes.pm +++ b/Koha/REST/V1/ExtendedAttributeTypes.pm @@ -54,6 +54,7 @@ sub list { agreement => 'erm_agreements', package => 'erm_packages', order => 'aqorders', + vendor => 'aqbooksellers:vendor', }; return try { diff --git a/Koha/Schema/Result/Aqbookseller.pm b/Koha/Schema/Result/Aqbookseller.pm index 89dd569f23..8399f438ef 100644 --- a/Koha/Schema/Result/Aqbookseller.pm +++ b/Koha/Schema/Result/Aqbookseller.pm @@ -565,6 +565,38 @@ __PACKAGE__->has_many( { cascade_copy => 0, cascade_delete => 0 }, ); +__PACKAGE__->has_many( + "additional_field_values", + "Koha::Schema::Result::AdditionalFieldValue", + sub { + my ($args) = @_; + + return { + "$args->{foreign_alias}.record_id" => { -ident => "$args->{self_alias}.id" }, + + "$args->{foreign_alias}.field_id" => + { -in => \'(SELECT id FROM additional_fields WHERE tablename LIKE "aqbooksellers:%")' }, + }; + }, + { cascade_copy => 0, cascade_delete => 0 }, +); + +__PACKAGE__->has_many( + "extended_attributes", + "Koha::Schema::Result::AdditionalFieldValue", + sub { + my ($args) = @_; + + return { + "$args->{foreign_alias}.record_id" => { -ident => "$args->{self_alias}.id" }, + + "$args->{foreign_alias}.field_id" => + { -in => \'(SELECT id FROM additional_fields WHERE tablename LIKE "aqbooksellers:%")' }, + }; + }, + { cascade_copy => 0, cascade_delete => 0 }, +); + =head2 koha_object_class Missing POD for koha_object_class. -- 2.39.5