add insert script

This commit is contained in:
Rawley Fowler
2023-07-11 15:54:26 -06:00
parent 28f919d8d7
commit c0803dc3da
4 changed files with 36 additions and 62 deletions

34
insert.pl Executable file
View File

@@ -0,0 +1,34 @@
#!/usr/bin/env perl
use strict;
use warnings;
use Carp qw(croak);
use Mojo::SQLite;
use Mojo::File;
use feature qw(say);
my $db = Mojo::SQLite->new('sqlite:site.db')->db;
my $title = shift || croak 'Expected $1 to be title';
my $slug = shift || croak 'Expected $2 to be slug';
my $content = shift || croak 'Expected $3 to be md file';
$content = Mojo::File->new($content)->slurp;
my $post = {
title => $title,
slug => $slug,
content => $content
};
if ( my $old_post = $db->select( 'posts', ['id'], { slug => $slug } )->hash ) {
say 'Updating post.';
$db->update( 'posts', $post, { id => $old_post->{id} } );
}
else {
say 'Inserting post.';
$db->insert( 'posts', $post );
}
say 'Finished.';