Bug 17428: (QA followup) Remove warning
[koha.git] / 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 C4::Auth qw( haspermission );
23 use Koha::City;
24 use Koha::Cities;
25
26 use Try::Tiny;
27
28 sub list {
29     my ( $c, $args, $cb ) = @_;
30
31     my $cities;
32     my $filter;
33     $args //= {};
34
35     for my $filter_param ( keys %$args ) {
36         $filter->{$filter_param} = { LIKE => $args->{$filter_param} . "%" };
37     }
38
39     return try {
40         $cities = Koha::Cities->search($filter)->unblessed;
41         return $c->$cb( $cities, 200 );
42     }
43     catch {
44         if ( $_->isa('DBIx::Class::Exception') ) {
45             return $c->$cb( { error => $_->{msg} }, 500 );
46         }
47         else {
48             return $c->$cb(
49                 { error => "Something went wrong, check the logs." }, 500 );
50         }
51     };
52 }
53
54 sub get {
55     my ( $c, $args, $cb ) = @_;
56
57     my $city = Koha::Cities->find( $args->{cityid} );
58     unless ($city) {
59         return $c->$cb( { error => "City not found" }, 404 );
60     }
61
62     return $c->$cb( $city->unblessed, 200 );
63 }
64
65 sub add {
66     my ( $c, $args, $cb ) = @_;
67
68     my $city = Koha::City->new( $args->{body} );
69
70     return try {
71         $city->store;
72         return $c->$cb( $city->unblessed, 200 );
73     }
74     catch {
75         if ( $_->isa('DBIx::Class::Exception') ) {
76             return $c->$cb( { error => $_->msg }, 500 );
77         }
78         else {
79             return $c->$cb(
80                 { error => "Something went wrong, check the logs." }, 500 );
81         }
82     };
83 }
84
85 sub update {
86     my ( $c, $args, $cb ) = @_;
87
88     my $city;
89
90     return try {
91         $city = Koha::Cities->find( $args->{cityid} );
92         $city->set( $args->{body} );
93         $city->store();
94         return $c->$cb( $city->unblessed, 200 );
95     }
96     catch {
97         if ( not defined $city ) {
98             return $c->$cb( { error => "Object not found" }, 404 );
99         }
100         elsif ( $_->isa('Koha::Exceptions::Object') ) {
101             return $c->$cb( { error => $_->message }, 500 );
102         }
103         else {
104             return $c->$cb(
105                 { error => "Something went wrong, check the logs." }, 500 );
106         }
107     };
108
109 }
110
111 sub delete {
112     my ( $c, $args, $cb ) = @_;
113
114     my $city;
115
116     return try {
117         $city = Koha::Cities->find( $args->{cityid} );
118         $city->delete;
119         return $c->$cb( "", 200 );
120     }
121     catch {
122         if ( not defined $city ) {
123             return $c->$cb( { error => "Object not found" }, 404 );
124         }
125         elsif ( $_->isa('DBIx::Class::Exception') ) {
126             return $c->$cb( { error => $_->msg }, 500 );
127         }
128         else {
129             return $c->$cb(
130                 { error => "Something went wrong, check the logs." }, 500 );
131         }
132     };
133
134 }
135
136 1;