Skip to content

Commit

Permalink
add stock filter
Browse files Browse the repository at this point in the history
  • Loading branch information
Xiaonan Song committed Feb 2, 2013
1 parent abee8a7 commit 0ffe961
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
1 change: 1 addition & 0 deletions Makefile.PL
Expand Up @@ -34,6 +34,7 @@ requires 'Image::OCR::Tesseract';
requires 'Business::ISBN';
requires 'Date::Calc';
requires 'Catalyst::Helper::View::JSON';
requires 'AnyEvent::HTTP';

test_requires 'Test::More' => '0.88';
catalyst;
Expand Down
11 changes: 11 additions & 0 deletions lib/Ptt/Controller/Search.pm
Expand Up @@ -27,6 +27,17 @@ sub search : Chained("/") : PathPart("search") : Args(0) {
$qh->{p1} = $c->req->params->{p1};
$qh->{p2} = $c->req->params->{p2};

# NOTE: stock is 0, include: all
# stock is 1, only include: in stock
# stock is 2, only include: out of stock
# stock is 3, only include: pre order

if ( defined $c->req->params->{stock} && $c->req->params->{stock} ne '' ) {
$qh->{stock} = $c->req->params->{stock};
} else {
$qh->{stock} = 1;
}

my $data = $c->model("API::Search")->search($qh, $sort)->recv;

my $total_results = $data->{response}->{numFound};
Expand Down
13 changes: 13 additions & 0 deletions lib/Ptt/Model/API/Search.pm
Expand Up @@ -33,6 +33,19 @@ sub search {
$path .= "&fq=" . uri_escape($pr);
}

my $stock;
if ( $qh->{stock} == 1 ) {
$stock = 'available:"in stock"';
} elsif ( $qh->{stock} == 2 ) {
$stock = 'available:"out of stock"';
} elsif ( $qh->{stock} == 3 ) {
$stock = 'available:"pre order"';
}

if ( $stock ) {
$path .= "&fq=" . uri_escape($stock);
}

if ( $qh->{p} && $qh->{page_size} ) {
$path .= "&rows=$qh->{page_size}&start=" . ($qh->{page_size}*($qh->{p}-1));
}
Expand Down

0 comments on commit 0ffe961

Please sign in to comment.