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