Changeset 71 in bookmarks for trunk/lib


Ignore:
Timestamp:
02/13/14 16:10:42 (11 years ago)
Author:
peter
Message:
  • added a Bookmarks::Search class that encapsulates the query, tags, limit, and offset values for a given search
  • Bookmarks::List now has a search attribute that takes a Bookmarks::Search object instead of the individual attributes from the search
  • Bookmarks::get_cotags() takes a Bookmarks::Search object as its search parameter instead of discrete tags and query parameters
  • changed the name of the Bookmarks::get_bookmarks() parameter from tag to tags to align with the rest of the backend APIs
Location:
trunk/lib
Files:
1 added
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/lib/Bookmarks.pm

    r70 r71  
    22 
    33use Moose; 
     4 
    45use SQL::Interp qw{:all}; 
    56use URI; 
     7 
    68use Bookmark; 
    79use Bookmarks::List; 
     10use Bookmarks::Search; 
    811 
    912has dbh      => ( is => 'rw' ); 
     
    7376    my $self = shift; 
    7477    my $params = shift; 
    75     my $query = $params->{query}; 
    76     my $tags = $params->{tag} || []; 
    77     my $limit = $params->{limit}; 
    78     my $offset = $params->{offset}; 
     78    my $search = Bookmarks::Search->new($params); 
    7979 
    8080    # build the query 
    8181    my @sql; 
    8282 
    83     if (!ref $tags) { 
    84         $tags = [ $tags ]; 
    85     } 
    86     if (@$tags) { 
     83    if (@{ $search->tags }) { 
    8784        my $intersect = 0; 
    88         for my $tag (@{ $tags }) { 
     85        for my $tag (@{ $search->tags }) { 
    8986            push @sql, 'intersect' if $intersect; 
    9087            push @sql, 'select resources.*, bookmarks.* from resources join bookmarks on resources.uri = bookmarks.uri'; 
     
    9592        push @sql, 'select * from resources join bookmarks on resources.uri = bookmarks.uri'; 
    9693    } 
    97     if ($query) { 
    98         push @sql, (@$tags ? 'and' : 'where'), 'title like', \"%$query%"; 
     94    if ($search->query) { 
     95        my $fuzzy_match = '%' . $search->query . '%'; 
     96        push @sql, (@{ $search->tags } ? 'and' : 'where'), 'title like', \$fuzzy_match; 
    9997    } 
    10098    push @sql, 'order by ctime desc'; 
    101     push @sql, ('limit', \$limit) if $limit; 
     99    push @sql, ('limit', \$search->limit) if $search->limit; 
    102100    # an offset is only allowed if we have a limit clause 
    103     push @sql, ('offset', \$offset) if $limit && $offset; 
     101    push @sql, ('offset', \$search->offset) if $search->limit && $search->offset; 
    104102 
    105103    my ($sql, @bind) = sql_interp(@sql); 
    106     #die $sql; 
    107104 
    108105    my $sth_resource = $self->dbh->prepare($sql); 
     
    119116    return Bookmarks::List->new({ 
    120117        bookmarks => $self, 
    121         tags      => $tags, 
    122         query     => $query, 
    123         limit     => $limit, 
    124         offset    => $offset, 
     118        search    => $search, 
    125119        results   => \@resources, 
    126120    }); 
     
    147141    my $self = shift; 
    148142    my $params = shift; 
    149     my $query = $params->{query}; 
    150     my $tags = $params->{tag} || []; 
    151     if (!ref $tags) { 
    152         $tags = [ $tags ]; 
    153     } 
     143    my $search = $params->{search}; 
     144 
    154145    my @sql; 
    155146 
    156147    push @sql, 'select tag, count(tag) as count from tags'; 
    157     push @sql, 'join resources on tags.uri = resources.uri' if $query; 
     148    push @sql, 'join resources on tags.uri = resources.uri' if $search->query; 
    158149 
    159150    # build the where clause 
    160     if (@$tags) { 
     151    if (@{ $search->tags }) { 
    161152        push @sql, 'where tags.uri in ('; 
    162153        my $intersect = 0; 
    163         for my $tag (@{ $tags }) { 
     154        for my $tag (@{ $search->tags }) { 
    164155            push @sql, 'intersect' if $intersect; 
    165156            push @sql, 'select uri from tags where tag = ', \$tag; 
    166157            $intersect++; 
    167158        } 
    168         push @sql, ') and tag not in ', $tags, ''; 
    169     } 
    170     if ($query) { 
    171         push @sql, (@$tags ? 'and' : 'where'), 'title like', \"%$query%"; 
     159        push @sql, ') and tag not in ', $search->tags, ''; 
     160    } 
     161    if ($search->query) { 
     162        my $fuzzy_match = '%' . $search->query . '%'; 
     163        push @sql, (@{ $search->tags } ? 'and' : 'where'), 'title like', \$fuzzy_match; 
    172164    } 
    173165 
  • trunk/lib/Bookmarks/Controller.pm

    r70 r71  
    8585    my $list = $self->bookmarks->get_bookmarks({ 
    8686        query  => $query, 
    87         tag    => \@tags, 
     87        tags   => \@tags, 
    8888        limit  => $limit, 
    8989        offset => $offset, 
     
    104104 
    105105    # construct a feed from the most recent 12 bookmarks 
    106     my $list = $self->bookmarks->get_bookmarks({ query => $query, tag => \@tags, limit => 12 }); 
     106    my $list = $self->bookmarks->get_bookmarks({ query => $query, tags => \@tags, limit => 12 }); 
    107107    return $list->as_atom; 
    108108} 
  • trunk/lib/Bookmarks/List.pm

    r70 r71  
    66use HTTP::Date qw{time2iso time2isoz}; 
    77 
    8 has bookmarks => (is => 'ro'); 
    9 has query  => (is => 'ro'); 
    10 has tags   => ( 
     8has bookmarks => ( 
    119    is => 'ro', 
    12     default => sub { [] }, 
     10    isa => 'Bookmarks', 
    1311); 
    14 has limit  => (is => 'ro'); 
    15 has offset => (is => 'ro'); 
     12has search => ( 
     13    is => 'ro', 
     14    isa => 'Bookmarks::Search', 
     15    handles => [qw{query tags limit offset}], 
     16); 
    1617has results => ( is => 'ro' ); 
    1718has title => ( 
     
    182183 
    183184    my @all_tags = $self->bookmarks->get_tags({ selected => @{ $self->tags }[0] }); 
    184     my @cotags = $self->bookmarks->get_cotags({ 
    185         query  => $self->query, 
    186         tag    => $self->tags, 
    187     }); 
     185    my @cotags = $self->bookmarks->get_cotags({ search => $self->search }); 
    188186 
    189187    $template->process( 
Note: See TracChangeset for help on using the changeset viewer.