Bug 22789: Add non priority feature to C4 classes and staff interface
[koha.git] / Koha / REST / V1 / Libraries.pm
1 package Koha::REST::V1::Libraries;
2
3 # This file is part of Koha.
4 #
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
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     return try {
47         my $libraries_set = Koha::Libraries->new;
48         my $libraries     = $c->objects->search( $libraries_set );
49         return $c->render( status => 200, openapi => $libraries );
50     }
51     catch {
52         $c->unhandled_exception($_);
53     };
54 }
55
56 =head3 get
57
58 Controller function that handles retrieving a single Koha::Library
59
60 =cut
61
62 sub get {
63     my $c = shift->openapi->valid_input or return;
64
65     return try {
66         my $library_id = $c->validation->param('library_id');
67         my $library = Koha::Libraries->find( $library_id );
68
69         unless ($library) {
70             return $c->render( status  => 404,
71                             openapi => { error => "Library not found" } );
72         }
73
74         return $c->render(
75             status  => 200,
76             openapi => $library->to_api
77         );
78     }
79     catch {
80         $c->unhandled_exception($_);
81     };
82 }
83
84 =head3 add
85
86 Controller function that handles adding a new Koha::Library object
87
88 =cut
89
90 sub add {
91     my $c = shift->openapi->valid_input or return;
92
93     return try {
94         my $library = Koha::Library->new_from_api( $c->validation->param('body') );
95         $library->store;
96         $c->res->headers->location( $c->req->url->to_string . '/' . $library->branchcode );
97
98         return $c->render(
99             status  => 201,
100             openapi => $library->to_api
101         );
102     }
103     catch {
104         if ( blessed $_ && $_->isa('Koha::Exceptions::Object::DuplicateID') ) {
105             return $c->render(
106                 status  => 409,
107                 openapi => { error => $_->error, conflict => $_->duplicate_id }
108             );
109         }
110
111         $c->unhandled_exception($_);
112     };
113 }
114
115 =head3 update
116
117 Controller function that handles updating a Koha::Library object
118
119 =cut
120
121 sub update {
122     my $c = shift->openapi->valid_input or return;
123
124     my $library = Koha::Libraries->find( $c->validation->param('library_id') );
125
126     if ( not defined $library ) {
127         return $c->render(
128             status  => 404,
129             openapi => { error => "Library not found" }
130         );
131     }
132
133     return try {
134         my $params = $c->req->json;
135         $library->set_from_api( $params );
136         $library->store();
137         return $c->render(
138             status  => 200,
139             openapi => $library->to_api
140         );
141     }
142     catch {
143         $c->unhandled_exception($_);
144     };
145 }
146
147 =head3 delete
148
149 Controller function that handles deleting a Koha::Library object
150
151 =cut
152
153 sub delete {
154
155     my $c = shift->openapi->valid_input or return;
156
157     my $library = Koha::Libraries->find( $c->validation->param( 'library_id' ) );
158
159     if ( not defined $library ) {
160         return $c->render( status => 404, openapi => { error => "Library not found" } );
161     }
162
163     return try {
164         $library->delete;
165         return $c->render( status => 204, openapi => '');
166     }
167     catch {
168         $c->unhandled_exception($_);
169     };
170 }
171
172 1;