yay we can create posts

This commit is contained in:
Christoph J. Scherr 2024-06-27 11:51:28 +02:00
parent 0603c3f460
commit a353680e63
7 changed files with 31 additions and 27 deletions

1
.gitignore vendored
View File

@ -5,3 +5,4 @@ members/c-bindings/lib/libtest.so
members/c-bindings/lib/libtest.so.1
members/c-bindings/lib/libtest.so.1.0.1
members/sqlite-demo/data/cats.db
members/diesel-demo/data/dieseldemo.db

View File

@ -1,8 +0,0 @@
-- Your SQL goes here
CREATE TABLE `posts`(
`id` INT4 NOT NULL PRIMARY KEY,
`title` VARCHAR NOT NULL,
`body` TEXT NOT NULL,
`published` BOOL NOT NULL
);

View File

@ -0,0 +1,8 @@
-- Your SQL goes here
CREATE TABLE `posts`(
`id` INTEGER NOT NULL PRIMARY KEY,
`title` TEXT NOT NULL,
`body` TEXT NOT NULL,
`published` BOOL NOT NULL DEFAULT false
);

View File

@ -1,6 +1,6 @@
use diesel::SqliteConnection;
use diesel_demo::models::PostDraft;
use libpt::log::{self, debug, info, trace, warn};
use libpt::log::{self, debug, error, info, trace, warn};
use diesel_demo as lib;
@ -14,10 +14,6 @@ fn main() -> anyhow::Result<()> {
let mut conn = lib::establish_connection()?;
debug!("db connection established");
let posts = lib::load_posts(&mut conn)?;
lib::print_posts(&posts);
trace!("entering the repl");
repl(&mut conn)?;
trace!("leaving the repl");
@ -32,19 +28,24 @@ fn repl(conn: &mut SqliteConnection) -> anyhow::Result<()> {
lib::read_buf_interactive(&mut buf)?;
buf = buf.to_uppercase();
if buf.starts_with("HELP") {
println!("\
println!(
"\
help - show this menu\n\
exit - exit the application\n\
new - create a new post")
}
else if buf.starts_with("EXIT") {
list - list all posts\n\
new - create a new post"
)
} else if buf.starts_with("EXIT") {
break;
}
else if buf.starts_with("NEW") {
} else if buf.starts_with("LIST") {
let posts = lib::load_posts(conn)?;
lib::print_posts(&posts);
} else if buf.starts_with("NEW") {
let post = PostDraft::interactive_create()?;
post.post(conn)?;
}
else {
let _ = post.post(conn).inspect_err(|e| {
error!("Could not submit the post: {e:?}");
});
} else {
println!("Bad input: try 'help'");
}
}

View File

@ -1,6 +1,7 @@
use std::io::{self, Read, Write};
use diesel::prelude::*;
use libpt::log::trace;
use crate::schema::posts;
@ -10,10 +11,9 @@ const EOF: &str = "CTRL+D";
#[cfg(windows)]
const EOF: &str = "CTRL+Z";
#[derive(Queryable, Selectable)]
#[derive(Queryable, Selectable, Debug)]
#[diesel(table_name = crate::schema::posts)]
#[diesel(check_for_backend(diesel::sqlite::Sqlite))] // optional but improves generated compiler
// errors
#[diesel(check_for_backend(diesel::sqlite::Sqlite))] // optional but improves generated compiler errors
pub struct Post {
pub id: i32,
pub title: String,
@ -21,8 +21,9 @@ pub struct Post {
pub published: bool,
}
#[derive(Insertable)]
#[derive(Insertable, Debug)]
#[diesel(table_name = crate::schema::posts)]
#[diesel(check_for_backend(diesel::sqlite::Sqlite))] // optional but improves generated compiler errors
pub struct PostDraft {
pub title: String,
pub body: String,
@ -36,6 +37,7 @@ impl PostDraft {
}
}
pub fn post(self, conn: &mut SqliteConnection) -> anyhow::Result<Post> {
trace!("PostDraft to post: {self:#?}");
Ok(diesel::insert_into(posts::table)
.values(&self)
.returning(Post::as_returning())
@ -52,7 +54,7 @@ impl PostDraft {
stdin.read_line(&mut title)?;
title = title.trim().to_string();
println!("(End with {} when finished) Body:\n", EOF);
println!("(End with {} when finished) Body:", EOF);
stdin.read_to_string(&mut body)?;
body = body.trim().to_string();