Changeset 45 in bookmarks


Ignore:
Timestamp:
06/03/13 17:43:16 (12 years ago)
Author:
peter
Message:
  • created a separate edit runmode to edit existing bookmarks; handled POST /{id}
  • the old edit runmode has been renamed to create, and only handles POST /
  • added an update method to the Bookmark class
  • the URI field on the bookmark editing form is no longer ever readonly; the URI can be changed after the bookmark has been created
  • changes to the URI or the bookmark's tags cause the bookmark's mtime to be updated
Location:
trunk
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/Bookmark.pm

    r44 r45  
    3737} 
    3838 
     39sub 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 
    3995# module return 
    40961; 
  • trunk/BookmarkApp.pm

    r43 r45  
    2020        view 
    2121        view_field 
     22        create 
    2223        edit 
    2324    }]); 
     
    361362} 
    362363 
    363 #TODO: split this into edit and create methods 
    364 sub edit { 
     364sub create { 
    365365    my $self = shift; 
    366366    my $q = $self->query; 
    367     #TODO: get the bookmark based on the id and edit it directly? 
    368     #TODO: deal with changing URIs 
    369367    my $uri = $q->param('uri'); 
    370368    my $title = $q->param('title'); 
     
    392390} 
    393391 
     392sub edit { 
     393    my $self = shift; 
     394    my $q = $self->query; 
     395    my $id = $self->param('id'); 
     396 
     397    my $bookmark = $self->_bookmarks->get_bookmark({ id => $id }); 
     398    if ($bookmark) { 
     399        # update the URI, title, and tags 
     400        $bookmark->uri($q->param('uri')); 
     401        $bookmark->title($q->param('title')); 
     402        $bookmark->tags([ split(' ', $q->param('tags')) ]); 
     403        $bookmark->update($self->_bookmarks->dbh); 
     404 
     405        # return to the form 
     406        $self->header_type('redirect'); 
     407        $self->header_props( 
     408            -uri => $bookmark->bookmark_uri->canonical, 
     409            -status => 303, 
     410        ); 
     411    } else { 
     412        $self->header_props( 
     413            -type    => 'text/html', 
     414            -charset => 'UTF-8', 
     415            -status  => 404, 
     416        ); 
     417        return "Bookmark $id Not Found"; 
     418    } 
     419} 
     420 
    3944211; 
  • trunk/BookmarkApp/Dispatch.pm

    r42 r45  
    1313            ':id[get]'        => { app => 'BookmarkApp', rm => 'view' }, 
    1414            ':id/:field[get]' => { app => 'BookmarkApp', rm => 'view_field' }, 
    15             ':id?[post]'      => { app => 'BookmarkApp', rm => 'edit' }, 
     15            '[post]'          => { app => 'BookmarkApp', rm => 'create' }, 
     16            ':id[post]'       => { app => 'BookmarkApp', rm => 'edit' }, 
    1617        ], 
    1718    }; 
  • trunk/TODO

    r18 r45  
    11    - import/export of bookmark data 
    2     - allow changing of URLs of resources 
    32    - tool to scan database for broken URLs 
    43    - machine tags 
  • trunk/bookmark.tt

    r40 r45  
    3636            <th>URI:</th> 
    3737            <td> 
    38               <input type="text" name="uri" value="[% uri | html %]" size="80"  
    39               [% IF exists %]readonly="readonly"[% END%]/> 
     38              <input type="text" name="uri" value="[% uri | html %]" size="80"/> 
    4039            </td> 
    4140          </tr> 
Note: See TracChangeset for help on using the changeset viewer.