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