e458fe3fc6684992c4e494dc5806fb98cb0cae47
[srvgit] / Koha / REST / V1 / Cities.pm
1 package Koha::REST::V1::Cities;
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
22 use Koha::Cities;
23
24 use Try::Tiny;
25
26 =head1 API
27
28 =head2 Methods
29
30 =head3 list
31
32 =cut
33
34 sub list {
35     my $c = shift->openapi->valid_input or return;
36
37     return try {
38         my $cities_set = Koha::Cities->new;
39         my $cities = $c->objects->search( $cities_set );
40         return $c->render( status => 200, openapi => $cities );
41     }
42     catch {
43         if ( $_->isa('DBIx::Class::Exception') ) {
44             return $c->render( status  => 500,
45                                openapi => { error => $_->{msg} } );
46         }
47         else {
48             return $c->render( status => 500,
49                 openapi => { error => "Something went wrong, check the logs."} );
50         }
51     };
52
53 }
54
55 =head3 get
56
57 =cut
58
59 sub get {
60     my $c = shift->openapi->valid_input or return;
61
62     my $city = Koha::Cities->find( $c->validation->param('city_id') );
63     unless ($city) {
64         return $c->render( status  => 404,
65                            openapi => { error => "City not found" } );
66     }
67
68     return $c->render( status => 200, openapi => $city->to_api );
69 }
70
71 =head3 add
72
73 =cut
74
75 sub add {
76     my $c = shift->openapi->valid_input or return;
77
78     return try {
79         my $city = Koha::City->new_from_api( $c->validation->param('body') );
80         $city->store;
81         $c->res->headers->location( $c->req->url->to_string . '/' . $city->cityid );
82         return $c->render(
83             status  => 201,
84             openapi => $city->to_api
85         );
86     }
87     catch {
88         if ( $_->isa('DBIx::Class::Exception') ) {
89             return $c->render(
90                 status  => 500,
91                 openapi => { error => $_->{msg} }
92             );
93         }
94         else {
95             return $c->render(
96                 status  => 500,
97                 openapi => { error => "Something went wrong, check the logs." }
98             );
99         }
100     };
101 }
102
103 =head3 update
104
105 =cut
106
107 sub update {
108     my $c = shift->openapi->valid_input or return;
109
110     my $city = Koha::Cities->find( $c->validation->param('city_id') );
111
112     if ( not defined $city ) {
113         return $c->render( status  => 404,
114                            openapi => { error => "Object not found" } );
115     }
116
117     return try {
118         $city->set_from_api( $c->validation->param('body') );
119         $city->store();
120         return $c->render( status => 200, openapi => $city->to_api );
121     }
122     catch {
123         if ( $_->isa('Koha::Exceptions::Object') ) {
124             return $c->render( status  => 500,
125                                openapi => { error => $_->message } );
126         }
127         else {
128             return $c->render( status => 500,
129                 openapi => { error => "Something went wrong, check the logs."} );
130         }
131     };
132 }
133
134 =head3 delete
135
136 =cut
137
138 sub delete {
139     my $c = shift->openapi->valid_input or return;
140
141     my $city = Koha::Cities->find( $c->validation->param('city_id') );
142     if ( not defined $city ) {
143         return $c->render( status  => 404,
144                            openapi => { error => "Object not found" } );
145     }
146
147     return try {
148         $city->delete;
149         return $c->render( status => 200, openapi => "" );
150     }
151     catch {
152         if ( $_->isa('DBIx::Class::Exception') ) {
153             return $c->render( status  => 500,
154                                openapi => { error => $_->{msg} } );
155         }
156         else {
157             return $c->render( status => 500,
158                 openapi => { error => "Something went wrong, check the logs."} );
159         }
160     };
161 }
162
163 1;