Index: /trunk/Bookmark.pm
===================================================================
--- /trunk/Bookmark.pm	(revision 45)
+++ /trunk/Bookmark.pm	(revision 46)
@@ -37,60 +37,4 @@
 }
 
-sub update {
-    my $self = shift;
-    # TODO: store the dbh somewhere better, or use a more generic "Bookmark Store" object
-    my $dbh = shift;
-    my $mtime = time;
-
-    my $changed_uri = 0;
-    my $sth_current = $dbh->prepare('select uri from bookmarks where id = ?');
-    $sth_current->execute($self->id);
-    my ($stored_uri) = $sth_current->fetchrow_array;
-
-    if ($stored_uri ne $self->uri) {
-        # the URI has changed
-        my $sth_update_uri = $dbh->prepare('update resources set uri = ? where uri = ?');
-        $sth_update_uri->execute($self->uri, $stored_uri);
-        $changed_uri++;
-    }
-
-    # update the title
-    # TODO: only do this if the title has changed
-    # TODO: should we update mtime if the title changes?
-    my $sth_update = $dbh->prepare('update resources set title = ? where uri = ?');
-    $sth_update->execute($self->title, $self->uri);
-
-    # update the tags
-    my $changed_tags = 0;
-    my %new_tags = map { $_ => 1 } @{ $self->tags };
-    my $sth_delete_tag = $dbh->prepare('delete from tags where uri = ? and tag = ?');
-    my $sth_insert_tag = $dbh->prepare('insert into tags (uri, tag) values (?, ?)');
-    my $sth_current_tags = $dbh->prepare('select tag from tags where uri = ?');
-    $sth_current_tags->execute($self->uri);
-    while (my ($tag) = $sth_current_tags->fetchrow_array) {
-        if (!$new_tags{$tag}) {
-            # if a current tag is not in the new tags, remove it from the database
-            $sth_delete_tag->execute($self->uri, $tag);
-            $changed_tags++;
-        } else {
-            # if a new tag is already in the database, remove it from the list of tags to add
-            delete $new_tags{$tag};
-        }
-    }
-    for my $tag (keys %new_tags) {
-        $sth_insert_tag->execute($self->uri, $tag);
-        $changed_tags++;
-    }
-
-    if ($changed_uri or $changed_tags) {
-        # update the mtime if the bookmark already existed but the tags were changed
-        my $sth_update = $dbh->prepare('update bookmarks set mtime = ? where uri = ?');
-        $sth_update->execute($mtime, $self->uri);
-    }
-
-    # return the bookmark
-    return $self;
-}
-
 # module return
 1;
Index: /trunk/BookmarkApp.pm
===================================================================
--- /trunk/BookmarkApp.pm	(revision 45)
+++ /trunk/BookmarkApp.pm	(revision 46)
@@ -401,5 +401,7 @@
         $bookmark->title($q->param('title'));
         $bookmark->tags([ split(' ', $q->param('tags')) ]);
-        $bookmark->update($self->_bookmarks->dbh);
+
+        # write to the database
+        $self->_bookmarks->update($bookmark);
 
         # return to the form
Index: /trunk/Bookmarks.pm
===================================================================
--- /trunk/Bookmarks.pm	(revision 45)
+++ /trunk/Bookmarks.pm	(revision 46)
@@ -193,6 +193,58 @@
     }
 
+    my $changed_tags = $self->_update_tags($uri, $bookmark->{tags});
+
+    if ($bookmark_exists && $changed_tags) {
+        # update the mtime if the bookmark already existed but the tags were changed
+        my $sth_update = $self->dbh->prepare('update bookmarks set mtime = ? where uri = ?');
+        $sth_update->execute($mtime, $uri);
+    }
+
+    # return the newly created or updated bookmark
+    return $self->get_bookmark({ uri => $uri });
+}
+
+sub update {
+    my $self = shift;
+    my $bookmark = shift;
+
+    my $mtime = time;
+
+    my $changed_uri = 0;
+    my $sth_current = $self->dbh->prepare('select uri from bookmarks where id = ?');
+    $sth_current->execute($bookmark->id);
+    my ($stored_uri) = $sth_current->fetchrow_array;
+
+    if ($stored_uri ne $bookmark->uri) {
+        # the URI has changed
+        my $sth_update_uri = $self->dbh->prepare('update resources set uri = ? where uri = ?');
+        $sth_update_uri->execute($bookmark->uri, $stored_uri);
+        $changed_uri++;
+    }
+
+    # update the title
+    # TODO: only do this if the title has changed
+    # TODO: should we update mtime if the title changes?
+    my $sth_update = $self->dbh->prepare('update resources set title = ? where uri = ?');
+    $sth_update->execute($bookmark->title, $bookmark->uri);
+
+    my $changed_tags = $self->_update_tags($bookmark->uri, $bookmark->tags);
+
+    if ($changed_uri or $changed_tags) {
+        # update the mtime if the bookmark already existed but the tags were changed
+        my $sth_update = $self->dbh->prepare('update bookmarks set mtime = ? where uri = ?');
+        $sth_update->execute($mtime, $bookmark->uri);
+    }
+
+    # return the bookmark
+    return $bookmark;
+}
+
+sub _update_tags {
+    my $self = shift;
+    my ($uri, $tags) = @_;
+
     my $changed_tags = 0;
-    my %new_tags = map { $_ => 1 } @{ $bookmark->{tags} };
+    my %new_tags = map { $_ => 1 } @{ $tags };
     my $sth_delete_tag = $self->dbh->prepare('delete from tags where uri = ? and tag = ?');
     my $sth_insert_tag = $self->dbh->prepare('insert into tags (uri, tag) values (?, ?)');
@@ -214,13 +266,8 @@
     }
 
-    if ($bookmark_exists && $changed_tags) {
-        # update the mtime if the bookmark already existed but the tags were changed
-        my $sth_update = $self->dbh->prepare('update bookmarks set mtime = ? where uri = ?');
-        $sth_update->execute($mtime, $uri);
-    }
-
-    # return the newly created or updated bookmark
-    return $self->get_bookmark({ uri => $uri });
-}
+    # how many tags have changed?
+    return $changed_tags;
+}
+
 
 # module returns true
