yay we can create posts
This commit is contained in:
parent
0603c3f460
commit
a353680e63
|
@ -5,3 +5,4 @@ members/c-bindings/lib/libtest.so
|
||||||
members/c-bindings/lib/libtest.so.1
|
members/c-bindings/lib/libtest.so.1
|
||||||
members/c-bindings/lib/libtest.so.1.0.1
|
members/c-bindings/lib/libtest.so.1.0.1
|
||||||
members/sqlite-demo/data/cats.db
|
members/sqlite-demo/data/cats.db
|
||||||
|
members/diesel-demo/data/dieseldemo.db
|
||||||
|
|
Binary file not shown.
|
@ -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
|
|
||||||
);
|
|
||||||
|
|
|
@ -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
|
||||||
|
);
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
use diesel::SqliteConnection;
|
use diesel::SqliteConnection;
|
||||||
use diesel_demo::models::PostDraft;
|
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;
|
use diesel_demo as lib;
|
||||||
|
|
||||||
|
@ -14,10 +14,6 @@ fn main() -> anyhow::Result<()> {
|
||||||
let mut conn = lib::establish_connection()?;
|
let mut conn = lib::establish_connection()?;
|
||||||
debug!("db connection established");
|
debug!("db connection established");
|
||||||
|
|
||||||
let posts = lib::load_posts(&mut conn)?;
|
|
||||||
|
|
||||||
lib::print_posts(&posts);
|
|
||||||
|
|
||||||
trace!("entering the repl");
|
trace!("entering the repl");
|
||||||
repl(&mut conn)?;
|
repl(&mut conn)?;
|
||||||
trace!("leaving the repl");
|
trace!("leaving the repl");
|
||||||
|
@ -32,19 +28,24 @@ fn repl(conn: &mut SqliteConnection) -> anyhow::Result<()> {
|
||||||
lib::read_buf_interactive(&mut buf)?;
|
lib::read_buf_interactive(&mut buf)?;
|
||||||
buf = buf.to_uppercase();
|
buf = buf.to_uppercase();
|
||||||
if buf.starts_with("HELP") {
|
if buf.starts_with("HELP") {
|
||||||
println!("\
|
println!(
|
||||||
|
"\
|
||||||
help - show this menu\n\
|
help - show this menu\n\
|
||||||
exit - exit the application\n\
|
exit - exit the application\n\
|
||||||
new - create a new post")
|
list - list all posts\n\
|
||||||
}
|
new - create a new post"
|
||||||
else if buf.starts_with("EXIT") {
|
)
|
||||||
|
} else if buf.starts_with("EXIT") {
|
||||||
break;
|
break;
|
||||||
}
|
} else if buf.starts_with("LIST") {
|
||||||
else if buf.starts_with("NEW") {
|
let posts = lib::load_posts(conn)?;
|
||||||
|
lib::print_posts(&posts);
|
||||||
|
} else if buf.starts_with("NEW") {
|
||||||
let post = PostDraft::interactive_create()?;
|
let post = PostDraft::interactive_create()?;
|
||||||
post.post(conn)?;
|
let _ = post.post(conn).inspect_err(|e| {
|
||||||
}
|
error!("Could not submit the post: {e:?}");
|
||||||
else {
|
});
|
||||||
|
} else {
|
||||||
println!("Bad input: try 'help'");
|
println!("Bad input: try 'help'");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
use std::io::{self, Read, Write};
|
use std::io::{self, Read, Write};
|
||||||
|
|
||||||
use diesel::prelude::*;
|
use diesel::prelude::*;
|
||||||
|
use libpt::log::trace;
|
||||||
|
|
||||||
use crate::schema::posts;
|
use crate::schema::posts;
|
||||||
|
|
||||||
|
@ -10,10 +11,9 @@ const EOF: &str = "CTRL+D";
|
||||||
#[cfg(windows)]
|
#[cfg(windows)]
|
||||||
const EOF: &str = "CTRL+Z";
|
const EOF: &str = "CTRL+Z";
|
||||||
|
|
||||||
#[derive(Queryable, Selectable)]
|
#[derive(Queryable, Selectable, Debug)]
|
||||||
#[diesel(table_name = crate::schema::posts)]
|
#[diesel(table_name = crate::schema::posts)]
|
||||||
#[diesel(check_for_backend(diesel::sqlite::Sqlite))] // optional but improves generated compiler
|
#[diesel(check_for_backend(diesel::sqlite::Sqlite))] // optional but improves generated compiler errors
|
||||||
// errors
|
|
||||||
pub struct Post {
|
pub struct Post {
|
||||||
pub id: i32,
|
pub id: i32,
|
||||||
pub title: String,
|
pub title: String,
|
||||||
|
@ -21,8 +21,9 @@ pub struct Post {
|
||||||
pub published: bool,
|
pub published: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Insertable)]
|
#[derive(Insertable, Debug)]
|
||||||
#[diesel(table_name = crate::schema::posts)]
|
#[diesel(table_name = crate::schema::posts)]
|
||||||
|
#[diesel(check_for_backend(diesel::sqlite::Sqlite))] // optional but improves generated compiler errors
|
||||||
pub struct PostDraft {
|
pub struct PostDraft {
|
||||||
pub title: String,
|
pub title: String,
|
||||||
pub body: String,
|
pub body: String,
|
||||||
|
@ -36,6 +37,7 @@ impl PostDraft {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
pub fn post(self, conn: &mut SqliteConnection) -> anyhow::Result<Post> {
|
pub fn post(self, conn: &mut SqliteConnection) -> anyhow::Result<Post> {
|
||||||
|
trace!("PostDraft to post: {self:#?}");
|
||||||
Ok(diesel::insert_into(posts::table)
|
Ok(diesel::insert_into(posts::table)
|
||||||
.values(&self)
|
.values(&self)
|
||||||
.returning(Post::as_returning())
|
.returning(Post::as_returning())
|
||||||
|
@ -52,7 +54,7 @@ impl PostDraft {
|
||||||
stdin.read_line(&mut title)?;
|
stdin.read_line(&mut title)?;
|
||||||
title = title.trim().to_string();
|
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)?;
|
stdin.read_to_string(&mut body)?;
|
||||||
body = body.trim().to_string();
|
body = body.trim().to_string();
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue