From d4b2744085cddc1be3ade9a2d934846bba294a1a Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Tue, 5 Sep 2017 16:44:43 -0300 Subject: [PATCH] Bug 18731: /acquisitions/orders endpoint This patches implement the /acquisitions/orders endpoint. It uses the new pagination, query parsing and object searching plugins. Sponsored-by: Camden County --- Koha/REST/V1/Acquisitions/Orders.pm | 170 ++++++++++++++++++++++++++++++++++++ Koha/Schema/Result/Aqorder.pm | 3 + 2 files changed, 173 insertions(+) create mode 100644 Koha/REST/V1/Acquisitions/Orders.pm diff --git a/Koha/REST/V1/Acquisitions/Orders.pm b/Koha/REST/V1/Acquisitions/Orders.pm new file mode 100644 index 0000000000..d4f16e19f5 --- /dev/null +++ b/Koha/REST/V1/Acquisitions/Orders.pm @@ -0,0 +1,170 @@ +package Koha::REST::V1::Acquisitions::Orders; + +# 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 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. + +use Modern::Perl; + +use Mojo::Base 'Mojolicious::Controller'; + +use Koha::Acquisition::Orders; + +use Try::Tiny; + +sub list_orders { + + my $c = shift->openapi->valid_input or return; + + return try { + my $orders = $c->objects->search( Koha::Acquisition::Orders->new ); + + return $c->render( + status => 200, + openapi => $orders + ); + } + catch { + if ( $_->isa('DBIx::Class::Exception') ) { + return $c->render( + status => 500, + openapi => { error => $_->{msg} } + ); + } + else { + return $c->render( + status => 500, + openapi => { error => "Something went wrong, check the logs." } + ); + } + }; +} + +sub get_order { + my $c = shift->openapi->valid_input or return; + + my $order = Koha::Acquisition::Orders->find( $c->validation->param('ordernumber') ); + unless ($order) { + return $c->render( + status => 404, + openapi => { error => "Order not found" } + ); + } + + return $c->render( + status => 200, + openapi => $order + ); +} + +sub add_order { + my $c = shift->openapi->valid_input or return; + + my $order = Koha::Acquisition::Order->new( $c->validation->param('body') ); + + return try { + $order->store; + return $c->render( + status => 200, + openapi => $order + ); + } + catch { + if ( $_->isa('DBIx::Class::Exception') ) { + return $c->render( + status => 500, + openapi => { error => $_->msg } + ); + } + else { + return $c->render( + status => 500, + openapi => { error => "Something went wrong, check the logs." } + ); + } + }; +} + +sub update_order { + my $c = shift->openapi->valid_input or return; + + my $order; + + return try { + $order = Koha::Acquisition::Orders->find( $c->validation->param('ordernumber') ); + $order->set( $c->validation->param('body') ); + $order->store(); + return $c->render( + status => 200, + openapi => $order + ); + } + catch { + if ( not defined $order ) { + return $c->render( + status => 404, + openapi => { error => "Object not found" } + ); + } + elsif ( $_->isa('Koha::Exceptions::Object') ) { + return $c->render( + status => 500, + openapi => { error => $_->message } + ); + } + else { + return $c->render( + status => 500, + openapi => { error => "Something went wrong, check the logs." } + ); + } + }; +} + +sub delete_order { + my $c = shift->openapi->valid_input or return; + + my $order; + + return try { + $order = Koha::Acquisition::Orders->find( $c->validation->param('ordernumber') ); + $order->delete; + return $c->render( + status => 200, + openapi => q{} + ); + } + catch { + if ( not defined $order ) { + return $c->render( + status => 404, + openapi => { error => "Object not found" } + ); + } + elsif ( $_->isa('DBIx::Class::Exception') ) { + return $c->render( + status => 500, + openapi => { error => $_->msg } + ); + } + else { + return $c->render( + status => 500, + openapi => { error => "Something went wrong, check the logs." } + ); + } + }; +} + +1; diff --git a/Koha/Schema/Result/Aqorder.pm b/Koha/Schema/Result/Aqorder.pm index af849d7882..9f39d456b3 100644 --- a/Koha/Schema/Result/Aqorder.pm +++ b/Koha/Schema/Result/Aqorder.pm @@ -628,6 +628,9 @@ __PACKAGE__->many_to_many("borrowernumbers", "aqorder_users", "borrowernumber"); # Created by DBIx::Class::Schema::Loader v0.07042 @ 2016-10-28 15:05:37 # DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:FyZsBWGJ8wsPkFdYUAetmg +__PACKAGE__->add_columns( + '+uncertainprice' => { is_boolean => 1 } +); # You can replace this text with custom code or comments, and it will be preserved on regeneration 1; -- 2.15.0