Skip to content

Commit

Permalink
Changed GET call
Browse files Browse the repository at this point in the history
  • Loading branch information
ysyrota committed Dec 4, 2011
1 parent 6a7f99f commit d27cb89
Showing 1 changed file with 27 additions and 16 deletions.
43 changes: 27 additions & 16 deletions expert.pl
Original file line number Diff line number Diff line change
Expand Up @@ -29,24 +29,35 @@ package main;

get '/prices' => sub {
my $self = shift;
my $id = $self->param('id');
if (defined $id) {
my $record = Model::Prices->load($id);
if (defined
$self->render_json(
{
id => $record->id,
seller => $record->seller,
buyer => $record->buyer,
article=> $record->article,
date => $record->date,
price => $record->price,
comment=> $record->comment
}, status => 200);
my @keys = $self->param;
my @result = ();
my $functor = sub {
push @result, {
id => $_->id,
seller => $_->seller,
buyer => $_->buyer,
article => $_->article,
date => $_->date,
price => $_->price,
comment => $_->comment};
};
if ('like' ~~ @keys) {
my $val = Model->dbh->quote('%'.$self->param('like').'%');
Model::Prices->iterate(
"WHERE seller LIKE $val OR buyer LIKE $val" ,
$functor);
} elsif (scalar @keys) {
my %vals;
for my $key (qw(id seller buyer article date price comment)) {
$vals{$key} = $self->param($key) if $key ~~ @keys;
}
Model::Prices->iterate(
'WHERE ' . join(', ', map { "$_ = ?" } keys %vals),
values %vals, $functor);
} else {
$self->render_json({message => 'id is an obligatory parameter'},
status => 400);
Model::Prices->iterate($functor);
}
$self->render_json(\@result);
};

# insertion
Expand Down

0 comments on commit d27cb89

Please sign in to comment.