Skip to content

Instantly share code, notes, and snippets.

@s1037989
Created January 27, 2024 20:24
Show Gist options
  • Save s1037989/73779409c197b704bf4501f65be32ec9 to your computer and use it in GitHub Desktop.
Save s1037989/73779409c197b704bf4501f65be32ec9 to your computer and use it in GitHub Desktop.
POC for storing git log in a db via HTTP
diff --git a/examples/blog/lib/Blog.pm b/examples/blog/lib/Blog.pm
index 57a2291..ebf83cb 100644
--- a/examples/blog/lib/Blog.pm
+++ b/examples/blog/lib/Blog.pm
@@ -30,6 +30,8 @@ sub startup {
$r->get('/posts/:id/edit')->to('posts#edit')->name('edit_post');
$r->put('/posts/:id')->to('posts#update')->name('update_post');
$r->delete('/posts/:id')->to('posts#remove')->name('remove_post');
+
+ $r->post('/log')->to('posts#log')->name('log_post');
}
1;
diff --git a/examples/blog/lib/Blog/Controller/Posts.pm b/examples/blog/lib/Blog/Controller/Posts.pm
index 5815f2b..a2b17f9 100644
--- a/examples/blog/lib/Blog/Controller/Posts.pm
+++ b/examples/blog/lib/Blog/Controller/Posts.pm
@@ -13,6 +13,13 @@ sub index {
$self->render(posts => $self->posts->all);
}
+sub log {
+ my $self = shift;
+ $self->app->log->debug('Hello from the controller!');
+ $self->posts->log($self->req->body);
+ $self->render(text => 'Logged!');
+}
+
sub remove {
my $self = shift;
$self->posts->remove($self->param('id'));
@@ -32,6 +39,7 @@ sub store {
if $validation->has_error;
my $id = $self->posts->add($validation->output);
+ $self->posts->log(join ' | ', $id, 'manual', time, 'Manual', $validation->param('title'));
$self->redirect_to('show_post', id => $id);
}
diff --git a/examples/blog/lib/Blog/Model/Posts.pm b/examples/blog/lib/Blog/Model/Posts.pm
index 8837400..dde7c27 100644
--- a/examples/blog/lib/Blog/Model/Posts.pm
+++ b/examples/blog/lib/Blog/Model/Posts.pm
@@ -15,6 +15,15 @@ sub find {
return $self->sqlite->db->select('posts', undef, {id => $id})->hash;
}
+sub log {
+ my ($self, $log) = @_;
+
+ my @log = map { [split /\s+\|\s+/, $_ ] } split /\n/, $log;
+ warn Mojo::Util::dumper($_) for @log;
+ $self->sqlite->db->insert('log', $_, {on_conflict => undef}) for @log;
+ return $#log;
+}
+
sub remove {
my ($self, $id) = @_;
$self->sqlite->db->delete('posts', {id => $id});
diff --git a/examples/blog/migrations/blog.sql b/examples/blog/migrations/blog.sql
index ee5fa28..cc782f7 100644
--- a/examples/blog/migrations/blog.sql
+++ b/examples/blog/migrations/blog.sql
@@ -7,3 +7,15 @@ create table if not exists posts (
-- 1 down
drop table if exists posts;
+
+-- 2 up
+create table if not exists log (
+ hash text primary key,
+ repo text,
+ ct datetime,
+ author text,
+ message text
+);
+
+-- 2 down
+drop table if exists log;
\ No newline at end of file
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment