View | Details | Raw Unified | Return to bug 17428
Collapse All | Expand All

(-)a/Koha/REST/V1/Cities.pm (-1 / +133 lines)
Line 0 Link Here
0
- 
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
33
    return try {
34
        $cities =
35
          Koha::Cities->search( $c->req->query_params->to_hash )->unblessed;
36
        return $c->$cb( $cities, 200 );
37
    }
38
    catch {
39
        if ( $_->isa('DBIx::Class::Exception') ) {
40
            return $c->$cb( { error => $_->{msg} }, 500 );
41
        }
42
        else {
43
            return $c->$cb(
44
                { error => "Something went wrong, check the logs." }, 500 );
45
        }
46
    };
47
}
48
49
sub get {
50
    my ( $c, $args, $cb ) = @_;
51
52
    my $city = Koha::Cities->find( $args->{cityid} );
53
    unless ($city) {
54
        return $c->$cb( { error => "City not found" }, 404 );
55
    }
56
57
    return $c->$cb( $city->unblessed, 200 );
58
}
59
60
sub add {
61
    my ( $c, $args, $cb ) = @_;
62
63
    my $city = Koha::City->new( $c->req->json );
64
65
    return try {
66
        $city->store;
67
        return $c->$cb( $city->unblessed, 200 );
68
    }
69
    catch {
70
        if ( $_->isa('DBIx::Class::Exception') ) {
71
            return $c->$cb( { error => $_->msg }, 500 );
72
        }
73
        else {
74
            return $c->$cb(
75
                { error => "Something went wrong, check the logs." }, 500 );
76
        }
77
    };
78
}
79
80
sub update {
81
    my ( $c, $args, $cb ) = @_;
82
83
    my $city;
84
85
    return try {
86
        $city = Koha::Cities->find( $args->{cityid} );
87
        while ( my ( $k, $v ) = each %{ $c->req->json } ) {
88
            $city->$k($v);
89
        }
90
        $city->store;
91
        return $c->$cb( $city->unblessed, 200 );
92
    }
93
    catch {
94
        if ( not defined $city ) {
95
            return $c->$cb( { error => "Object not found" }, 404 );
96
        }
97
        elsif ( $_->isa('Koha::Exceptions::Object') ) {
98
            return $c->$cb( { error => $_->message }, 500 );
99
        }
100
        else {
101
            return $c->$cb(
102
                { error => "Something went wrong, check the logs." }, 500 );
103
        }
104
    };
105
106
}
107
108
sub delete {
109
    my ( $c, $args, $cb ) = @_;
110
111
    my $city;
112
113
    return try {
114
        $city = Koha::Cities->find( $args->{cityid} );
115
        $city->delete;
116
        return $c->$cb( "", 200 );
117
    }
118
    catch {
119
        if ( not defined $city ) {
120
            return $c->$cb( { error => "Object not found" }, 404 );
121
        }
122
        elsif ( $_->isa('DBIx::Class::Exception') ) {
123
            return $c->$cb( { error => $_->msg }, 500 );
124
        }
125
        else {
126
            return $c->$cb(
127
                { error => "Something went wrong, check the logs." }, 500 );
128
        }
129
    };
130
131
}
132
133
1;

Return to bug 17428