Skip to content

Instantly share code, notes, and snippets.

@phillipadsmith
Created June 15, 2014 01:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save phillipadsmith/80edef0d39de45ca3de4 to your computer and use it in GitHub Desktop.
Save phillipadsmith/80edef0d39de45ca3de4 to your computer and use it in GitHub Desktop.
Good candidate for a group?
# Get feed items with optional limit or page
get '/items/:limit/:page/:category' =>
{ category => undef, page => 1, limit => 10, } => sub {
my $self = shift;
my $category = $self->stash( 'category' );
my $page = $self->stash( 'page' );
my $limit = $self->stash( 'limit' );
my @items = $self->schema->resultset( 'Item' )->search(
{ 'feed.category' => $category },
{ page => $page, # page to return (defaults to 1)
rows => $limit, # number of results per page
join => [qw/ feed /],
order_by => { -desc => 'pubdate' },
},
);
my @results;
for my $item ( @items ) {
my $doc = {
title => $item->title,
description => $item->description,
pubdate => $item->pubdate,
url => $item->url,
source => $item->feed->name,
source_desc => $item->feed->description,
source_id => $item->feed->id,
image => $item->image,
};
push @results, $doc;
}
$self->respond_to(
json => sub {
$self->render_jsonp( { results => \@results } ), status => 200;
},
any => { text => '', status => 204 }
);
};
# Get feed items with counts, ordered by total
# TODO merge into the previous route
get '/hot/:limit/:page/:category' =>
{ category => undef, page => 1, limit => 10, } => sub {
my $self = shift;
my $category = $self->stash( 'category' );
my $page = $self->stash( 'page' );
my $limit = $self->stash( 'limit' );
my @items = $self->schema->resultset( 'Item' )->search(
{ 'feed.category' => $category },
{ page => $page, # page to return (defaults to 1)
rows => $limit, # number of results per page
join => [qw/ feed /],
order_by => [
{ -desc => 'count_tw + count_fb' }, { -desc => 'pubdate' }
]
}
);
my @results;
for my $item ( @items ) {
my $doc = {
title => $item->title,
description => $item->description,
pubdate => $item->pubdate,
url => $item->url,
source => $item->feed->name,
source_desc => $item->feed->description,
source_id => $item->feed->id,
image => $item->image,
count_tw => $item->count_tw,
count_fb => $item->count_fb,
};
push @results, $doc;
}
$self->respond_to(
json => sub {
$self->render_jsonp( { results => \@results } ), status => 200;
},
any => { text => '', status => 204 }
);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment