Bug 24545: Fix license statements
[srvgit] / Koha / REST / V1 / Holds.pm
index 04d1014..047a69b 100644 (file)
@@ -2,18 +2,18 @@ package Koha::REST::V1::Holds;
 
 # This file is part of Koha.
 #
-# Koha is free software; you can redistribute it and/or modify it under the
-# terms of the GNU General Public License as published by the Free Software
-# Foundation; either version 3 of the License, or (at your option) any later
-# version.
+# Koha is free software; you can redistribute it and/or modify it
+# under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
 #
-# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
-# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
-# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
+# Koha is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
 #
-# You should have received a copy of the GNU General Public License along
-# with Koha; if not, write to the Free Software Foundation, Inc.,
-# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+# You should have received a copy of the GNU General Public License
+# along with Koha; if not, see <http://www.gnu.org/licenses>.
 
 use Modern::Perl;
 
@@ -31,7 +31,7 @@ use Try::Tiny;
 
 =head1 API
 
-=head2 Class methods
+=head2 Methods
 
 =head3 list
 
@@ -44,7 +44,7 @@ sub list {
 
     return try {
         my $holds_set = Koha::Holds->new;
-        my $holds     = $c->objects->search( $holds_set, \&_to_model, \&_to_api );
+        my $holds     = $c->objects->search( $holds_set );
         return $c->render( status => 200, openapi => $holds );
     }
     catch {
@@ -84,7 +84,14 @@ sub add {
         my $item_type         = $body->{item_type};
         my $expiration_date   = $body->{expiration_date};
         my $notes             = $body->{notes};
-        my $hold_date         = C4::Context->preference( 'AllowHoldDateInFuture' )?$body->{hold_date}:undef;
+        my $hold_date         = $body->{hold_date};
+
+        if(!C4::Context->preference( 'AllowHoldDateInFuture' ) && $hold_date) {
+            return $c->render(
+                status  => 400,
+                openapi => { error => "Hold date in future not allowed" }
+            );
+        }
 
         if ( $item_id and $biblio_id ) {
 
@@ -154,18 +161,19 @@ sub add {
         }
 
         my $hold_id = C4::Reserves::AddReserve(
-            $pickup_library_id,
-            $patron_id,
-            $biblio_id,
-            undef,    # $bibitems param is unused
-            $priority,
-            $hold_date,
-            $expiration_date,
-            $notes,
-            $biblio->title,
-            $item_id,
-            undef,    # TODO: Why not?
-            $item_type
+            {
+                branchcode       => $pickup_library_id,
+                borrowernumber   => $patron_id,
+                biblionumber     => $biblio_id,
+                priority         => $priority,
+                reservation_date => $hold_date,
+                expiration_date  => $expiration_date,
+                notes            => $notes,
+                title            => $biblio->title,
+                itemnumber       => $item_id,
+                found            => undef,                # TODO: Why not?
+                itemtype         => $item_type,
+            }
         );
 
         unless ($hold_id) {
@@ -177,17 +185,20 @@ 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') ) {
             if ( $_->isa('Koha::Exceptions::Object::FKConstraint') ) {
                 my $broken_fk = $_->broken_fk;
 
-                if ( grep { $_ eq $broken_fk } keys %{$Koha::REST::V1::Holds::to_api_mapping} ) {
+                if ( grep { $_ eq $broken_fk } keys %{Koha::Holds->new->to_api_mapping} ) {
                     $c->render(
                         status  => 404,
-                        openapi => $Koha::REST::V1::Holds::to_api_mapping->{$broken_fk} . ' not found.'
+                        openapi => Koha::Holds->new->to_api_mapping->{$broken_fk} . ' not found.'
                     );
                 }
                 else {
@@ -251,7 +262,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
@@ -390,147 +404,4 @@ sub update_priority {
     };
 }
 
-=head2 Internal methods
-
-=head3 _to_api
-
-Helper function that maps unblessed Koha::Hold objects into REST api
-attribute names.
-
-=cut
-
-sub _to_api {
-    my $hold = shift;
-
-    # Rename attributes
-    foreach my $column ( keys %{ $Koha::REST::V1::Holds::to_api_mapping } ) {
-        my $mapped_column = $Koha::REST::V1::Holds::to_api_mapping->{$column};
-        if (    exists $hold->{ $column }
-             && defined $mapped_column )
-        {
-            # key != undef
-            $hold->{ $mapped_column } = delete $hold->{ $column };
-        }
-        elsif (    exists $hold->{ $column }
-                && !defined $mapped_column )
-        {
-            # key == undef
-            delete $hold->{ $column };
-        }
-    }
-
-    return $hold;
-}
-
-=head3 _to_model
-
-Helper function that maps REST api objects into Koha::Hold
-attribute names.
-
-=cut
-
-sub _to_model {
-    my $hold = shift;
-
-    foreach my $attribute ( keys %{ $Koha::REST::V1::Holds::to_model_mapping } ) {
-        my $mapped_attribute = $Koha::REST::V1::Holds::to_model_mapping->{$attribute};
-        if (    exists $hold->{ $attribute }
-             && defined $mapped_attribute )
-        {
-            # key => !undef
-            $hold->{ $mapped_attribute } = delete $hold->{ $attribute };
-        }
-        elsif (    exists $hold->{ $attribute }
-                && !defined $mapped_attribute )
-        {
-            # key => undef / to be deleted
-            delete $hold->{ $attribute };
-        }
-    }
-
-    if ( exists $hold->{lowestPriority} ) {
-        $hold->{lowestPriority} = ($hold->{lowestPriority}) ? 1 : 0;
-    }
-
-    if ( exists $hold->{suspend} ) {
-        $hold->{suspend} = ($hold->{suspend}) ? 1 : 0;
-    }
-
-    if ( exists $hold->{reservedate} ) {
-        $hold->{reservedate} = output_pref({ str => $hold->{reservedate}, dateformat => 'sql' });
-    }
-
-    if ( exists $hold->{cancellationdate} ) {
-        $hold->{cancellationdate} = output_pref({ str => $hold->{cancellationdate}, dateformat => 'sql' });
-    }
-
-    if ( exists $hold->{timestamp} ) {
-        $hold->{timestamp} = output_pref({ str => $hold->{timestamp}, dateformat => 'sql' });
-    }
-
-    if ( exists $hold->{waitingdate} ) {
-        $hold->{waitingdate} = output_pref({ str => $hold->{waitingdate}, dateformat => 'sql' });
-    }
-
-    if ( exists $hold->{expirationdate} ) {
-        $hold->{expirationdate} = output_pref({ str => $hold->{expirationdate}, dateformat => 'sql' });
-    }
-
-    if ( exists $hold->{suspend_until} ) {
-        $hold->{suspend_until} = output_pref({ str => $hold->{suspend_until}, dateformat => 'sql' });
-    }
-
-    return $hold;
-}
-
-=head2 Global variables
-
-=head3 $to_api_mapping
-
-=cut
-
-our $to_api_mapping = {
-    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',
-};
-
-=head3 $to_model_mapping
-
-=cut
-
-our $to_model_mapping = {
-    hold_id           => 'reserve_id',
-    patron_id         => 'borrowernumber',
-    hold_date         => 'reservedate',
-    biblio_id         => 'biblionumber',
-    pickup_library_id => 'branchcode',
-    cancelation_date  => 'cancellationdate',
-    notes             => 'reservenotes',
-    status            => 'found',
-    item_id           => 'itemnumber',
-    waiting_date      => 'waitingdate',
-    expiration_date   => 'expirationdate',
-    lowest_priority   => 'lowestPriority',
-    suspended         => 'suspend',
-    suspended_until   => 'suspend_until',
-    item_type         => 'itemtype',
-    item_level        => 'item_level_hold',
-};
-
 1;