From 787f2c499e242ef642f70a0a6f6f6c9f198f61d8 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 and changed attirbutes naming --- Koha/REST/V1/Suggestions.pm | 135 ++++++++++++++++++++++++++++- api/v1/swagger/definitions/suggestion.json | 46 +++++----- 2 files changed, 157 insertions(+), 24 deletions(-) diff --git a/Koha/REST/V1/Suggestions.pm b/Koha/REST/V1/Suggestions.pm index c5e3c4d8e9..64f7d54de9 100644 --- a/Koha/REST/V1/Suggestions.pm +++ b/Koha/REST/V1/Suggestions.pm @@ -44,7 +44,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 { @@ -253,5 +253,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_created', + volumedesc => 'volume_desc', + publicationyear => 'publication_year', + place => 'publication_place', + 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_created => 'date', + volume_desc => 'volumedesc', + publication_year => 'publicationyear', + publication_place => '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; diff --git a/api/v1/swagger/definitions/suggestion.json b/api/v1/swagger/definitions/suggestion.json index 2e4af794a8..57dcaac1cc 100644 --- a/api/v1/swagger/definitions/suggestion.json +++ b/api/v1/swagger/definitions/suggestion.json @@ -1,43 +1,43 @@ { "type": "object", "properties": { - "suggestionid": { + "suggestion_id": { "type": "string", "description": "unique identifier assigned automatically by Koha" }, - "suggestedby": { + "suggested_by": { "type": ["string", "null"], "description": "patron_id for the person making the suggestion, foreign key linking to the borrowers table" }, - "suggesteddate": { + "suggested_date": { "type": ["string", "null"], "description": "the suggestion was submitted" }, - "managedby": { + "managed_by": { "type": ["string", "null"], "description": "patron_id for the librarian managing the suggestion, foreign key linking to the borrowers table" }, - "manageddate": { + "managed_date": { "type": ["string", "null"], "description": "date the suggestion was updated" }, - "acceptedby": { + "accepted_by": { "type": ["string", "null"], "description": "patron_id for the librarian who accepted the suggestion, foreign key linking to the borrowers table" }, - "accepteddate": { + "accepted_date": { "type": ["string", "null"], "description": "date the suggestion was marked as accepted" }, - "rejectedby": { + "rejected_by": { "type": ["string", "null"], "description": "patron_id for the librarian who rejected the suggestion, foreign key linking to the borrowers table" }, - "rejecteddate": { + "rejected_date": { "type": ["string", "null"], "description": "date the suggestion was marked as rejected" }, - "STATUS": { + "status": { "type": "string", "description": "suggestion status (ASKED, CHECKED, ACCEPTED, or REJECTED)" }, @@ -53,27 +53,27 @@ "type": ["string", "null"], "description": "title of the suggested item" }, - "copyrightdate": { + "copyright_date": { "type": ["string", "null"], "description": "copyright date of the suggested item" }, - "publishercode": { + "publisher_code": { "type": ["string", "null"], "description": "publisher of the suggested item" }, - "date": { + "date_created": { "type": ["string", "null"], "description": "date created" }, - "volumedesc": { + "volume_desc": { "type": ["string", "null"], "description": "volume description" }, - "publicationyear": { + "publication_year": { "type": ["string", "null"], "description": "year of publication" }, - "place": { + "publication_place": { "type": ["string", "null"], "description": "publication place of the suggested item" }, @@ -81,7 +81,7 @@ "type": ["string", "null"], "description": "isbn of the suggested item" }, - "biblionumber": { + "biblio_id": { "type": ["string", "null"], "description": "foreign key linking the suggestion to the biblio table after the suggestion has been ordered" }, @@ -89,23 +89,23 @@ "type": ["string", "null"], "description": "reason for accepting or rejecting the suggestion" }, - "patronreason": { + "patron_reason": { "type": ["string", "null"], "description": "reason for making the suggestion" }, - "budgetid": { + "budget_id": { "type": ["string", "null"], "description": "foreign key linking the suggested budget to the aqbudgets table" }, - "branchcode": { + "library_id": { "type": ["string", "null"], "description": "foreign key linking the suggested branch to the branches table" }, - "collectiontitle": { + "collection_title": { "type": ["string", "null"], "description": "collection name for the suggested item" }, - "itemtype": { + "item_type": { "type": ["string", "null"], "description": "suggested item type" }, @@ -117,7 +117,7 @@ "type": ["string", "null"], "description": "suggested currency for the suggested price" }, - "price": { + "item_price": { "type": ["string", "null"], "description": "suggested price" }, -- 2.11.0