From 57c4e31fe042bd1f0196e975c65438e18f92d7b4 Mon Sep 17 00:00:00 2001 From: Arthur Suzuki Date: Fri, 12 Jul 2019 18:05:45 +0200 Subject: [PATCH] Bug 17314 : Added _to_model and _to_api methods --- Koha/REST/V1/Suggestions.pm | 135 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 134 insertions(+), 1 deletion(-) diff --git a/Koha/REST/V1/Suggestions.pm b/Koha/REST/V1/Suggestions.pm index 7b5aed4780..f23e591c2c 100644 --- a/Koha/REST/V1/Suggestions.pm +++ b/Koha/REST/V1/Suggestions.pm @@ -58,7 +58,7 @@ sub list { } return try { - $suggestions = Koha::Suggestions->search($filter)->unblessed; + $suggestions = Koha::Suggestions->search($filter, \&_to_model, \&_to_api); return $c->render( status => 200, openapi => $suggestions ); } catch { @@ -297,5 +297,138 @@ sub _validate_body { # Everything's fine .. return 0; } +=head3 _to_api +Helper function that maps unblessed Koha::Suggestion objects into REST api +attribute names. + +=cut + +sub _to_api { + my $suggestion = shift; + + # Rename attributes + foreach my $column ( keys %{ $Koha::REST::V1::Suggestions::to_api_mapping } ) { + my $mapped_column = $Koha::REST::V1::Suggestions::to_api_mapping->{$column}; + if ( exists $suggestion->{ $column } + && defined $mapped_column ) + { + # key != undef + $suggestion->{ $mapped_column } = delete $suggestion->{ $column }; + } + elsif ( exists $suggestion->{ $column } + && !defined $mapped_column ) + { + # key == undef + delete $suggestion->{ $column }; + } + } + + return $suggestion; +} + +=head3 _to_model + +Helper function that maps REST api objects into Koha::Suggestion +attribute names. + +=cut + +sub _to_model { + my $suggestion = shift; + + foreach my $attribute ( keys %{ $Koha::REST::V1::Suggestions::to_model_mapping } ) { + my $mapped_attribute = $Koha::REST::V1::Suggestions::to_model_mapping->{$attribute}; + if ( exists $suggestion->{ $attribute } + && defined $mapped_attribute ) + { + # key => !undef + $suggestion->{ $mapped_attribute } = delete $suggestion->{ $attribute }; + } + elsif ( exists $suggestion->{ $attribute } + && !defined $mapped_attribute ) + { + # key => undef / to be deleted + delete $suggestion->{ $attribute }; + } + } + + return $suggestion; +} + +=head2 Global variables + +=head3 $to_api_mapping + +=cut + +our $to_api_mapping = { + suggestionid => 'suggestion_id', + suggestedby => 'suggested_by', + suggesteddate => 'suggestion_date', + managedby => 'managed_by', + manageddate => 'managed_date', + accepteddate => 'accepted_date', + rejectedby => 'rejected_by', + rejectiondate => 'rejected_date', + STATUS => 'status', + note => 'note', + author => 'author', + title => 'title', + copyrightdate => 'copyright_date', + publishercode => 'publisher_code', + date => 'date', + volumedesc => 'volume_desc', + publicationyear => 'publication_year', + place => 'location', + isbn => 'isbn', + biblionumber => 'biblio_id', + reason => 'reason', + patronreason => 'patron_reason', + budgetid => 'budget_id', + branchcode => 'library_id', + collectiontitle => 'collection_title', + itemtype => 'item_type', + quantity => 'quantity', + currency => 'currency', + price => 'item_price', + total => 'total_price' +}; + +=head3 $to_model_mapping + +=cut + +our $to_model_mapping = { + suggestion_id => 'suggestionid', + suggested_by => 'suggestedby', + suggestion_date => 'suggesteddate', + managed_by => 'managedby', + managed_date => 'manageddate', + accepted_date => 'accepteddate', + rejected_by => 'rejectedby', + rejected_date => 'rejectiondate', + status => 'STATUS', + note => 'note', + author => 'author', + title => 'title', + copyright_date => 'copyrightdate', + publisher_code => 'publishercode', + date => 'date', + volume_desc => 'volumedesc', + publication_year => 'publicationyear', + location => 'place', + isbn => 'isbn', + biblio_id => 'biblionumber', + reason => 'reason', + patron_reason => 'patronreason', + budget_id => 'budgetid', + library_id => 'branchcode', + collection_title => 'collectiontitle', + item_type => 'itemtype', + quantity => 'quantity', + currency => 'currency', + item_price => 'price', + total_price => 'total' +}; 1; -- 2.11.0