Bug 16330: Move patches to OpenAPI
[koha.git] / Koha / REST / V1 / Patron.pm
blob52ffab3bbb30064f3c858ea7dfed7760be593279
1 package Koha::REST::V1::Patron;
3 # This file is part of Koha.
5 # Koha is free software; you can redistribute it and/or modify it under the
6 # terms of the GNU General Public License as published by the Free Software
7 # Foundation; either version 3 of the License, or (at your option) any later
8 # version.
10 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
11 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License along
15 # with Koha; if not, write to the Free Software Foundation, Inc.,
16 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 use Modern::Perl;
20 use Mojo::Base 'Mojolicious::Controller';
22 use C4::Members qw( AddMember ModMember );
23 use Koha::Patrons;
25 use Scalar::Util qw(blessed);
26 use Try::Tiny;
28 =head1 NAME
30 Koha::REST::V1::Patron
32 =head1 API
34 =head2 Methods
36 =head3 list
38 Controller function that handles listing Koha::Patron objects
40 =cut
42 sub list {
43 my $c = shift->openapi->valid_input or return;
45 return try {
46 my $patrons_set = Koha::Patrons->new;
47 my @patrons = $c->objects->search( $patrons_set )->as_list;
48 return $c->render( status => 200, openapi => \@patrons );
50 catch {
51 if ( $_->isa('DBIx::Class::Exception') ) {
52 return $c->render( status => 500,
53 openapi => { error => $_->{msg} } );
55 else {
56 return $c->render(
57 status => 500,
58 openapi => { error => "Something went wrong, check the logs." }
64 =head3 get
66 Controller function that handles retrieving a single Koha::Patron object
68 =cut
70 sub get {
71 my $c = shift->openapi->valid_input or return;
73 my $borrowernumber = $c->validation->param('borrowernumber');
74 my $patron = Koha::Patrons->find($borrowernumber);
76 unless ($patron) {
77 return $c->render(status => 404, openapi => { error => "Patron not found." });
80 return $c->render(status => 200, openapi => $patron);
83 =head3 add
85 Controller function that handles adding a new Koha::Patron object
87 =cut
89 sub add {
90 my $c = shift->openapi->valid_input or return;
92 return try {
93 my $body = $c->validation->param('body');
95 Koha::Patron->new($body)->_validate;
97 # TODO: Use AddMember until it has been moved to Koha-namespace
98 my $borrowernumber = AddMember(%$body);
99 my $patron = Koha::Patrons->find($borrowernumber);
101 return $c->render( status => 201, openapi => $patron );
103 catch {
104 unless ( blessed $_ && $_->can('rethrow') ) {
105 return $c->render(
106 status => 500,
107 openapi => {
108 error => "Something went wrong, check Koha logs for details."
112 if ( $_->isa('Koha::Exceptions::Patron::DuplicateObject') ) {
113 return $c->render(
114 status => 409,
115 openapi => { error => $_->error, conflict => $_->conflict }
118 elsif ( $_->isa('Koha::Exceptions::Library::BranchcodeNotFound') ) {
119 return $c->render(
120 status => 400,
121 openapi => { error => "Given branchcode does not exist" }
124 elsif ( $_->isa('Koha::Exceptions::Category::CategorycodeNotFound') ) {
125 return $c->render(
126 status => 400,
127 openapi => { error => "Given categorycode does not exist" }
130 else {
131 return $c->render(
132 status => 500,
133 openapi => {
134 error => "Something went wrong, check Koha logs for details."
141 =head3 update
143 Controller function that handles updating a Koha::Patron object
145 =cut
147 sub update {
148 my $c = shift->openapi->valid_input or return;
150 my $patron = Koha::Patrons->find( $c->validation->param('borrowernumber') );
152 return try {
153 my $body = $c->validation->param('body');
155 $patron->set( _to_model($body) )->_validate;
157 # TODO: Use ModMember until it has been moved to Koha-namespace
158 if ( ModMember(%$body) ) {
159 return $c->render( status => 200, openapi => $patron );
161 else {
162 return $c->render(
163 status => 500,
164 openapi => {
165 error => 'Something went wrong, check Koha logs for details.'
170 catch {
171 unless ($patron) {
172 return $c->render(
173 status => 404,
174 openapi => { error => "Patron not found" }
177 unless ( blessed $_ && $_->can('rethrow') ) {
178 return $c->render(
179 status => 500,
180 openapi => {
181 error => "Something went wrong, check Koha logs for details."
185 if ( $_->isa('Koha::Exceptions::Patron::DuplicateObject') ) {
186 return $c->render(
187 status => 409,
188 openapi => { error => $_->error, conflict => $_->conflict }
191 elsif ( $_->isa('Koha::Exceptions::Library::BranchcodeNotFound') ) {
192 return $c->render(
193 status => 400,
194 openapi => { error => "Given branchcode does not exist" }
197 elsif ( $_->isa('Koha::Exceptions::Category::CategorycodeNotFound') ) {
198 return $c->render(
199 status => 400,
200 openapi => { error => "Given categorycode does not exist" }
203 elsif ( $_->isa('Koha::Exceptions::MissingParameter') ) {
204 return $c->render(
205 status => 400,
206 openapi => {
207 error => "Missing mandatory parameter(s)",
208 parameters => $_->parameter
212 elsif ( $_->isa('Koha::Exceptions::BadParameter') ) {
213 return $c->render(
214 status => 400,
215 openapi => {
216 error => "Invalid parameter(s)",
217 parameters => $_->parameter
221 elsif ( $_->isa('Koha::Exceptions::NoChanges') ) {
222 return $c->render(
223 status => 204,
224 openapi => { error => "No changes have been made" }
227 else {
228 return $c->render(
229 status => 500,
230 openapi => {
231 error =>
232 "Something went wrong, check Koha logs for details."
239 =head3 delete
241 Controller function that handles deleting a Koha::Patron object
243 =cut
245 sub delete {
246 my $c = shift->openapi->valid_input or return;
248 my $patron;
250 return try {
251 $patron = Koha::Patrons->find( $c->validation->param('borrowernumber') );
253 # check if loans, reservations, debarrment, etc. before deletion!
254 my $res = $patron->delete;
255 return $c->render( status => 200, openapi => {} );
257 catch {
258 unless ($patron) {
259 return $c->render(
260 status => 404,
261 openapi => { error => "Patron not found" }
264 else {
265 return $c->render(
266 status => 500,
267 openapi => {
268 error =>
269 "Something went wrong, check Koha logs for details."
276 =head3 _to_model
278 Helper function that maps REST api objects into Koha::Patron
279 attribute names.
281 =cut
283 sub _to_model {
284 my $params = shift;
286 $params->{lost} = ($params->{lost}) ? 1 : 0;
287 $params->{gonenoaddress} = ($params->{gonenoaddress}) ? 1 : 0;
289 return $params;