source: bookmarks/trunk/BookmarkApp.pm @ 7

Last change on this file since 7 was 7, checked in by peter, 12 years ago
  • added a base_uri property to the Bookmarks class; if present, it adds a bookmark_uri key to bookmarks returned by get_bookmark() and get_resources()
  • an individual bookmark can be returned as JSON by passing a "?format=json" parameter to the individual bookmark GET request
File size: 5.1 KB
Line 
1package BookmarkApp;
2use strict;
3use warnings;
4use base qw{CGI::Application};
5
6use CGI::Application::Plugin::TT;
7
8use DBI;
9use Encode;
10use JSON;
11use Bookmarks;
12
13use URI;
14my $base_uri = URI->new;
15$base_uri->scheme('http');
16$base_uri->host($ENV{SERVER_NAME});
17$base_uri->port($ENV{SERVER_PORT});
18$base_uri->path($ENV{SCRIPT_NAME} . '/');
19
20my $dbname = 'new.db';
21my $dbh = DBI->connect("dbi:SQLite:dbname=$dbname", "", "", { RaiseError => 1 });
22my $bookmarks = Bookmarks->new({
23    dbh => $dbh,
24    base_uri => $base_uri,
25});
26
27sub setup {
28    my $self = shift;
29    $self->mode_param(path_info => 1);
30    $self->run_modes([qw{
31        list
32        view
33        edit
34    }]);
35}
36
37sub list {
38    my $self = shift;
39    my $q = $self->query;
40
41    # check for a uri param, and if there is one present,
42    # see if a bookmark for that URI already exists
43    if (defined(my $uri = $q->param('uri'))) {
44        my $bookmark = $bookmarks->get_bookmark({ uri => $uri });
45        if ($bookmark) {
46            # redirect to the view of the existing bookmark
47            $self->header_type('redirect');
48            $self->header_props(
49                -uri => $q->url . '/' . $bookmark->{id},
50            );
51            return;
52        } else {
53            # bookmark was not found; show the form to create a new bookmark
54            $bookmark->{uri} = $uri;
55            $bookmark->{title} = $q->param('title');
56            $self->header_props(
57                -type    => 'text/html',
58                -charset => 'UTF-8',
59                -status  => 404,
60            );
61            return $self->tt_process(
62                'bookmark.tt',
63                $bookmark,
64            );
65        }
66    }
67
68    # list all the bookmarks
69    my $format = $q->param('format') || 'html';
70    my $tag = $q->param('tag');
71    my @resources = $bookmarks->get_resources({ tag => $tag });
72    my @all_tags = $bookmarks->get_tags({ selected => $tag });
73    my @cotags = $bookmarks->get_cotags({ tag => $tag });
74
75    if ($format eq 'json') {
76        $self->header_props(
77            -type    => 'application/json',
78            -charset => 'UTF-8',
79        );
80        return decode_utf8(
81            encode_json({
82                bookmarks => \@resources,
83            })
84        );
85    } else {
86        $self->header_props(
87            -type    => 'text/html',
88            -charset => 'UTF-8',
89        );
90
91        # set the base URL, adding a trailing slash if needed
92        my $base_url = $q->url;
93        $base_url .= '/' if $base_url =~ m{/bookmarks$};
94
95        return $self->tt_process(
96            'list.tt',
97            {
98                base_url     => $base_url,
99                selected_tag => $tag,
100                tags         => \@all_tags,
101                cotags       => \@cotags,
102                resources    => \@resources,
103            },
104        );
105    }
106}
107
108sub view {
109    my $self = shift;
110    my $id = $self->param('id');
111    my $field = $self->param('field');
112    my $format = $self->query->param('format') || 'html';
113
114    my $bookmark = $bookmarks->get_bookmark({ id => $id });
115    if ($bookmark) {
116        if ($field) {
117            # respond with just the requested field as plain text
118            $self->header_props(
119                -type    => 'text/plain',
120                -charset => 'UTF-8',
121            );
122            my $value = $bookmark->{$field};
123            return ref $value eq 'ARRAY' ? join(',', @{ $value }) : $value;
124        } else {
125            if ($format eq 'json') {
126                $self->header_props(
127                    -type    => 'application/json',
128                    -charset => 'UTF-8',
129                );
130                return decode_utf8(encode_json($bookmark));
131            } else {
132                # display the bookmark form for this bookmark
133                $bookmark->{exists} = 1;
134                $bookmark->{created} = "Created " . localtime($bookmark->{ctime});
135                $bookmark->{created} .= '; Updated ' . localtime($bookmark->{mtime}) unless $bookmark->{ctime} == $bookmark->{mtime};
136                $self->header_props(
137                    -type    => 'text/html',
138                    -charset => 'UTF-8',
139                );
140                return $self->tt_process(
141                    'bookmark.tt',
142                    $bookmark,
143                );
144            }
145        }
146    } else {
147        $self->header_props(
148            -type    => 'text/html',
149            -charset => 'UTF-8',
150            -status  => 404,
151        );
152        return "Bookmark $id Not Found";
153    }
154}
155
156#TODO: split this into edit and create methods
157sub edit {
158    my $self = shift;
159    my $q = $self->query;
160    #TODO: get the bookmark based on the id and edit it directly?
161    #TODO: deal with changing URIs
162    my $uri = $q->param('uri');
163    my $title = $q->param('title');
164    my @tags = split ' ', $q->param('tags');
165    $bookmarks->add({
166        uri   => $uri,
167        title => $title,
168        tags  => \@tags,
169    });
170
171=begin
172
173    my $location = URI->new($q->url);
174    $location->query_form(uri => $uri) if defined $q->url_param('uri');
175    $location->fragment('updated');
176
177=cut
178
179    # return to the form
180    $self->header_type('redirect');
181    $self->header_props(
182        -uri => $ENV{REQUEST_URI},
183        -status => 303,
184    );
185}
186
1871;
Note: See TracBrowser for help on using the repository browser.