@ -19,7 +19,6 @@ use Modern::Perl;
use Mojo::Base 'Mojolicious::Controller' ;
use Koha::City ;
use Koha::Cities ;
use Try::Tiny ;
@ -27,16 +26,9 @@ use Try::Tiny;
sub list {
my $ c = shift - > openapi - > valid_input or return ;
my $ cities ;
my $ filter ;
my $ args = $ c - > req - > params - > to_hash ;
for my $ filter_param ( keys %$ args ) {
$ filter - > { $ filter_param } = { LIKE = > $ args - > { $ filter_param } . "%" } ;
}
return try {
$ cities = Koha::Cities - > search ( $ filter ) ;
my $ cities_set = Koha::Cities - > new ;
my $ cities = $ c - > objects - > search ( $ cities_set , \ & _to_model , \ & _to_api ) ;
return $ c - > render ( status = > 200 , openapi = > $ cities ) ;
}
catch {
@ -55,32 +47,35 @@ sub list {
sub get {
my $ c = shift - > openapi - > valid_input or return ;
my $ city = Koha::Cities - > find ( $ c - > validation - > param ( 'cityid' ) ) ;
my $ city = Koha::Cities - > find ( $ c - > validation - > param ( 'city_ id' ) ) ;
unless ( $ city ) {
return $ c - > render ( status = > 404 ,
openapi = > { error = > "City not found" } ) ;
}
return $ c - > render ( status = > 200 , openapi = > $ city ) ;
return $ c - > render ( status = > 200 , openapi = > _to_api ( $ city - > TO_JSON ) ) ;
}
sub add {
my $ c = shift - > openapi - > valid_input or return ;
my $ city = Koha::City - > new ( $ c - > validation - > param ( 'body' ) ) ;
return try {
my $ city = Koha::City - > new ( _to_model ( $ c - > validation - > param ( 'body' ) ) ) ;
$ city - > store ;
return $ c - > render ( status = > 200 , openapi = > $ city ) ;
return $ c - > render ( status = > 200 , openapi = > _to_api ( $ city - > TO_JSON ) ) ;
}
catch {
if ( $ _ - > isa ( 'DBIx::Class::Exception' ) ) {
return $ c - > render ( status = > 500 ,
openapi = > { error = > $ _ - > { msg } } ) ;
return $ c - > render (
status = > 500 ,
openapi = > { error = > $ _ - > { msg } }
) ;
}
else {
return $ c - > render ( status = > 500 ,
openapi = > { error = > "Something went wrong, check the logs." } ) ;
return $ c - > render (
status = > 500 ,
openapi = > { error = > "Something went wrong, check the logs." }
) ;
}
} ;
}
@ -88,21 +83,21 @@ sub add {
sub update {
my $ c = shift - > openapi - > valid_input or return ;
my $ city ;
my $ city = Koha::Cities - > find ( $ c - > validation - > param ( 'city_id' ) ) ;
if ( not defined $ city ) {
return $ c - > render ( status = > 404 ,
openapi = > { error = > "Object not found" } ) ;
}
return try {
$ city = Koha::Cities - > find ( $ c - > validation - > param ( 'cityid' ) ) ;
my $ params = $ c - > req - > json ;
$ city - > set ( $ params ) ;
$ city - > set ( _to_model ( $ params ) ) ;
$ city - > store ( ) ;
return $ c - > render ( status = > 200 , openapi = > $ city ) ;
return $ c - > render ( status = > 200 , openapi = > _to_api ( $ city - > TO_JSON ) ) ;
}
catch {
if ( not defined $ city ) {
return $ c - > render ( status = > 404 ,
openapi = > { error = > "Object not found" } ) ;
}
elsif ( $ _ - > isa ( 'Koha::Exceptions::Object' ) ) {
if ( $ _ - > isa ( 'Koha::Exceptions::Object' ) ) {
return $ c - > render ( status = > 500 ,
openapi = > { error = > $ _ - > message } ) ;
}
@ -111,25 +106,23 @@ sub update {
openapi = > { error = > "Something went wrong, check the logs." } ) ;
}
} ;
}
sub delete {
my $ c = shift - > openapi - > valid_input or return ;
my $ city ;
my $ city = Koha::Cities - > find ( $ c - > validation - > param ( 'city_id' ) ) ;
if ( not defined $ city ) {
return $ c - > render ( status = > 404 ,
openapi = > { error = > "Object not found" } ) ;
}
return try {
$ city = Koha::Cities - > find ( $ c - > validation - > param ( 'cityid' ) ) ;
$ city - > delete ;
return $ c - > render ( status = > 200 , openapi = > "" ) ;
}
catch {
if ( not defined $ city ) {
return $ c - > render ( status = > 404 ,
openapi = > { error = > "Object not found" } ) ;
}
elsif ( $ _ - > isa ( 'DBIx::Class::Exception' ) ) {
if ( $ _ - > isa ( 'DBIx::Class::Exception' ) ) {
return $ c - > render ( status = > 500 ,
openapi = > { error = > $ _ - > { msg } } ) ;
}
@ -138,7 +131,91 @@ sub delete {
openapi = > { error = > "Something went wrong, check the logs." } ) ;
}
} ;
}
= head3 _to_api
Helper function that maps a hashref of Koha:: City attributes into REST api
attribute names .
= cut
sub _to_api {
my $ city = shift ;
# Rename attributes
foreach my $ column ( keys % { $ Koha:: REST:: V1:: Cities:: to_api_mapping } ) {
my $ mapped_column = $ Koha:: REST:: V1:: Cities:: to_api_mapping - > { $ column } ;
if ( exists $ city - > { $ column }
&& defined $ mapped_column )
{
# key /= undef
$ city - > { $ mapped_column } = delete $ city - > { $ column } ;
}
elsif ( exists $ city - > { $ column }
&& ! defined $ mapped_column )
{
# key == undef => to be deleted
delete $ city - > { $ column } ;
}
}
return $ city ;
}
= head3 _to_model
Helper function that maps REST api objects into Koha:: Cities
attribute names .
= cut
sub _to_model {
my $ city = shift ;
foreach my $ attribute ( keys % { $ Koha:: REST:: V1:: Cities:: to_model_mapping } ) {
my $ mapped_attribute = $ Koha:: REST:: V1:: Cities:: to_model_mapping - > { $ attribute } ;
if ( exists $ city - > { $ attribute }
&& defined $ mapped_attribute )
{
# key /= undef
$ city - > { $ mapped_attribute } = delete $ city - > { $ attribute } ;
}
elsif ( exists $ city - > { $ attribute }
&& ! defined $ mapped_attribute )
{
# key == undef => to be deleted
delete $ city - > { $ attribute } ;
}
}
return $ city ;
}
= head2 Global variables
= head3 $ to_api_mapping
= cut
our $ to_api_mapping = {
cityid = > 'city_id' ,
city_country = > 'country' ,
city_name = > 'name' ,
city_state = > 'state' ,
city_zipcode = > 'postal_code'
} ;
= head3 $ to_model_mapping
= cut
our $ to_model_mapping = {
city_id = > 'cityid' ,
country = > 'city_country' ,
name = > 'city_name' ,
postal_code = > 'city_zipcode' ,
state = > 'city_state'
} ;
1 ;