Changeset 9 in bookmarks


Ignore:
Timestamp:
03/25/12 15:45:11 (13 years ago)
Author:
peter
Message:
  • use SQL::Interp to generate the SQL for Bookmarks::get_resources()
  • Bookmarks::get_resources() accepts a limit parameter to add a limit to the SQL query
  • added a feed run mode that returns an Atom feed of the most recent 12 bookmarks; can have an optional ?tag parameter that only looks for bookmarks with that tag
  • added an explicit "list" dispatch target that calls the `list run mode
Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/BookmarkApp.pm

    r8 r9  
    3030    $self->run_modes([qw{ 
    3131        list 
     32        feed 
    3233        view 
    3334        edit 
     
    104105        ); 
    105106    } 
     107} 
     108 
     109sub feed { 
     110    my $self = shift; 
     111    my $q = $self->query; 
     112 
     113    my $tag = $q->param('tag'); 
     114 
     115    require XML::Atom::Feed; 
     116    require XML::Atom::Entry; 
     117    require XML::Atom::Link; 
     118 
     119    my $feed = XML::Atom::Feed->new; 
     120    $feed->title('Bookmarks'); 
     121    $feed->id($base_uri . 'feed'); 
     122 
     123    # construct a feed from the most recent 12 bookmarks 
     124    for my $bookmark ($bookmarks->get_resources({ tag => $tag, limit => 12 })) { 
     125        my $entry = XML::Atom::Entry->new; 
     126        $entry->id($bookmark->{bookmark_uri}); 
     127        $entry->title($bookmark->{title}); 
     128        my $link = XML::Atom::Link->new; 
     129        $link->href($bookmark->{uri}); 
     130        $entry->add_link($link); 
     131        $entry->summary('Tags: ' . join(', ', @{ $bookmark->{tags} })); 
     132        $feed->add_entry($entry); 
     133    } 
     134 
     135    $self->header_props( 
     136        -type => 'application/atom+xml', 
     137        -charset => 'UTF-8', 
     138    ); 
     139    return $feed->as_xml; 
    106140} 
    107141 
  • trunk/Bookmarks.pm

    r7 r9  
    22 
    33use Class::Accessor 'antlers'; 
     4use SQL::Interp qw{:all}; 
    45use Bookmark; 
    56 
     
    3637    my $params = shift; 
    3738    my $tag = $params->{tag}; 
    38     my $sth_resource; 
    39     if ($tag) { 
    40         $sth_resource = $self->dbh->prepare('select * from resources join tags on resources.uri = tags.uri join bookmarks on resources.uri = bookmarks.uri where tags.tag = ? order by ctime desc'); 
    41         $sth_resource->execute($tag); 
    42     } else { 
    43         $sth_resource = $self->dbh->prepare('select * from resources join bookmarks on resources.uri = bookmarks.uri order by ctime desc'); 
    44         $sth_resource->execute; 
    45     } 
     39    my $limit = $params->{limit}; 
     40 
     41    my ($sql, @bind) = sql_interp( 
     42        'select * from resources join bookmarks on resources.uri = bookmarks.uri', 
     43        ($tag   ? ('join tags on resources.uri = tags.uri where tags.tag =', \$tag) : ''), 
     44        'order by ctime desc', 
     45        ($limit ? ('limit', \$limit) : ''), 
     46    ); 
     47 
     48    my $sth_resource = $self->dbh->prepare($sql); 
     49    $sth_resource->execute(@bind); 
    4650 
    4751    my $sth_tag = $self->dbh->prepare('select tag from tags where uri = ? order by tag'); 
  • trunk/index.cgi

    r5 r9  
    99    table => [ 
    1010        '[get]'              => { app => 'BookmarkApp', rm => 'list' }, 
     11        'list[get]'          => { app => 'BookmarkApp', rm => 'list' }, 
     12        'feed[get]'          => { app => 'BookmarkApp', rm => 'feed' }, 
    1113        ':id/:field?[get]'   => { app => 'BookmarkApp', rm => 'view' }, 
    1214        ':id?[post]'         => { app => 'BookmarkApp', rm => 'edit' }, 
Note: See TracChangeset for help on using the changeset viewer.