|
Lines 19-25
use Modern::Perl;
Link Here
|
| 19 |
|
19 |
|
| 20 |
use Mojo::Base 'Mojolicious::Controller'; |
20 |
use Mojo::Base 'Mojolicious::Controller'; |
| 21 |
|
21 |
|
| 22 |
use Koha::City; |
|
|
| 23 |
use Koha::Cities; |
22 |
use Koha::Cities; |
| 24 |
|
23 |
|
| 25 |
use Try::Tiny; |
24 |
use Try::Tiny; |
|
Lines 27-42
use Try::Tiny;
Link Here
|
| 27 |
sub list { |
26 |
sub list { |
| 28 |
my $c = shift->openapi->valid_input or return; |
27 |
my $c = shift->openapi->valid_input or return; |
| 29 |
|
28 |
|
| 30 |
my $cities; |
|
|
| 31 |
my $filter; |
| 32 |
my $args = $c->req->params->to_hash; |
| 33 |
|
| 34 |
for my $filter_param ( keys %$args ) { |
| 35 |
$filter->{$filter_param} = { LIKE => $args->{$filter_param} . "%" }; |
| 36 |
} |
| 37 |
|
| 38 |
return try { |
29 |
return try { |
| 39 |
$cities = Koha::Cities->search($filter); |
30 |
my $cities_set = Koha::Cities->new; |
|
|
31 |
my $cities = $c->objects->search( $cities_set, \&_to_model, \&_to_api ); |
| 40 |
return $c->render( status => 200, openapi => $cities ); |
32 |
return $c->render( status => 200, openapi => $cities ); |
| 41 |
} |
33 |
} |
| 42 |
catch { |
34 |
catch { |
|
Lines 55-86
sub list {
Link Here
|
| 55 |
sub get { |
47 |
sub get { |
| 56 |
my $c = shift->openapi->valid_input or return; |
48 |
my $c = shift->openapi->valid_input or return; |
| 57 |
|
49 |
|
| 58 |
my $city = Koha::Cities->find( $c->validation->param('cityid') ); |
50 |
my $city = Koha::Cities->find( $c->validation->param('city_id') ); |
| 59 |
unless ($city) { |
51 |
unless ($city) { |
| 60 |
return $c->render( status => 404, |
52 |
return $c->render( status => 404, |
| 61 |
openapi => { error => "City not found" } ); |
53 |
openapi => { error => "City not found" } ); |
| 62 |
} |
54 |
} |
| 63 |
|
55 |
|
| 64 |
return $c->render( status => 200, openapi => $city ); |
56 |
return $c->render( status => 200, openapi => _to_api($city->TO_JSON) ); |
| 65 |
} |
57 |
} |
| 66 |
|
58 |
|
| 67 |
sub add { |
59 |
sub add { |
| 68 |
my $c = shift->openapi->valid_input or return; |
60 |
my $c = shift->openapi->valid_input or return; |
| 69 |
|
61 |
|
| 70 |
my $city = Koha::City->new( $c->validation->param('body') ); |
|
|
| 71 |
|
| 72 |
return try { |
62 |
return try { |
|
|
63 |
my $city = Koha::City->new( _to_model( $c->validation->param('body') ) ); |
| 73 |
$city->store; |
64 |
$city->store; |
| 74 |
return $c->render( status => 200, openapi => $city ); |
65 |
return $c->render( status => 200, openapi => _to_api($city->TO_JSON) ); |
| 75 |
} |
66 |
} |
| 76 |
catch { |
67 |
catch { |
| 77 |
if ( $_->isa('DBIx::Class::Exception') ) { |
68 |
if ( $_->isa('DBIx::Class::Exception') ) { |
| 78 |
return $c->render( status => 500, |
69 |
return $c->render( |
| 79 |
openapi => { error => $_->{msg} } ); |
70 |
status => 500, |
|
|
71 |
openapi => { error => $_->{msg} } |
| 72 |
); |
| 80 |
} |
73 |
} |
| 81 |
else { |
74 |
else { |
| 82 |
return $c->render( status => 500, |
75 |
return $c->render( |
| 83 |
openapi => { error => "Something went wrong, check the logs."} ); |
76 |
status => 500, |
|
|
77 |
openapi => { error => "Something went wrong, check the logs." } |
| 78 |
); |
| 84 |
} |
79 |
} |
| 85 |
}; |
80 |
}; |
| 86 |
} |
81 |
} |
|
Lines 88-108
sub add {
Link Here
|
| 88 |
sub update { |
83 |
sub update { |
| 89 |
my $c = shift->openapi->valid_input or return; |
84 |
my $c = shift->openapi->valid_input or return; |
| 90 |
|
85 |
|
| 91 |
my $city; |
86 |
my $city = Koha::Cities->find( $c->validation->param('city_id') ); |
|
|
87 |
|
| 88 |
if ( not defined $city ) { |
| 89 |
return $c->render( status => 404, |
| 90 |
openapi => { error => "Object not found" } ); |
| 91 |
} |
| 92 |
|
92 |
|
| 93 |
return try { |
93 |
return try { |
| 94 |
$city = Koha::Cities->find( $c->validation->param('cityid') ); |
|
|
| 95 |
my $params = $c->req->json; |
94 |
my $params = $c->req->json; |
| 96 |
$city->set( $params ); |
95 |
$city->set( _to_model($params) ); |
| 97 |
$city->store(); |
96 |
$city->store(); |
| 98 |
return $c->render( status => 200, openapi => $city ); |
97 |
return $c->render( status => 200, openapi => _to_api($city->TO_JSON) ); |
| 99 |
} |
98 |
} |
| 100 |
catch { |
99 |
catch { |
| 101 |
if ( not defined $city ) { |
100 |
if ( $_->isa('Koha::Exceptions::Object') ) { |
| 102 |
return $c->render( status => 404, |
|
|
| 103 |
openapi => { error => "Object not found" } ); |
| 104 |
} |
| 105 |
elsif ( $_->isa('Koha::Exceptions::Object') ) { |
| 106 |
return $c->render( status => 500, |
101 |
return $c->render( status => 500, |
| 107 |
openapi => { error => $_->message } ); |
102 |
openapi => { error => $_->message } ); |
| 108 |
} |
103 |
} |
|
Lines 111-135
sub update {
Link Here
|
| 111 |
openapi => { error => "Something went wrong, check the logs."} ); |
106 |
openapi => { error => "Something went wrong, check the logs."} ); |
| 112 |
} |
107 |
} |
| 113 |
}; |
108 |
}; |
| 114 |
|
|
|
| 115 |
} |
109 |
} |
| 116 |
|
110 |
|
| 117 |
sub delete { |
111 |
sub delete { |
| 118 |
my $c = shift->openapi->valid_input or return; |
112 |
my $c = shift->openapi->valid_input or return; |
| 119 |
|
113 |
|
| 120 |
my $city; |
114 |
my $city = Koha::Cities->find( $c->validation->param('city_id') ); |
|
|
115 |
if ( not defined $city ) { |
| 116 |
return $c->render( status => 404, |
| 117 |
openapi => { error => "Object not found" } ); |
| 118 |
} |
| 121 |
|
119 |
|
| 122 |
return try { |
120 |
return try { |
| 123 |
$city = Koha::Cities->find( $c->validation->param('cityid') ); |
|
|
| 124 |
$city->delete; |
121 |
$city->delete; |
| 125 |
return $c->render( status => 200, openapi => "" ); |
122 |
return $c->render( status => 200, openapi => "" ); |
| 126 |
} |
123 |
} |
| 127 |
catch { |
124 |
catch { |
| 128 |
if ( not defined $city ) { |
125 |
if ( $_->isa('DBIx::Class::Exception') ) { |
| 129 |
return $c->render( status => 404, |
|
|
| 130 |
openapi => { error => "Object not found" } ); |
| 131 |
} |
| 132 |
elsif ( $_->isa('DBIx::Class::Exception') ) { |
| 133 |
return $c->render( status => 500, |
126 |
return $c->render( status => 500, |
| 134 |
openapi => { error => $_->{msg} } ); |
127 |
openapi => { error => $_->{msg} } ); |
| 135 |
} |
128 |
} |
|
Lines 138-144
sub delete {
Link Here
|
| 138 |
openapi => { error => "Something went wrong, check the logs."} ); |
131 |
openapi => { error => "Something went wrong, check the logs."} ); |
| 139 |
} |
132 |
} |
| 140 |
}; |
133 |
}; |
|
|
134 |
} |
| 135 |
|
| 136 |
=head3 _to_api |
| 137 |
|
| 138 |
Helper function that maps a hashref of Koha::City attributes into REST api |
| 139 |
attribute names. |
| 140 |
|
| 141 |
=cut |
| 142 |
|
| 143 |
sub _to_api { |
| 144 |
my $city = shift; |
| 141 |
|
145 |
|
|
|
146 |
# Rename attributes |
| 147 |
foreach my $column ( keys %{ $Koha::REST::V1::Cities::to_api_mapping } ) { |
| 148 |
my $mapped_column = $Koha::REST::V1::Cities::to_api_mapping->{$column}; |
| 149 |
if ( exists $city->{ $column } |
| 150 |
&& defined $mapped_column ) |
| 151 |
{ |
| 152 |
# key /= undef |
| 153 |
$city->{ $mapped_column } = delete $city->{ $column }; |
| 154 |
} |
| 155 |
elsif ( exists $city->{ $column } |
| 156 |
&& !defined $mapped_column ) |
| 157 |
{ |
| 158 |
# key == undef => to be deleted |
| 159 |
delete $city->{ $column }; |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
return $city; |
| 142 |
} |
164 |
} |
| 143 |
|
165 |
|
|
|
166 |
=head3 _to_model |
| 167 |
|
| 168 |
Helper function that maps REST api objects into Koha::Cities |
| 169 |
attribute names. |
| 170 |
|
| 171 |
=cut |
| 172 |
|
| 173 |
sub _to_model { |
| 174 |
my $city = shift; |
| 175 |
|
| 176 |
foreach my $attribute ( keys %{ $Koha::REST::V1::Cities::to_model_mapping } ) { |
| 177 |
my $mapped_attribute = $Koha::REST::V1::Cities::to_model_mapping->{$attribute}; |
| 178 |
if ( exists $city->{ $attribute } |
| 179 |
&& defined $mapped_attribute ) |
| 180 |
{ |
| 181 |
# key /= undef |
| 182 |
$city->{ $mapped_attribute } = delete $city->{ $attribute }; |
| 183 |
} |
| 184 |
elsif ( exists $city->{ $attribute } |
| 185 |
&& !defined $mapped_attribute ) |
| 186 |
{ |
| 187 |
# key == undef => to be deleted |
| 188 |
delete $city->{ $attribute }; |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
return $city; |
| 193 |
} |
| 194 |
|
| 195 |
=head2 Global variables |
| 196 |
|
| 197 |
=head3 $to_api_mapping |
| 198 |
|
| 199 |
=cut |
| 200 |
|
| 201 |
our $to_api_mapping = { |
| 202 |
cityid => 'city_id', |
| 203 |
city_country => 'country', |
| 204 |
city_name => 'name', |
| 205 |
city_state => 'state', |
| 206 |
city_zipcode => 'postal_code' |
| 207 |
}; |
| 208 |
|
| 209 |
=head3 $to_model_mapping |
| 210 |
|
| 211 |
=cut |
| 212 |
|
| 213 |
our $to_model_mapping = { |
| 214 |
city_id => 'cityid', |
| 215 |
country => 'city_country', |
| 216 |
name => 'city_name', |
| 217 |
postal_code => 'city_zipcode', |
| 218 |
state => 'city_state' |
| 219 |
}; |
| 220 |
|
| 144 |
1; |
221 |
1; |