c0b92b2de9e7d079f1d07100b08085b967e2a3d4
[koha.git] / Koha / REST / V1 / Library.pm
1 package Koha::REST::V1::Library;
2
3 # This file is part of Koha.
4 #
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.
9 #
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.
13 #
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.
17
18 use Modern::Perl;
19
20 use Mojo::Base 'Mojolicious::Controller';
21 use Koha::Libraries;
22
23 use Scalar::Util qw( blessed );
24
25 use Try::Tiny;
26
27 =head1 NAME
28
29 Koha::REST::V1::Library - Koha REST API for handling libraries (V1)
30
31 =head1 API
32
33 =head2 Methods
34
35 =cut
36
37 =head3 list
38
39 Controller function that handles listing Koha::Library objects
40
41 =cut
42
43 sub list {
44     my $c = shift->openapi->valid_input or return;
45
46     my $libraries;
47     my $filter;
48     my $args = $c->req->params->to_hash;
49
50     for my $filter_param ( keys %$args ) {
51         $filter->{$filter_param} = { LIKE => $args->{$filter_param} . "%" };
52     }
53
54     return try {
55         my $libraries = Koha::Libraries->search($filter);
56         return $c->render( status => 200, openapi => $libraries );
57     }
58     catch {
59         if ( $_->isa('DBIx::Class::Exception') ) {
60             return $c->render( status  => 500,
61                                openapi => { error => $_->{msg} } );
62         }
63         else {
64             return $c->render( status => 500,
65                 openapi => { error => "Something went wrong, check the logs."} );
66         }
67     };
68 }
69
70 =head3 get
71
72 Controller function that handles retrieving a single Koha::Library
73
74 =cut
75
76 sub get {
77     my $c = shift->openapi->valid_input or return;
78
79     my $branchcode = $c->validation->param('branchcode');
80     my $library = Koha::Libraries->find({ branchcode => $branchcode });
81     unless ($library) {
82         return $c->render( status  => 404,
83                            openapi => { error => "Library not found" } );
84     }
85
86     return $c->render( status => 200, openapi => $library );
87 }
88
89 =head3 add
90
91 Controller function that handles adding a new Koha::Library object
92
93 =cut
94
95 sub add {
96     my $c = shift->openapi->valid_input or return;
97
98     return try {
99         if (Koha::Libraries->find($c->req->json->{branchcode})) {
100             return $c->render( status => 400,
101                 openapi => { error => 'Library already exists' } );
102         }
103         my $library = Koha::Library->new($c->validation->param('body'))->store;
104         my $branchcode = $library->branchcode;
105         $c->res->headers->location($c->req->url->to_string.'/'.$branchcode);
106         return $c->render( status => 201, openapi => $library);
107     }
108     catch {
109         if ( $_->isa('DBIx::Class::Exception') ) {
110             return $c->render( status  => 500,
111                                openapi => { error => $_->{msg} } );
112         }
113         else {
114             return $c->render( status => 500,
115                 openapi => { error => "Something went wrong, check the logs."} );
116         }
117     };
118 }
119
120 =head3 update
121
122 Controller function that handles updating a Koha::Library object
123
124 =cut
125
126 sub update {
127     my $c = shift->openapi->valid_input or return;
128
129     my $library;
130     return try {
131         $library = Koha::Libraries->find($c->validation->param('branchcode'));
132         $library->set($c->validation->param('body'))->store;
133         return $c->render( status => 200, openapi => $library );
134     }
135     catch {
136         if ( not defined $library ) {
137             return $c->render( status => 404,
138                                openapi => { error => "Object not found" });
139         }
140         elsif ( $_->isa('DBIx::Class::Exception') ) {
141             return $c->render( status  => 500,
142                                openapi => { error => $_->{msg} } );
143         }
144         else {
145             return $c->render( status => 500,
146                 openapi => { error => "Something went wrong, check the logs."} );
147         }
148     };
149 }
150
151 =head3 delete
152
153 Controller function that handles deleting a Koha::Library object
154
155 =cut
156
157 sub delete {
158     my $c = shift->openapi->valid_input or return;
159
160     my $library;
161     return try {
162         $library = Koha::Libraries->find($c->validation->param('branchcode'));
163         $library->delete;
164         return $c->render( status => 204, openapi => '');
165     }
166     catch {
167         if ( not defined $library ) {
168             return $c->render( status => 404, openapi => { error => "Object not found" } );
169         }
170         elsif ( $_->isa('DBIx::Class::Exception') ) {
171             return $c->render( status  => 500,
172                                openapi => { error => $_->{msg} } );
173         }
174         else {
175             return $c->render( status => 500,
176                 openapi => { error => "Something went wrong, check the logs."} );
177         }
178     };
179 }
180
181 1;