Bugzilla – Attachment 94511 Details for
Bug 23843
Make existing endpoints use Koha::Object(s)->to_api
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
[x]
|
Forgot Password
Login:
[x]
[patch]
Bug 23843: Add mapping to Koha::Patron
Bug-23843-Add-mapping-to-KohaPatron.patch (text/plain), 5.82 KB, created by
Martin Renvoize (ashimema)
on 2019-10-22 07:57:13 UTC
(
hide
)
Description:
Bug 23843: Add mapping to Koha::Patron
Filename:
MIME Type:
Creator:
Martin Renvoize (ashimema)
Created:
2019-10-22 07:57:13 UTC
Size:
5.82 KB
patch
obsolete
>From fe68660f61b8a809181146646a3af11a71b2cef0 Mon Sep 17 00:00:00 2001 >From: Tomas Cohen Arazi <tomascohen@theke.io> >Date: Mon, 21 Oct 2019 13:27:11 -0300 >Subject: [PATCH] Bug 23843: Add mapping to Koha::Patron > >This patch adds a to_api_mapping method to the class. This in effect >enables calling ->to_api on the object. The mapping is borrowed from the >API controller. It is not removed from the controller so we are able to >verify (through the tests) that there is no behavior change. >Once this is pushed we need to implement the counter-wise methods and >clean the controllers. >To test: >1. Run: > $ kshell > k$ prove t/db_dependent/api/v1/patrons.t >=> SUCCESS: Tests pass >2. Apply this patch >3. Repeat (1) >=> SUCCESS: Tests still pass! >4. Sign off :-D > >Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com> >--- > Koha/Patron.pm | 84 +++++++++++++++++++++++++++++++++++++++++ > Koha/REST/V1/Patrons.pm | 10 ++--- > 2 files changed, 88 insertions(+), 6 deletions(-) > >diff --git a/Koha/Patron.pm b/Koha/Patron.pm >index fca796ff28..2655f1c90b 100644 >--- a/Koha/Patron.pm >+++ b/Koha/Patron.pm >@@ -1476,6 +1476,90 @@ sub add_guarantor { > )->store(); > } > >+=head3 to_api >+ >+ my $json = $patron->to_api; >+ >+Overloaded method that returns a JSON representation of the Koha::Patron object, >+suitable for API output. >+ >+=cut >+ >+sub to_api { >+ my ( $self ) = @_; >+ >+ my $json_patron = $self->SUPER::to_api; >+ >+ $json_patron->{restricted} = ( $self->is_debarred ) >+ ? Mojo::JSON->true >+ : Mojo::JSON->false; >+ >+ return $json_patron; >+} >+ >+=head3 to_api_mapping >+ >+This method returns the mapping for representing a Koha::Patron object >+on the API. >+ >+=cut >+ >+sub to_api_mapping { >+ return { >+ borrowernotes => 'staff_notes', >+ borrowernumber => 'patron_id', >+ branchcode => 'library_id', >+ categorycode => 'category_id', >+ checkprevcheckout => 'check_previous_checkout', >+ contactfirstname => undef, # Unused >+ contactname => undef, # Unused >+ contactnote => 'altaddress_notes', >+ contacttitle => undef, # Unused >+ dateenrolled => 'date_enrolled', >+ dateexpiry => 'expiry_date', >+ dateofbirth => 'date_of_birth', >+ debarred => undef, # replaced by 'restricted' >+ debarredcomment => undef, # calculated, API consumers will use /restrictions instead >+ emailpro => 'secondary_email', >+ flags => undef, # permissions manipulation handled in /permissions >+ gonenoaddress => 'incorrect_address', >+ guarantorid => 'guarantor_id', >+ lastseen => 'last_seen', >+ lost => 'patron_card_lost', >+ opacnote => 'opac_notes', >+ othernames => 'other_name', >+ password => undef, # password manipulation handled in /password >+ phonepro => 'secondary_phone', >+ relationship => 'relationship_type', >+ sex => 'gender', >+ smsalertnumber => 'sms_number', >+ sort1 => 'statistics_1', >+ sort2 => 'statistics_2', >+ streetnumber => 'street_number', >+ streettype => 'street_type', >+ zipcode => 'postal_code', >+ B_address => 'altaddress_address', >+ B_address2 => 'altaddress_address2', >+ B_city => 'altaddress_city', >+ B_country => 'altaddress_country', >+ B_email => 'altaddress_email', >+ B_phone => 'altaddress_phone', >+ B_state => 'altaddress_state', >+ B_streetnumber => 'altaddress_street_number', >+ B_streettype => 'altaddress_street_type', >+ B_zipcode => 'altaddress_postal_code', >+ altcontactaddress1 => 'altcontact_address', >+ altcontactaddress2 => 'altcontact_address2', >+ altcontactaddress3 => 'altcontact_city', >+ altcontactcountry => 'altcontact_country', >+ altcontactfirstname => 'altcontact_firstname', >+ altcontactphone => 'altcontact_phone', >+ altcontactsurname => 'altcontact_surname', >+ altcontactstate => 'altcontact_state', >+ altcontactzipcode => 'altcontact_postal_code' >+ }; >+} >+ > =head2 Internal methods > > =head3 _type >diff --git a/Koha/REST/V1/Patrons.pm b/Koha/REST/V1/Patrons.pm >index 29331fc4d0..46ad80d01d 100644 >--- a/Koha/REST/V1/Patrons.pm >+++ b/Koha/REST/V1/Patrons.pm >@@ -73,9 +73,8 @@ sub list { > } > ); > } >- my @patrons = $patrons->as_list; >- @patrons = map { _to_api( $_->TO_JSON ) } @patrons; >- return $c->render( status => 200, openapi => \@patrons ); >+ >+ return $c->render( status => 200, openapi => $patrons->to_api ); > } > catch { > if ( $_->isa('DBIx::Class::Exception') ) { >@@ -110,7 +109,7 @@ sub get { > return $c->render( status => 404, openapi => { error => "Patron not found." } ); > } > >- return $c->render( status => 200, openapi => _to_api( $patron->TO_JSON ) ); >+ return $c->render( status => 200, openapi => $patron->to_api ); > } > > =head3 add >@@ -127,9 +126,8 @@ sub add { > my $body = _to_model( $c->validation->param('body') ); > > my $patron = Koha::Patron->new( _to_model($body) )->store; >- $patron = _to_api( $patron->TO_JSON ); > >- return $c->render( status => 201, openapi => $patron ); >+ return $c->render( status => 201, openapi => $patron->to_api ); > } > catch { > unless ( blessed $_ && $_->can('rethrow') ) { >-- >2.20.1
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
|
Splinter Review
Attachments on
bug 23843
:
94470
|
94471
|
94472
|
94473
|
94474
|
94475
|
94476
|
94477
|
94478
|
94496
|
94506
|
94507
|
94508
|
94509
|
94510
|
94511
|
94512
|
94513
|
94514
|
94587
|
94588
|
94589
|
94590
|
94591
|
94592
|
94593
|
94594
|
94595
|
94596
|
94619
|
94620
|
94621
|
94622
|
94623
|
94624
|
94625
|
94626
|
94627
|
94628