Bug 23843: Add mapping to Koha::Hold
authorTomas Cohen Arazi <tomascohen@theke.io>
Mon, 21 Oct 2019 16:26:07 +0000 (13:26 -0300)
committerMartin Renvoize <martin.renvoize@ptfs-europe.com>
Wed, 23 Oct 2019 17:03:07 +0000 (18:03 +0100)
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/holds.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>
Signed-off-by: Joonas Kylmälä <joonas.kylmala@helsinki.fi>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>

Koha/Hold.pm
Koha/REST/V1/Holds.pm

index 30188ae..05afea8 100644 (file)
@@ -400,7 +400,39 @@ sub _move_to_old {
     return Koha::Old::Hold->new( $hold_infos )->store;
 }
 
-=head3 type
+=head3 to_api_mapping
+
+This method returns the mapping for representing a Koha::Hold object
+on the API.
+
+=cut
+
+sub to_api_mapping {
+    return {
+        reserve_id       => 'hold_id',
+        borrowernumber   => 'patron_id',
+        reservedate      => 'hold_date',
+        biblionumber     => 'biblio_id',
+        branchcode       => 'pickup_library_id',
+        notificationdate => undef,
+        reminderdate     => undef,
+        cancellationdate => 'cancelation_date',
+        reservenotes     => 'notes',
+        found            => 'status',
+        itemnumber       => 'item_id',
+        waitingdate      => 'waiting_date',
+        expirationdate   => 'expiration_date',
+        lowestPriority   => 'lowest_priority',
+        suspend          => 'suspended',
+        suspend_until    => 'suspended_until',
+        itemtype         => 'item_type',
+        item_level_hold  => 'item_level',
+    };
+}
+
+=head2 Internal methods
+
+=head3 _type
 
 =cut
 
index 00faf2f..41e93fc 100644 (file)
@@ -184,7 +184,10 @@ sub add {
 
         my $hold = Koha::Holds->find($hold_id);
 
-        return $c->render( status => 201, openapi => _to_api($hold->TO_JSON) );
+        return $c->render(
+            status  => 201,
+            openapi => $hold->to_api
+        );
     }
     catch {
         if ( blessed $_ and $_->isa('Koha::Exceptions') ) {
@@ -258,7 +261,10 @@ sub edit {
     C4::Reserves::ModReserve($params);
     $hold->discard_changes; # refresh
 
-    return $c->render( status => 200, openapi => _to_api( $hold->TO_JSON ) );
+    return $c->render(
+        status  => 200,
+        openapi => $hold->to_api
+    );
 }
 
 =head3 delete