View | Details | Raw Unified | Return to bug 13738
Collapse All | Expand All

(-)a/Koha/Object.pm (+7 lines)
Lines 238-243 sub _columns { Link Here
238
    return $self->{_columns};
238
    return $self->{_columns};
239
}
239
}
240
240
241
sub as_hashref {
242
    my ($self) = @_;
243
244
    my %columns = $self->{_result}->get_columns;
245
246
    return \%columns;
247
}
241
248
242
=head3 AUTOLOAD
249
=head3 AUTOLOAD
243
250
(-)a/Koha/Objects.pm (+2 lines)
Lines 84-89 sub find { Link Here
84
84
85
    my $result = $self->_resultset()->find($id);
85
    my $result = $self->_resultset()->find($id);
86
86
87
    return unless $result;
88
87
    my $object = $self->object_class()->_new_from_dbic( $result );
89
    my $object = $self->object_class()->_new_from_dbic( $result );
88
90
89
    return $object;
91
    return $object;
(-)a/Koha/Service.pm (-1 / +1 lines)
Lines 170-176 sub output { Link Here
170
    };
170
    };
171
171
172
    if ( $options->{type} eq 'json' ) {
172
    if ( $options->{type} eq 'json' ) {
173
        $response = encode_json($response);
173
        $response = encode_json($response) if $response;
174
174
175
        if ( $self->query->param( 'callback' ) ) {
175
        if ( $self->query->param( 'callback' ) ) {
176
            $response = $self->query->param( 'callback' ) . '(' . encode_json($response) . ');';
176
            $response = $self->query->param( 'callback' ) . '(' . encode_json($response) . ');';
(-)a/Koha/Service/Borrower.pm (+183 lines)
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;
(-)a/rest/borrower (-1 / +24 lines)
Line 0 Link Here
0
- 
1
#!/usr/bin/perl
2
3
# Copyright (C) 2015 ByWater Solutions
4
#
5
# This file is part of Koha.
6
#
7
# Koha is free software; you can redistribute it and/or modify it under the
8
# terms of the GNU General Public License as published by the Free Software
9
# Foundation; either version 2 of the License, or (at your option) any later
10
# version.
11
#
12
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14
# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15
#
16
# You should have received a copy of the GNU General Public License along
17
# with Koha; if not, write to the Free Software Foundation, Inc.,
18
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
#
20
21
use Modern::Perl;
22
23
use Koha::Service::Borrower;
24
Koha::Service::Borrower->new->run;

Return to bug 13738