From 6a90b0925e53eccfe8cd2c9ac138465d821b43d1 Mon Sep 17 00:00:00 2001 From: swaggboi Date: Sat, 2 Dec 2023 23:03:20 -0500 Subject: [PATCH] Added some stuff for the blog --- BLOG.md | 81 +++++++++++++++++++++++++ META6.json | 3 +- lib/Pastes-Bin.rakumod | 11 +++- lib/Pastes-Bin/Controller/Paste.rakumod | 13 ++++ lib/Pastes-Bin/Model/Paste.rakumod | 8 +++ 5 files changed, 112 insertions(+), 4 deletions(-) diff --git a/BLOG.md b/BLOG.md index 74ee492..0f6da4a 100644 --- a/BLOG.md +++ b/BLOG.md @@ -81,3 +81,84 @@ Let's check on DB: paste_body | text | | | Indexes: "pastes_pkey" PRIMARY KEY, btree (paste_id) + +Some mock-up 'model' code: + + use Pastes-Bin::Model::Paste; + + # No routes yet just prompt to 'fake it' + my $new-paste = prompt 'enter a new paste: '; + Pastes-Bin::Model::Paste.create: $new-paste; + +Now test it: + + $ ./bin/pastes-bin + enter DB password: C65VCQyp&zKsi#wKXzTLUM5V + line: -- 1 up + version: 1, direction: up + line: CREATE TABLE IF NOT EXISTS pastes ( + line: paste_id SERIAL PRIMARY KEY, + line: paste_body TEXT + line: ); + line: -- 1 down + version: 1, direction: down + line: DROP TABLE pastes; + current-version: allrows: [[1]] + {1 => {down => DROP TABLE pastes; + , up => CREATE TABLE IF NOT EXISTS pastes ( + paste_id SERIAL PRIMARY KEY, + paste_body TEXT + ); + }} + migrating from version '1' to version '1' + DB already at version 1 + enter a new paste: testing 123... + Humming-Bird listening on port http://localhost:3000 + ^C + +Now check the DB for the paste: + + pastes_bin=> SELECT * FROM pastes; + paste_id | paste_body + ----------+------------ + (0 rows) + + pastes_bin=> SELECT * FROM pastes; + paste_id | paste_body + ----------+---------------- + 1 | testing 123... + (1 row) + +End result: + + use Humming-Bird::Core; + use Humming-Bird::Middleware; + use Humming-Bird::Advice; + use DB::Migration::Simple; + use DBIish; + + # Local libs + use Pastes-Bin::Model::Paste; + + # Logging + middleware &middleware-logger; + advice &advice-logger; + + # Database stuff + my $*dbh = DBIish.connect: + 'Pg', + :host, + :database, + :user, + password => prompt 'enter DB password: '; + + my $m = DB::Migration::Simple.new: + :$*dbh, + :migration-file, + :verbose; + + $m.migrate: :version<1>; + + # No routes yet just prompt to 'fake it' + my $new-paste = prompt 'enter a new paste: '; + Pastes-Bin::Model::Paste.create: $new-paste; diff --git a/META6.json b/META6.json index 836c824..ea7ea5f 100644 --- a/META6.json +++ b/META6.json @@ -7,6 +7,7 @@ "DB::Migration::Simple" ], "provides": { - "Pastes-Bin": "lib/Pastes-Bin.rakumod" + "Pastes-Bin": "lib/Pastes-Bin.rakumod", + "Pastes-Bin::Controller::Paste": "lib/Pastes-Bin/Controller/Paste.rakumod" } } diff --git a/lib/Pastes-Bin.rakumod b/lib/Pastes-Bin.rakumod index dfa929d..7e1ee11 100644 --- a/lib/Pastes-Bin.rakumod +++ b/lib/Pastes-Bin.rakumod @@ -5,14 +5,15 @@ use DB::Migration::Simple; use DBIish; # Local libs -#use Pastes-Bin::Controller::Paste; # Coming soon! +#use Pastes-Bin::Controller::Paste; +use Pastes-Bin::Model::Paste; # Logging middleware &middleware-logger; advice &advice-logger; # Database stuff -my $dbh = DBIish.connect: +my $*dbh = DBIish.connect: 'Pg', :host, :database, @@ -20,8 +21,12 @@ my $dbh = DBIish.connect: password => prompt 'enter DB password: '; my $m = DB::Migration::Simple.new: - :$dbh, + :$*dbh, :migration-file, :verbose; $m.migrate: :version<1>; + +# No routes yet just prompt to 'fake it' +my $new-paste = prompt 'enter a new paste: '; +Pastes-Bin::Model::Paste.create: $new-paste; diff --git a/lib/Pastes-Bin/Controller/Paste.rakumod b/lib/Pastes-Bin/Controller/Paste.rakumod index e69de29..83e78e7 100644 --- a/lib/Pastes-Bin/Controller/Paste.rakumod +++ b/lib/Pastes-Bin/Controller/Paste.rakumod @@ -0,0 +1,13 @@ +use Humming-Bird::Core; + +use Pastes-Bin::Model::Paste; + +unit module Pastes-Bin::Controller::Paste; + +get '/', -> $request, $response { + # No routes yet just prompt to 'fake it' + my $new-paste = prompt 'enter a new paste: '; + Pastes-Bin::Model::Paste.create: $new-paste; + + $response.text('sent the paste!'); +}; diff --git a/lib/Pastes-Bin/Model/Paste.rakumod b/lib/Pastes-Bin/Model/Paste.rakumod index e69de29..1775972 100644 --- a/lib/Pastes-Bin/Model/Paste.rakumod +++ b/lib/Pastes-Bin/Model/Paste.rakumod @@ -0,0 +1,8 @@ +unit class Pastes-Bin::Model::Paste; + +submethod create(Str $new-paste) { + $*dbh.execute(q:to/END_SQL/, $new-paste) + INSERT INTO pastes (paste_body) + VALUES (?); + END_SQL +}