Changeset 25 in bookmarks
- Timestamp:
- 05/23/13 00:37:57 (11 years ago)
- Location:
- trunk
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Bookmark.pm
r2 r25 1 1 package Bookmark; 2 2 3 use Class::Accessor qw{antlers};3 use Moose; 4 4 5 has id => ( is => 'ro' ); 6 has uri => ( is => 'ro' ); 7 has ctime => ( is => 'ro' ); 8 has mtime => ( is => 'ro' ); 5 has id => ( is => 'rw' ); 6 has uri => ( is => 'rw' ); 7 has title => ( is => 'rw' ); 8 has ctime => ( is => 'rw' ); 9 has mtime => ( is => 'rw' ); 10 has tags => ( is => 'rw' ); 11 has bookmark_uri => ( is => 'rw' ); 12 13 sub BUILD { 14 my $self = shift; 15 my $args = shift; 16 if ($args->{base_uri}) { 17 $self->bookmark_uri(URI->new_abs($self->id, $args->{base_uri})); 18 } 19 } 20 21 sub TO_JSON { 22 my $self = shift; 23 return { 24 id => $self->id, 25 uri => $self->uri, 26 title => $self->title, 27 ctime => $self->ctime, 28 mtime => $self->mtime, 29 tags => $self->tags, 30 ($self->bookmark_uri ? (bookmark_uri => $self->bookmark_uri->canonical->as_string) : ()), 31 }; 32 } 9 33 10 34 # module return -
trunk/BookmarkApp.pm
r24 r25 90 90 ); 91 91 return decode_utf8( 92 encode_json({92 JSON->new->utf8->convert_blessed->encode({ 93 93 bookmarks => \@resources, 94 94 }) -
trunk/Bookmarks.pm
r24 r25 8 8 has dbh => ( is => 'rw' ); 9 9 has base_uri => ( is => 'ro', isa => 'URI' ); 10 11 has _sth_tags_from_uri => ( 12 is => 'ro', 13 init_arg => undef, 14 lazy => 1, 15 default => sub { $_[0]->dbh->prepare('select tag from tags where uri = ? order by tag'); }, 16 ); 10 17 11 18 sub BUILD { … … 29 36 my $self = shift; 30 37 my $params = shift; 38 39 # look for bookmark by id or uri 31 40 my $sth; 32 41 if ($params->{id}) { … … 40 49 } 41 50 my $bookmark = $sth->fetchrow_hashref; 42 if ($bookmark) { 43 my $sth_tag = $self->dbh->prepare('select tag from tags where uri = ? order by tag'); 44 $sth_tag->execute($bookmark->{uri}); 45 $bookmark->{tags} = [ map { $$_[0] } @{ $sth_tag->fetchall_arrayref } ]; 46 if ($self->base_uri) { 47 $bookmark->{bookmark_uri} = URI->new_abs($bookmark->{id}, $self->base_uri); 48 } 49 } 50 return $bookmark; 51 return unless $bookmark; 52 53 return Bookmark->new({ 54 %$bookmark, 55 tags => [ $self->get_tags({ uri => $bookmark->{uri} }) ], 56 base_uri => $self->base_uri, 57 }); 51 58 } 52 59 … … 85 92 $sth_resource->execute(@bind); 86 93 87 my $sth_tag = $self->dbh->prepare('select tag from tags where uri = ? order by tag');88 94 my @resources; 89 95 while (my $resource = $sth_resource->fetchrow_hashref) { 90 $sth_tag->execute($resource->{uri}); 91 $resource->{tags} = [ map { $$_[0] } @{ $sth_tag->fetchall_arrayref } ]; 92 if ($self->base_uri) { 93 $resource->{bookmark_uri} = URI->new_abs($resource->{id}, $self->base_uri); 94 } 95 push @resources, $resource; 96 push @resources, Bookmark->new({ 97 %$resource, 98 tags => [ $self->get_tags({ uri => $resource->{uri} }) ], 99 base_uri => $self->base_uri, 100 }); 96 101 } 97 102 return @resources; … … 101 106 my $self = shift; 102 107 my $params = shift; 103 my $tag = $params->{selected}; 104 my $sth_all_tags = $self->dbh->prepare('select tag, count(tag) as count, tag = ? as selected from tags group by tag order by tag'); 105 $sth_all_tags->execute($tag); 106 my $all_tags = $sth_all_tags->fetchall_arrayref({}); 107 return @{ $all_tags }; 108 if (my $uri = $params->{uri}) { 109 # get the tags for a particular URI 110 $self->_sth_tags_from_uri->execute($uri); 111 return map { $$_[0] } @{ $self->_sth_tags_from_uri->fetchall_arrayref }; 112 } else { 113 # return all tags 114 my $tag = $params->{selected}; 115 my $sth_all_tags = $self->dbh->prepare('select tag, count(tag) as count, tag = ? as selected from tags group by tag order by tag'); 116 $sth_all_tags->execute($tag); 117 my $all_tags = $sth_all_tags->fetchall_arrayref({}); 118 return @{ $all_tags }; 119 } 108 120 } 109 121
Note: See TracChangeset
for help on using the changeset viewer.