From 5bed71e0b24e4d118917dd2dc9dfef96b057e73a Mon Sep 17 00:00:00 2001 From: Paul Derscheid Date: Thu, 10 Apr 2025 15:17:18 +0000 Subject: [PATCH] Bug 37829: Module, Controller, DBIx::Class changes --- Koha/AdditionalField.pm | 1 + Koha/Booking.pm | 2 +- Koha/Bookings.pm | 2 +- Koha/REST/V1/Bookings.pm | 22 +++++++++++++++------ Koha/REST/V1/ExtendedAttributeTypes.pm | 1 + Koha/Schema/Result/Booking.pm | 27 ++++++++++++++++++++++++++ 6 files changed, 47 insertions(+), 8 deletions(-) diff --git a/Koha/AdditionalField.pm b/Koha/AdditionalField.pm index 160d248322..c2abfa4a14 100644 --- a/Koha/AdditionalField.pm +++ b/Koha/AdditionalField.pm @@ -62,6 +62,7 @@ sub to_api { 'erm_licenses' => 'license', 'erm_agreements' => 'agreement', 'erm_packages' => 'package', + 'bookings' => 'booking', 'aqorders' => 'order', }; diff --git a/Koha/Booking.pm b/Koha/Booking.pm index ab5a418047..33ea5d7106 100644 --- a/Koha/Booking.pm +++ b/Koha/Booking.pm @@ -29,7 +29,7 @@ use C4::Letters; use List::Util qw(any); -use base qw(Koha::Object); +use base qw( Koha::Object Koha::Object::Mixin::AdditionalFields ); =head1 NAME diff --git a/Koha/Bookings.pm b/Koha/Bookings.pm index ca89e8eacb..8d80bcf8cc 100644 --- a/Koha/Bookings.pm +++ b/Koha/Bookings.pm @@ -22,7 +22,7 @@ use Modern::Perl; use Koha::Database; use Koha::Booking; -use base qw(Koha::Objects); +use base qw( Koha::Objects::Mixin::AdditionalFields Koha::Objects ); =head1 NAME diff --git a/Koha/REST/V1/Bookings.pm b/Koha/REST/V1/Bookings.pm index 096c58961b..1ee0655326 100644 --- a/Koha/REST/V1/Bookings.pm +++ b/Koha/REST/V1/Bookings.pm @@ -75,9 +75,14 @@ sub add { my $c = shift->openapi->valid_input or return; return try { - my $booking = Koha::Booking->new_from_api( $c->req->json ); - $booking->store; - $booking->discard_changes; + my $body = $c->req->json; + my $extended_attributes = delete $body->{extended_attributes} // []; + + my $booking = Koha::Booking->new_from_api($body)->store; + + my @extended_attributes = map { { 'id' => $_->{field_id}, 'value' => $_->{value} } } @{$extended_attributes}; + $booking->extended_attributes( \@extended_attributes ); + $c->res->headers->location( $c->req->url->to_string . '/' . $booking->booking_id ); return $c->render( status => 201, @@ -117,9 +122,14 @@ sub update { unless $booking; return try { - $booking->set_from_api( $c->req->json ); - $booking->store(); - $booking->discard_changes; + my $body = $c->req->json; + my $extended_attributes = delete $body->{extended_attributes} // []; + + $booking->set_from_api($body)->store; + + my @extended_attributes = map { { 'id' => $_->{field_id}, 'value' => $_->{value} } } @{$extended_attributes}; + $booking->extended_attributes( \@extended_attributes ); + return $c->render( status => 200, openapi => $c->objects->to_api($booking) ); } catch { $c->unhandled_exception($_); diff --git a/Koha/REST/V1/ExtendedAttributeTypes.pm b/Koha/REST/V1/ExtendedAttributeTypes.pm index 844582c5a4..4c77d90f83 100644 --- a/Koha/REST/V1/ExtendedAttributeTypes.pm +++ b/Koha/REST/V1/ExtendedAttributeTypes.pm @@ -53,6 +53,7 @@ sub list { license => 'erm_licenses', agreement => 'erm_agreements', package => 'erm_packages', + booking => 'bookings', order => 'aqorders', }; diff --git a/Koha/Schema/Result/Booking.pm b/Koha/Schema/Result/Booking.pm index 2231dbf19a..78f39cbaf0 100644 --- a/Koha/Schema/Result/Booking.pm +++ b/Koha/Schema/Result/Booking.pm @@ -260,6 +260,33 @@ __PACKAGE__->belongs_to( # Created by DBIx::Class::Schema::Loader v0.07051 @ 2024-10-24 16:23:05 # DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:kTR2kwiwY2PnjU1E0P+CMQ +__PACKAGE__->has_many( + "additional_field_values", + "Koha::Schema::Result::AdditionalFieldValue", + sub { + my ($args) = @_; + return { + "$args->{foreign_alias}.record_id" => { -ident => "$args->{self_alias}.booking_id" }, + "$args->{foreign_alias}.field_id" => + { -in => \'(SELECT id FROM additional_fields WHERE tablename="bookings")' }, + }; + }, + { 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}.booking_id" }, + "$args->{foreign_alias}.field_id" => + { -in => \'(SELECT id FROM additional_fields WHERE tablename="bookings")' }, + }; + }, + { cascade_copy => 0, cascade_delete => 0 }, +); # You can replace this text with custom code or comments, and it will be preserved on regeneration 1; -- 2.39.5