[2] | 1 | package Bookmark; |
---|
| 2 | |
---|
[25] | 3 | use Moose; |
---|
[2] | 4 | |
---|
[44] | 5 | has id => ( is => 'ro' ); |
---|
[25] | 6 | has uri => ( is => 'rw' ); |
---|
| 7 | has title => ( is => 'rw' ); |
---|
[44] | 8 | has ctime => ( is => 'ro' ); |
---|
| 9 | has mtime => ( |
---|
| 10 | is => 'ro', |
---|
| 11 | # mtime defaults to ctime |
---|
| 12 | default => sub { $_[0]->ctime }, |
---|
| 13 | lazy => 1, |
---|
| 14 | ); |
---|
[25] | 15 | has tags => ( is => 'rw' ); |
---|
| 16 | has bookmark_uri => ( is => 'rw' ); |
---|
[2] | 17 | |
---|
[25] | 18 | sub BUILD { |
---|
| 19 | my $self = shift; |
---|
| 20 | my $args = shift; |
---|
| 21 | if ($args->{base_uri}) { |
---|
| 22 | $self->bookmark_uri(URI->new_abs($self->id, $args->{base_uri})); |
---|
| 23 | } |
---|
| 24 | } |
---|
| 25 | |
---|
| 26 | sub TO_JSON { |
---|
| 27 | my $self = shift; |
---|
| 28 | return { |
---|
| 29 | id => $self->id, |
---|
| 30 | uri => $self->uri, |
---|
| 31 | title => $self->title, |
---|
| 32 | ctime => $self->ctime, |
---|
| 33 | mtime => $self->mtime, |
---|
| 34 | tags => $self->tags, |
---|
| 35 | ($self->bookmark_uri ? (bookmark_uri => $self->bookmark_uri->canonical->as_string) : ()), |
---|
| 36 | }; |
---|
| 37 | } |
---|
| 38 | |
---|
[45] | 39 | sub update { |
---|
| 40 | my $self = shift; |
---|
| 41 | # TODO: store the dbh somewhere better, or use a more generic "Bookmark Store" object |
---|
| 42 | my $dbh = shift; |
---|
| 43 | my $mtime = time; |
---|
| 44 | |
---|
| 45 | my $changed_uri = 0; |
---|
| 46 | my $sth_current = $dbh->prepare('select uri from bookmarks where id = ?'); |
---|
| 47 | $sth_current->execute($self->id); |
---|
| 48 | my ($stored_uri) = $sth_current->fetchrow_array; |
---|
| 49 | |
---|
| 50 | if ($stored_uri ne $self->uri) { |
---|
| 51 | # the URI has changed |
---|
| 52 | my $sth_update_uri = $dbh->prepare('update resources set uri = ? where uri = ?'); |
---|
| 53 | $sth_update_uri->execute($self->uri, $stored_uri); |
---|
| 54 | $changed_uri++; |
---|
| 55 | } |
---|
| 56 | |
---|
| 57 | # update the title |
---|
| 58 | # TODO: only do this if the title has changed |
---|
| 59 | # TODO: should we update mtime if the title changes? |
---|
| 60 | my $sth_update = $dbh->prepare('update resources set title = ? where uri = ?'); |
---|
| 61 | $sth_update->execute($self->title, $self->uri); |
---|
| 62 | |
---|
| 63 | # update the tags |
---|
| 64 | my $changed_tags = 0; |
---|
| 65 | my %new_tags = map { $_ => 1 } @{ $self->tags }; |
---|
| 66 | my $sth_delete_tag = $dbh->prepare('delete from tags where uri = ? and tag = ?'); |
---|
| 67 | my $sth_insert_tag = $dbh->prepare('insert into tags (uri, tag) values (?, ?)'); |
---|
| 68 | my $sth_current_tags = $dbh->prepare('select tag from tags where uri = ?'); |
---|
| 69 | $sth_current_tags->execute($self->uri); |
---|
| 70 | while (my ($tag) = $sth_current_tags->fetchrow_array) { |
---|
| 71 | if (!$new_tags{$tag}) { |
---|
| 72 | # if a current tag is not in the new tags, remove it from the database |
---|
| 73 | $sth_delete_tag->execute($self->uri, $tag); |
---|
| 74 | $changed_tags++; |
---|
| 75 | } else { |
---|
| 76 | # if a new tag is already in the database, remove it from the list of tags to add |
---|
| 77 | delete $new_tags{$tag}; |
---|
| 78 | } |
---|
| 79 | } |
---|
| 80 | for my $tag (keys %new_tags) { |
---|
| 81 | $sth_insert_tag->execute($self->uri, $tag); |
---|
| 82 | $changed_tags++; |
---|
| 83 | } |
---|
| 84 | |
---|
| 85 | if ($changed_uri or $changed_tags) { |
---|
| 86 | # update the mtime if the bookmark already existed but the tags were changed |
---|
| 87 | my $sth_update = $dbh->prepare('update bookmarks set mtime = ? where uri = ?'); |
---|
| 88 | $sth_update->execute($mtime, $self->uri); |
---|
| 89 | } |
---|
| 90 | |
---|
| 91 | # return the bookmark |
---|
| 92 | return $self; |
---|
| 93 | } |
---|
| 94 | |
---|
[2] | 95 | # module return |
---|
| 96 | 1; |
---|