|
Line 0
Link Here
|
|
|
1 |
package Koha::Service::Borrower; |
| 2 |
|
| 3 |
# This file is part of Koha. |
| 4 |
# |
| 5 |
# Copyright (C) 2015 ByWater Solutions |
| 6 |
# |
| 7 |
# Koha is free software; you can redistribute it and/or modify it |
| 8 |
# under the terms of the GNU General Public License as published by |
| 9 |
# the Free Software Foundation; either version 3 of the License, or |
| 10 |
# (at your option) any later version. |
| 11 |
# |
| 12 |
# Koha is distributed in the hope that it will be useful, but |
| 13 |
# WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
# GNU General Public License for more details. |
| 16 |
# |
| 17 |
# You should have received a copy of the GNU General Public License |
| 18 |
# along with Koha; if not, see <http://www.gnu.org/licenses>. |
| 19 |
|
| 20 |
use Modern::Perl; |
| 21 |
|
| 22 |
use JSON; |
| 23 |
use CGI; |
| 24 |
|
| 25 |
use C4::Koha; |
| 26 |
use Koha::Borrower; |
| 27 |
use Koha::Borrowers; |
| 28 |
|
| 29 |
use base 'Koha::Service'; |
| 30 |
|
| 31 |
our $cgi = new CGI; |
| 32 |
|
| 33 |
sub new { |
| 34 |
my ($class) = @_; |
| 35 |
|
| 36 |
# Authentication is handled manually below |
| 37 |
return $class->SUPER::new( |
| 38 |
{ |
| 39 |
authnotrequired => 0, |
| 40 |
needed_flags => { borrowers => 1 }, |
| 41 |
routes => [ |
| 42 |
[ qr'GET /(\d+)', 'get_borrower' ], |
| 43 |
[ qr'POST /(\d+)', 'add_borrower' ], |
| 44 |
[ qr'PUT /(\d+)', 'update_borrower' ], |
| 45 |
[ qr'DELETE /(\d+)', 'delete_borrower' ], |
| 46 |
] |
| 47 |
} |
| 48 |
); |
| 49 |
} |
| 50 |
|
| 51 |
sub run { |
| 52 |
my ($self) = @_; |
| 53 |
|
| 54 |
$self->authenticate; |
| 55 |
|
| 56 |
unless ( $self->auth_status eq "ok" ) { |
| 57 |
$self->output( '', { status => '403 Forbidden' } ); |
| 58 |
exit; |
| 59 |
} |
| 60 |
|
| 61 |
$self->dispatch; |
| 62 |
} |
| 63 |
|
| 64 |
=head2 GET |
| 65 |
|
| 66 |
Syntax: /rest/borrower/<id>?id=<id_field> |
| 67 |
|
| 68 |
Id field can be: |
| 69 |
borrowernumber - Koha's internal patron identifier ( default ) |
| 70 |
cardnumber - The cardnumber of the patron |
| 71 |
userid - The username of the patron |
| 72 |
|
| 73 |
=cut |
| 74 |
|
| 75 |
sub get_borrower { |
| 76 |
my ( $self, $id ) = @_; |
| 77 |
|
| 78 |
my $borrower = Koha::Borrowers->find( { get_id_field() => $id } ); |
| 79 |
|
| 80 |
if ($borrower) { |
| 81 |
$self->output( $borrower->as_hashref() ); |
| 82 |
} |
| 83 |
else { |
| 84 |
$self->output( '', { status => '404 Not Found' } ); |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
=head2 POST |
| 89 |
|
| 90 |
Syntax: /rest/borrower |
| 91 |
|
| 92 |
Creates a new patron. Data must be supplied as a POST of JSON data. |
| 93 |
|
| 94 |
Successful creation will return the new patron as JSON. |
| 95 |
|
| 96 |
=cut |
| 97 |
|
| 98 |
sub add_borrower { |
| 99 |
my ($self) = @_; |
| 100 |
|
| 101 |
my $query = $self->query; |
| 102 |
|
| 103 |
my $json = $query->param('POSTDATA'); |
| 104 |
my $data = from_json( $json, { utf8 => 1 } ); |
| 105 |
|
| 106 |
my $borrower = Koha::Borrower->new()->set($data)->store(); |
| 107 |
|
| 108 |
$self->output( $borrower->as_hashref() ); |
| 109 |
} |
| 110 |
|
| 111 |
=head2 PUT |
| 112 |
|
| 113 |
Syntax: /rest/borrower/<id>?id=<id_field> |
| 114 |
|
| 115 |
Updates a patron. Data must be supplied as a POST of JSON data. |
| 116 |
|
| 117 |
Successful update will return the updated patron as JSON. |
| 118 |
|
| 119 |
=cut |
| 120 |
|
| 121 |
sub update_borrower { |
| 122 |
my ( $self, $id ) = @_; |
| 123 |
|
| 124 |
my $query = $self->query; |
| 125 |
|
| 126 |
my $json = $query->param('PUTDATA'); |
| 127 |
my $data = from_json( $json, { utf8 => 1 } ); |
| 128 |
|
| 129 |
my $borrower = Koha::Borrowers->find( { get_id_field() => $id } ); |
| 130 |
|
| 131 |
if ($borrower) { |
| 132 |
$borrower->set($data)->store(); |
| 133 |
$self->output( $borrower->as_hashref() ); |
| 134 |
} |
| 135 |
else { |
| 136 |
$self->output( '', { status => '404 Not Found' } ); |
| 137 |
} |
| 138 |
|
| 139 |
} |
| 140 |
|
| 141 |
=head2 DELETE |
| 142 |
|
| 143 |
Syntax: /rest/borrower/<id>?id=<id_field> |
| 144 |
|
| 145 |
Deletes a patron. |
| 146 |
|
| 147 |
Successful update will return the updated patron as JSON. |
| 148 |
|
| 149 |
=cut |
| 150 |
|
| 151 |
sub delete_borrower { |
| 152 |
my ( $self, $id ) = @_; |
| 153 |
|
| 154 |
my $borrower = Koha::Borrowers->find( { get_id_field() => $id } ); |
| 155 |
|
| 156 |
if ($borrower) { |
| 157 |
$borrower->delete(); #TODO Use C4::Members to delete |
| 158 |
$self->output( $borrower->as_hashref() ); |
| 159 |
} |
| 160 |
else { |
| 161 |
$self->output( '', { status => '404 Not Found' } ); |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
sub get_id_field { |
| 166 |
my $id_field = $cgi->param('id'); |
| 167 |
|
| 168 |
if ( !$id_field ) { |
| 169 |
return 'borrowernumber'; |
| 170 |
} |
| 171 |
elsif ( $id_field eq 'cardnumber' ) { |
| 172 |
return 'cardnumber'; |
| 173 |
} |
| 174 |
elsif ( $id_field eq 'cardnumber' ) { |
| 175 |
return 'cardnumber'; |
| 176 |
} |
| 177 |
else { |
| 178 |
return 'borrowernumber'; |
| 179 |
} |
| 180 |
|
| 181 |
} |
| 182 |
|
| 183 |
1; |