瀏覽代碼

More work on #30, delete working-ish, update needs work & add

George S. Baugh 5 年之前
父節點
當前提交
3ad48966b4

+ 9 - 14
lib/Trog/Data/DUMMY.pm

@@ -255,28 +255,23 @@ sub _add_visibility (@posts) {
 sub add ($self, @posts) {
     require UUID::Tiny;
     foreach my $post (@posts) {
-        my $uuid = UUID::Tiny::create_uuid_as_string(UUID::Tiny::UUID_V1, UUID::Tiny::UUID_NS_DNS);
-        $post->{id} = $uuid;
-        push @$example_posts, $post;
-    }
-    return 1;
-}
-
-sub update($self, @posts) {
-    foreach my $update (@posts) {
-        foreach my $post (0..scalar(@$example_posts)) {
-            next unless $example_posts->[$post]->{id} eq $update->{id};
-            $example_posts->[$post] = $update;
+        $post->{id} //= UUID::Tiny::create_uuid_as_string(UUID::Tiny::UUID_V1, UUID::Tiny::UUID_NS_DNS);
+        my (undef, $existing_posts) = $self->get( id => $post->{id} );
+        if (@$existing_posts) {
+            my $existing_post = $existing_posts->[0];
+            $post->{version}  = $existing_post->{version};
+            $post->{version}++;
         }
+        push @$example_posts, $post;
     }
-    return 1;
+    return 0;
 }
 
 sub delete($self, @posts) {
     foreach my $update (@posts) {
         @$example_posts = grep { $_->{id} ne $update->{id} } @$example_posts;
     }
-    return 1;
+    return 0;
 }
 
 1;

+ 22 - 4
lib/Trog/Routes/HTML.pm

@@ -65,7 +65,12 @@ our %routes = (
     '/post/save' => {
         method   => 'POST',
         auth     => 1,
-        callback => \&Trog::Routes::HTML::post,
+        callback => \&Trog::Routes::HTML::post_save,
+    },
+    '/post/delete' => {
+        method   => 'POST',
+        auth     => 1,
+        callback => \&Trog::Routes::HTML::post_delete,
     },
     '/post/(.*)' => {
         method   => 'GET',
@@ -444,13 +449,14 @@ sub post ($query, $input, $render_cb) {
 
     return $render_cb->('post.tx', {
         title       => 'New Post',
+        to          => $query->{to},
+        failure     => $query->{failure} // -1,
         post_visibilities => ['public', 'private', 'unlisted'],
         stylesheets => $css,
         scripts     => $js,
         posts       => $posts,
         can_edit    => 1,
-        types       => [qw{microblog blog file series profile}],
-        route       => '/posts',
+        route       => $query->{route},
         category    => '/posts',
         page        => int($query->{page} || 1),
         limit       => int($query->{limit} || 25),
@@ -458,12 +464,24 @@ sub post ($query, $input, $render_cb) {
         sizes       => [25,50,100],
         id          => $query->{id},
         acls        => $acls,
+        post        => { tags => $query->{tag} },
         edittype    => $query->{type} || 'microblog',
     });
 }
 
-#TODO actually do stuff
 sub post_save ($query, $input, $render_cb) {
+    state $data = Trog::Data->new($conf);
+    my $postdata = _input2postdata($input);
+    $data->add($postdata);
+    $query->{failure} = 0;
+    return post($query, $input, $render_cb);
+}
+
+sub post_delete ($query, $input, $render_cb) {
+    state $data = Trog::Data->new($conf);
+    my $postdata = _input2postdata($input);
+    $query->{failure} = $data->delete($postdata);
+    $query->{to} = $postdata->{to};
     return post($query, $input, $render_cb);
 }
 

+ 1 - 0
www/templates/blog.tx

@@ -2,6 +2,7 @@
     Title *<br /><input required class="cooltext" type="text" name="title" placeholder="Iowa Man Destroys Moon" value="<: $post.title :>" />
     Preview Image<br /><input type="file" class="cooltext" name="preview" placeholder="PROMO.JPG" value="<: $post.preview :>" />
     : include "acls.tx";
+    : include "tags.tx";
     Content<br /><textarea required class="cooltext" name="comment" placeholder="Potzrebie"><: $post.data :></textarea>
     <input type="hidden" name="app" value="blog" />
     <input class="coolbutton" type="submit" value="Publish" text="Publish" />

+ 1 - 0
www/templates/file.tx

@@ -5,6 +5,7 @@
     <br /> TODO: Add "alternative" links, which scrape the appropriate icon for the alt link from the favicon<br />
     Preview Image<br /><input type="file" class="cooltext" name="preview" placeholder="PROMO.JPG" value="<: $post.preview :>" />
     : include "acls.tx";
+    : include "tags.tx";
     Comments<br /><textarea class="cooltext" name="comment" placeholder="Potzrebie"><: $post.data :></textarea>
     <input type="hidden" name="app" value="file" />
     <input class="coolbutton" type="submit" value="Publish" text="Publish" />

+ 1 - 0
www/templates/microblog.tx

@@ -5,6 +5,7 @@
     Audio<br /><input class="cooltext" type="url" name="audio_href" placeholder="https://soundclod.com/static.mp3" value="<: $post.audio_href :>" />
     Video<br /><input class="cooltext" type="url" name="video_href" placeholder="https://youvimeo.tv/infomercial.mp4" value="<: $post.video_href :>" />
     : include "acls.tx";
+    : include "tags.tx";
     Comments<br /><textarea class="cooltext" name="comment" placeholder="Potzrebie"><: $post.data :></textarea>
     <input type="hidden" name="app" value="microblog" />
     <input class="coolbutton" type="submit" value="Publish" text="Publish" />

+ 1 - 0
www/templates/post.tx

@@ -1,2 +1,3 @@
 : include "sysbar.tx";
+: include "jsalert.tx";
 : include "mbengine.tx";

+ 2 - 1
www/templates/posts.tx

@@ -43,8 +43,9 @@
             <a style="display: inline-block;" onclick="switchMenu('<: $post.id :>');">[Edit]</a>
             <div id="<: $post.id :>" style="display:none;">
                 : include $post.type ~ ".tx" { post => $post};
-                <form class="Submissions" action="/post/delete" class="inline">
+                <form class="Submissions" action="/post/delete" method="POST" class="inline">
                     <input type="hidden" name="id" value="<: $post.id :>"></input>
+                    <input type="hidden" name="to" value="<: $route :>"></input>
                     <input class="coolbutton" type="submit" value="Delete"></input>
                 </form>
             </div>

+ 1 - 0
www/templates/profile.tx

@@ -5,6 +5,7 @@
     Wallpaper<br /><input type="file" class="cooltext" name="wallpaper" placeholder="PROMO.JPG" value="<: $post.background_image :>" />
     Title  <br /><input class="cooltext" type="text" name="title" value="<: $post.title :>" />
     : include "acls.tx";
+    : include "tags.tx";
     Content<br /><textarea class="cooltext" name="comment" placeholder="Potzrebie"><: $post.data :></textarea>
     <input type="hidden" name="app" value="file" />
     <input class="coolbutton" type="submit" value="Publish" text="Publish" />

+ 6 - 0
www/templates/tags.tx

@@ -0,0 +1,6 @@
+Tags:<br />
+<select class="cooltext" multiple name="tags">
+    : for $post.tags -> $tag {
+    <option value="<: $tag :>" selected><: $tag :></option>
+    : }
+</select>