From 4fe92ebc26a8c76e36799400385f70eaebe3e8db Mon Sep 17 00:00:00 2001 From: Kyle M Hall Date: Fri, 20 Feb 2015 08:36:58 -0500 Subject: [PATCH] [WIP] Bug 13738 - Add RESTful Borrower service --- Koha/Object.pm | 7 ++ Koha/Objects.pm | 2 + Koha/Service.pm | 2 +- Koha/Service/Borrower.pm | 183 ++++++++++++++++++++++++++++++++++++++++++++++ rest/borrower | 24 ++++++ 5 files changed, 217 insertions(+), 1 deletions(-) create mode 100644 Koha/Service/Borrower.pm create mode 100755 rest/borrower diff --git a/Koha/Object.pm b/Koha/Object.pm index a77eb5c..d78d8ca 100644 --- a/Koha/Object.pm +++ b/Koha/Object.pm @@ -238,6 +238,13 @@ sub _columns { return $self->{_columns}; } +sub as_hashref { + my ($self) = @_; + + my %columns = $self->{_result}->get_columns; + + return \%columns; +} =head3 AUTOLOAD diff --git a/Koha/Objects.pm b/Koha/Objects.pm index 6b3100a..d963260 100644 --- a/Koha/Objects.pm +++ b/Koha/Objects.pm @@ -84,6 +84,8 @@ sub find { my $result = $self->_resultset()->find($id); + return unless $result; + my $object = $self->object_class()->_new_from_dbic( $result ); return $object; diff --git a/Koha/Service.pm b/Koha/Service.pm index 0e7e31b..904daf4 100644 --- a/Koha/Service.pm +++ b/Koha/Service.pm @@ -170,7 +170,7 @@ sub output { }; if ( $options->{type} eq 'json' ) { - $response = encode_json($response); + $response = encode_json($response) if $response; if ( $self->query->param( 'callback' ) ) { $response = $self->query->param( 'callback' ) . '(' . encode_json($response) . ');'; diff --git a/Koha/Service/Borrower.pm b/Koha/Service/Borrower.pm new file mode 100644 index 0000000..6adb49d --- /dev/null +++ b/Koha/Service/Borrower.pm @@ -0,0 +1,183 @@ +package Koha::Service::Borrower; + +# This file is part of Koha. +# +# Copyright (C) 2015 ByWater Solutions +# +# 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, see . + +use Modern::Perl; + +use JSON; +use CGI; + +use C4::Koha; +use Koha::Borrower; +use Koha::Borrowers; + +use base 'Koha::Service'; + +our $cgi = new CGI; + +sub new { + my ($class) = @_; + + # Authentication is handled manually below + return $class->SUPER::new( + { + authnotrequired => 0, + needed_flags => { borrowers => 1 }, + routes => [ + [ qr'GET /(\d+)', 'get_borrower' ], + [ qr'POST /(\d+)', 'add_borrower' ], + [ qr'PUT /(\d+)', 'update_borrower' ], + [ qr'DELETE /(\d+)', 'delete_borrower' ], + ] + } + ); +} + +sub run { + my ($self) = @_; + + $self->authenticate; + + unless ( $self->auth_status eq "ok" ) { + $self->output( '', { status => '403 Forbidden' } ); + exit; + } + + $self->dispatch; +} + +=head2 GET + + Syntax: /rest/borrower/?id= + + Id field can be: + borrowernumber - Koha's internal patron identifier ( default ) + cardnumber - The cardnumber of the patron + userid - The username of the patron + +=cut + +sub get_borrower { + my ( $self, $id ) = @_; + + my $borrower = Koha::Borrowers->find( { get_id_field() => $id } ); + + if ($borrower) { + $self->output( $borrower->as_hashref() ); + } + else { + $self->output( '', { status => '404 Not Found' } ); + } +} + +=head2 POST + + Syntax: /rest/borrower + + Creates a new patron. Data must be supplied as a POST of JSON data. + + Successful creation will return the new patron as JSON. + +=cut + +sub add_borrower { + my ($self) = @_; + + my $query = $self->query; + + my $json = $query->param('POSTDATA'); + my $data = from_json( $json, { utf8 => 1 } ); + + my $borrower = Koha::Borrower->new()->set($data)->store(); + + $self->output( $borrower->as_hashref() ); +} + +=head2 PUT + + Syntax: /rest/borrower/?id= + + Updates a patron. Data must be supplied as a POST of JSON data. + + Successful update will return the updated patron as JSON. + +=cut + +sub update_borrower { + my ( $self, $id ) = @_; + + my $query = $self->query; + + my $json = $query->param('PUTDATA'); + my $data = from_json( $json, { utf8 => 1 } ); + + my $borrower = Koha::Borrowers->find( { get_id_field() => $id } ); + + if ($borrower) { + $borrower->set($data)->store(); + $self->output( $borrower->as_hashref() ); + } + else { + $self->output( '', { status => '404 Not Found' } ); + } + +} + +=head2 DELETE + + Syntax: /rest/borrower/?id= + + Deletes a patron. + + Successful update will return the updated patron as JSON. + +=cut + +sub delete_borrower { + my ( $self, $id ) = @_; + + my $borrower = Koha::Borrowers->find( { get_id_field() => $id } ); + + if ($borrower) { + $borrower->delete(); #TODO Use C4::Members to delete + $self->output( $borrower->as_hashref() ); + } + else { + $self->output( '', { status => '404 Not Found' } ); + } +} + +sub get_id_field { + my $id_field = $cgi->param('id'); + + if ( !$id_field ) { + return 'borrowernumber'; + } + elsif ( $id_field eq 'cardnumber' ) { + return 'cardnumber'; + } + elsif ( $id_field eq 'cardnumber' ) { + return 'cardnumber'; + } + else { + return 'borrowernumber'; + } + +} + +1; diff --git a/rest/borrower b/rest/borrower new file mode 100755 index 0000000..e57a5cf --- /dev/null +++ b/rest/borrower @@ -0,0 +1,24 @@ +#!/usr/bin/perl + +# Copyright (C) 2015 ByWater Solutions +# +# 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 2 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 Koha::Service::Borrower; +Koha::Service::Borrower->new->run; -- 1.7.2.5