Changeset 46 in bookmarks


Ignore:
Timestamp:
06/03/13 17:57:19 (12 years ago)
Author:
peter
Message:
  • moved the update method from Bookmark to Bookmarks
  • added a utility helper method _update_tags that is called by add and update in the Bookmarks class
Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/Bookmark.pm

    r45 r46  
    3737} 
    3838 
    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  
    9539# module return 
    96401; 
  • trunk/BookmarkApp.pm

    r45 r46  
    401401        $bookmark->title($q->param('title')); 
    402402        $bookmark->tags([ split(' ', $q->param('tags')) ]); 
    403         $bookmark->update($self->_bookmarks->dbh); 
     403 
     404        # write to the database 
     405        $self->_bookmarks->update($bookmark); 
    404406 
    405407        # return to the form 
  • trunk/Bookmarks.pm

    r28 r46  
    193193    } 
    194194 
     195    my $changed_tags = $self->_update_tags($uri, $bookmark->{tags}); 
     196 
     197    if ($bookmark_exists && $changed_tags) { 
     198        # update the mtime if the bookmark already existed but the tags were changed 
     199        my $sth_update = $self->dbh->prepare('update bookmarks set mtime = ? where uri = ?'); 
     200        $sth_update->execute($mtime, $uri); 
     201    } 
     202 
     203    # return the newly created or updated bookmark 
     204    return $self->get_bookmark({ uri => $uri }); 
     205} 
     206 
     207sub update { 
     208    my $self = shift; 
     209    my $bookmark = shift; 
     210 
     211    my $mtime = time; 
     212 
     213    my $changed_uri = 0; 
     214    my $sth_current = $self->dbh->prepare('select uri from bookmarks where id = ?'); 
     215    $sth_current->execute($bookmark->id); 
     216    my ($stored_uri) = $sth_current->fetchrow_array; 
     217 
     218    if ($stored_uri ne $bookmark->uri) { 
     219        # the URI has changed 
     220        my $sth_update_uri = $self->dbh->prepare('update resources set uri = ? where uri = ?'); 
     221        $sth_update_uri->execute($bookmark->uri, $stored_uri); 
     222        $changed_uri++; 
     223    } 
     224 
     225    # update the title 
     226    # TODO: only do this if the title has changed 
     227    # TODO: should we update mtime if the title changes? 
     228    my $sth_update = $self->dbh->prepare('update resources set title = ? where uri = ?'); 
     229    $sth_update->execute($bookmark->title, $bookmark->uri); 
     230 
     231    my $changed_tags = $self->_update_tags($bookmark->uri, $bookmark->tags); 
     232 
     233    if ($changed_uri or $changed_tags) { 
     234        # update the mtime if the bookmark already existed but the tags were changed 
     235        my $sth_update = $self->dbh->prepare('update bookmarks set mtime = ? where uri = ?'); 
     236        $sth_update->execute($mtime, $bookmark->uri); 
     237    } 
     238 
     239    # return the bookmark 
     240    return $bookmark; 
     241} 
     242 
     243sub _update_tags { 
     244    my $self = shift; 
     245    my ($uri, $tags) = @_; 
     246 
    195247    my $changed_tags = 0; 
    196     my %new_tags = map { $_ => 1 } @{ $bookmark->{tags} }; 
     248    my %new_tags = map { $_ => 1 } @{ $tags }; 
    197249    my $sth_delete_tag = $self->dbh->prepare('delete from tags where uri = ? and tag = ?'); 
    198250    my $sth_insert_tag = $self->dbh->prepare('insert into tags (uri, tag) values (?, ?)'); 
     
    214266    } 
    215267 
    216     if ($bookmark_exists && $changed_tags) { 
    217         # update the mtime if the bookmark already existed but the tags were changed 
    218         my $sth_update = $self->dbh->prepare('update bookmarks set mtime = ? where uri = ?'); 
    219         $sth_update->execute($mtime, $uri); 
    220     } 
    221  
    222     # return the newly created or updated bookmark 
    223     return $self->get_bookmark({ uri => $uri }); 
    224 } 
     268    # how many tags have changed? 
     269    return $changed_tags; 
     270} 
     271 
    225272 
    226273# module returns true 
Note: See TracChangeset for help on using the changeset viewer.