Changeset 30 in bookmarks


Ignore:
Timestamp:
05/23/13 17:07:58 (11 years ago)
Author:
peter
Message:
  • added a separate run mode for the field resources (view_field)
  • added an api outline/plan
Location:
trunk
Files:
1 added
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/BookmarkApp.pm

    r27 r30  
    3131        feed 
    3232        view 
     33        view_field 
    3334        edit 
    3435    }]); 
     
    229230    my $self = shift; 
    230231    my $id = $self->param('id'); 
    231     my $field = $self->param('field'); 
    232232    my $format = $self->query->param('format') || 'html'; 
    233233 
    234234    my $bookmark = $bookmarks->get_bookmark({ id => $id }); 
    235235    if ($bookmark) { 
    236         if ($field) { 
    237             # respond with just the requested field as plain text 
    238             my $value = eval { $bookmark->$field }; 
    239             if ($@) { 
    240                 if ($@ =~ /Can't locate object method/) { 
    241                     $self->header_props( 
    242                         -type    => 'text/plain', 
    243                         -charset => 'UTF-8', 
    244                         -status  => 404, 
    245                     ); 
    246                     return qq{"$field" is not a valid bookmark data field}; 
    247                 } else { 
    248                     die $@; 
    249                 } 
     236        if ($format eq 'json') { 
     237            $self->header_props( 
     238                -type    => 'application/json', 
     239                -charset => 'UTF-8', 
     240            ); 
     241            return decode_utf8(JSON->new->utf8->convert_blessed->encode($bookmark)); 
     242        } else { 
     243            # display the bookmark form for this bookmark 
     244            $bookmark->{exists} = 1; 
     245            $bookmark->{created} = "Created " . localtime($bookmark->ctime); 
     246            $bookmark->{created} .= '; Updated ' . localtime($bookmark->mtime) unless $bookmark->ctime == $bookmark->mtime; 
     247            $self->header_props( 
     248                -type    => 'text/html', 
     249                -charset => 'UTF-8', 
     250            ); 
     251            return $self->tt_process( 
     252                'bookmark.tt', 
     253                $bookmark, 
     254            ); 
     255        } 
     256    } else { 
     257        $self->header_props( 
     258            -type    => 'text/html', 
     259            -charset => 'UTF-8', 
     260            -status  => 404, 
     261        ); 
     262        return "Bookmark $id Not Found"; 
     263    } 
     264} 
     265 
     266sub view_field { 
     267    my $self = shift; 
     268    my $id = $self->param('id'); 
     269    my $field = $self->param('field'); 
     270 
     271    my $bookmark = $bookmarks->get_bookmark({ id => $id }); 
     272    if ($bookmark) { 
     273        # respond with just the requested field as plain text 
     274        my $value = eval { $bookmark->$field }; 
     275        if ($@) { 
     276            if ($@ =~ /Can't locate object method/) { 
     277                $self->header_props( 
     278                    -type    => 'text/plain', 
     279                    -charset => 'UTF-8', 
     280                    -status  => 404, 
     281                ); 
     282                return qq{"$field" is not a valid bookmark data field}; 
     283            } else { 
     284                die $@; 
    250285            } 
    251             $self->header_props( 
    252                 -type    => 'text/plain', 
    253                 -charset => 'UTF-8', 
    254             ); 
    255             return ref $value eq 'ARRAY' ? join(',', @{ $value }) : $value; 
    256         } else { 
    257             if ($format eq 'json') { 
    258                 $self->header_props( 
    259                     -type    => 'application/json', 
    260                     -charset => 'UTF-8', 
    261                 ); 
    262                 return decode_utf8(JSON->new->utf8->convert_blessed->encode($bookmark)); 
    263             } else { 
    264                 # display the bookmark form for this bookmark 
    265                 $bookmark->{exists} = 1; 
    266                 $bookmark->{created} = "Created " . localtime($bookmark->ctime); 
    267                 $bookmark->{created} .= '; Updated ' . localtime($bookmark->mtime) unless $bookmark->ctime == $bookmark->mtime; 
    268                 $self->header_props( 
    269                     -type    => 'text/html', 
    270                     -charset => 'UTF-8', 
    271                 ); 
    272                 return $self->tt_process( 
    273                     'bookmark.tt', 
    274                     $bookmark, 
    275                 ); 
    276             } 
    277         } 
     286        } 
     287        $self->header_props( 
     288            -type    => 'text/plain', 
     289            -charset => 'UTF-8', 
     290        ); 
     291        return ref $value eq 'ARRAY' ? join(',', @{ $value }) : $value; 
    278292    } else { 
    279293        $self->header_props( 
  • trunk/index.cgi

    r29 r30  
    88    return { 
    99        table => [ 
    10             '[get]'              => { app => 'BookmarkApp', rm => 'list' }, 
    11             'list[get]'          => { app => 'BookmarkApp', rm => 'list' }, 
    12             'feed[get]'          => { app => 'BookmarkApp', rm => 'feed' }, 
    13             ':id/:field?[get]'   => { app => 'BookmarkApp', rm => 'view' }, 
    14             ':id?[post]'         => { app => 'BookmarkApp', rm => 'edit' }, 
     10            '[get]'           => { app => 'BookmarkApp', rm => 'list' }, 
     11            'list[get]'       => { app => 'BookmarkApp', rm => 'list' }, 
     12            'feed[get]'       => { app => 'BookmarkApp', rm => 'feed' }, 
     13            ':id[get]'        => { app => 'BookmarkApp', rm => 'view' }, 
     14            ':id/:field[get]' => { app => 'BookmarkApp', rm => 'view_field' }, 
     15            ':id?[post]'      => { app => 'BookmarkApp', rm => 'edit' }, 
    1516        ], 
    1617    }; 
Note: See TracChangeset for help on using the changeset viewer.