From 4e27aacd896937ac8b7f8a5f181f45937e4b393d Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Tue, 5 Sep 2017 16:44:43 -0300 Subject: [PATCH] Bug 18731: WIP /acquisitions/orders endpoint This patches implement the /acquisitions/orders endpoint. It uses the new pagination and query parsing plugins. Sponsored-by: Camden County --- Koha/REST/V1/Acquisitions/Orders.pm | 170 ++++++++++++++++++++++++++++++++++++ 1 file changed, 170 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..7d47306005 --- /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 = Koha::Acquisition::Orders->search_for_api($c); + + 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; -- 2.15.0