@ -25,11 +25,11 @@ use Koha::Cities;
use Try::Tiny ;
use Try::Tiny ;
sub list {
sub list {
my ( $ c , $ args , $ cb ) = @ _ ;
my $ c = shift - > openapi - > valid_input or return ;
my $ cities ;
my $ cities ;
my $ filter ;
my $ filter ;
$ args // = { } ;
my $ args = $ c - > req - > params - > to_hash ;
for my $ filter_param ( keys %$ args ) {
for my $ filter_param ( keys %$ args ) {
$ filter - > { $ filter_param } = { LIKE = > $ args - > { $ filter_param } . "%" } ;
$ filter - > { $ filter_param } = { LIKE = > $ args - > { $ filter_param } . "%" } ;
@ -37,96 +37,105 @@ sub list {
return try {
return try {
$ cities = Koha::Cities - > search ( $ filter ) ;
$ cities = Koha::Cities - > search ( $ filter ) ;
return $ c - > $ cb ( $ cities , 200 ) ;
return $ c - > render ( status = > 200 , openapi = > $ cities ) ;
}
}
catch {
catch {
if ( $ _ - > isa ( 'DBIx::Class::Exception' ) ) {
if ( $ _ - > isa ( 'DBIx::Class::Exception' ) ) {
return $ c - > $ cb ( { error = > $ _ - > { msg } } , 500 ) ;
return $ c - > render ( status = > 500 ,
openapi = > { error = > $ _ - > msg } ) ;
}
}
else {
else {
return $ c - > $ cb (
return $ c - > render ( status = > 500 ,
{ error = > "Something went wrong, check the logs." } , 500 ) ;
openapi = > { error = > "Something went wrong, check the logs." } ) ;
}
}
} ;
} ;
}
}
sub get {
sub get {
my ( $ c , $ args , $ cb ) = @ _ ;
my $ c = shift - > openapi - > valid_input or return ;
my $ city = Koha::Cities - > find ( $ args - > { cityid } ) ;
my $ city = Koha::Cities - > find ( $ c - > validation - > param ( 'cityid' ) ) ;
unless ( $ city ) {
unless ( $ city ) {
return $ c - > $ cb ( { error = > "City not found" } , 404 ) ;
return $ c - > render ( status = > 404 ,
openapi = > { error = > "City not found" } ) ;
}
}
return $ c - > $ cb ( $ city , 200 ) ;
return $ c - > render ( status = > 200 , openapi = > $ city ) ;
}
}
sub add {
sub add {
my ( $ c , $ args , $ cb ) = @ _ ;
my $ c = shift - > openapi - > valid_input or return ;
my $ city = Koha::City - > new ( $ args - > { body } ) ;
my $ city = Koha::City - > new ( $ c - > validation - > param ( 'body' ) ) ;
return try {
return try {
$ city - > store ;
$ city - > store ;
return $ c - > $ cb ( $ city , 200 ) ;
return $ c - > render ( status = > 200 , openapi = > $ city ) ;
}
}
catch {
catch {
if ( $ _ - > isa ( 'DBIx::Class::Exception' ) ) {
if ( $ _ - > isa ( 'DBIx::Class::Exception' ) ) {
return $ c - > $ cb ( { error = > $ _ - > msg } , 500 ) ;
return $ c - > render ( status = > 500 ,
openapi = > { error = > $ _ - > message } ) ;
}
}
else {
else {
return $ c - > $ cb (
return $ c - > render ( status = > 500 ,
{ error = > "Something went wrong, check the logs." } , 500 ) ;
openapi = > { error = > "Something went wrong, check the logs." } ) ;
}
}
} ;
} ;
}
}
sub update {
sub update {
my ( $ c , $ args , $ cb ) = @ _ ;
my $ c = shift - > openapi - > valid_input or return ;
my $ city ;
my $ city ;
return try {
return try {
$ city = Koha::Cities - > find ( $ args - > { cityid } ) ;
$ city = Koha::Cities - > find ( $ c - > validation - > param ( 'cityid' ) ) ;
$ city - > set ( $ args - > { body } ) ;
my $ params = $ c - > req - > json ;
$ city - > set ( $ params ) ;
$ city - > store ( ) ;
$ city - > store ( ) ;
return $ c - > $ cb ( $ city , 200 ) ;
return $ c - > render ( status = > 200 , openapi = > $ city ) ;
}
}
catch {
catch {
if ( not defined $ city ) {
if ( not defined $ city ) {
return $ c - > $ cb ( { error = > "Object not found" } , 404 ) ;
return $ c - > render ( status = > 404 ,
openapi = > { error = > "Object not found" } ) ;
}
}
elsif ( $ _ - > isa ( 'Koha::Exceptions::Object' ) ) {
elsif ( $ _ - > isa ( 'Koha::Exceptions::Object' ) ) {
return $ c - > $ cb ( { error = > $ _ - > message } , 500 ) ;
return $ c - > render ( status = > 500 ,
openapi = > { error = > $ _ - > message } ) ;
}
}
else {
else {
return $ c - > $ cb (
return $ c - > render ( status = > 500 ,
{ error = > "Something went wrong, check the logs." } , 500 ) ;
openapi = > { error = > "Something went wrong, check the logs." } ) ;
}
}
} ;
} ;
}
}
sub delete {
sub delete {
my ( $ c , $ args , $ cb ) = @ _ ;
my $ c = shift - > openapi - > valid_input or return ;
my $ city ;
my $ city ;
return try {
return try {
$ city = Koha::Cities - > find ( $ args - > { cityid } ) ;
$ city = Koha::Cities - > find ( $ c - > validation - > param ( 'cityid' ) ) ;
$ city - > delete ;
$ city - > delete ;
return $ c - > $ cb ( "" , 200 ) ;
return $ c - > render ( status = > 200 , openapi = > "" ) ;
}
}
catch {
catch {
if ( not defined $ city ) {
if ( not defined $ city ) {
return $ c - > $ cb ( { error = > "Object not found" } , 404 ) ;
return $ c - > render ( status = > 404 ,
openapi = > { error = > "Object not found" } ) ;
}
}
elsif ( $ _ - > isa ( 'DBIx::Class::Exception' ) ) {
elsif ( $ _ - > isa ( 'DBIx::Class::Exception' ) ) {
return $ c - > $ cb ( { error = > $ _ - > msg } , 500 ) ;
return $ c - > render ( status = > 500 ,
openapi = > { error = > $ _ - > msg } ) ;
}
}
else {
else {
return $ c - > $ cb (
return $ c - > render ( status = > 500 ,
{ error = > "Something went wrong, check the logs." } , 500 ) ;
openapi = > { error = > "Something went wrong, check the logs." } ) ;
}
}
} ;
} ;