diff --git a/src/bin/delete_post.rs b/src/bin/delete_post.rs new file mode 100644 index 0000000..4b8ef83 --- /dev/null +++ b/src/bin/delete_post.rs @@ -0,0 +1,17 @@ +use diesel::prelude::*; +use diesel_demo::*; +use std::env::args; + +fn main() { + use self::schema::posts::dsl::*; + + let target = args().nth(1).expect("Expected a target to match against"); + let pattern = format!("%{}%", target); + + let connection = &mut establish_connection(); + let num_deleted = diesel::delete(posts.filter(title.like(pattern))) + .execute(connection) + .expect("Error deleting posts"); + + println!("Deleted {} posts", num_deleted); +} \ No newline at end of file diff --git a/src/bin/get_post.rs b/src/bin/get_post.rs new file mode 100644 index 0000000..af94e2e --- /dev/null +++ b/src/bin/get_post.rs @@ -0,0 +1,28 @@ +use self::models::Post; +use diesel::{connection, prelude::*}; +use diesel_demo::*; +use std::env::args; + +fn main() { + use self::schema::posts::dsl::posts; + + let post_id = args() + .nth(1) + .expect("get_post requires a post id") + .parse::() + .expect("Invalid Id"); + + let connection = &mut establish_connection(); + + let post = posts + .find(post_id) + .select(Post::as_select()) + .first(connection) + .optional(); + + match post { + Ok(Some(post)) => println!("Post with id: {} has a title: {}", post.id, post.title), + Ok(None) => println!("Unable to find post {}", post_id), + Err(_) => println!("An error occured while fetching post {}", post_id), + } +} \ No newline at end of file diff --git a/src/bin/publish_post.rs b/src/bin/publish_post.rs new file mode 100644 index 0000000..5e95562 --- /dev/null +++ b/src/bin/publish_post.rs @@ -0,0 +1,22 @@ +use self::models::Post; +use diesel::{connection, prelude::*}; +use diesel_demo::*; +use std::env::args; + +fn main() { + use self::schema::posts::dsl::{posts, published}; + + let id = args() + .nth(1).expect("publish_post requires a post id").parse::() + .expect("Invalid ID"); + + let connection = &mut establish_connection(); + + let post = diesel::update(posts.find(id)) + .set(published.eq(true)) + .returning(Post::as_returning()) + .get_result(connection) + .unwrap(); + + println!("Published post {}", post.title); +} \ No newline at end of file diff --git a/src/bin/write_post.rs b/src/bin/write_post.rs new file mode 100644 index 0000000..b50771a --- /dev/null +++ b/src/bin/write_post.rs @@ -0,0 +1,25 @@ +use diesel_demo::*; +use std::io::{stdin, Read}; + +fn main() { + let connection = &mut establish_connection(); + + let mut title = String::new(); + let mut body = String::new(); + + println!("What would you like your title to be?"); + stdin().read_line(&mut title).unwrap(); + let title = title.trim_end(); // Remove the trailing newline + + println!("\nOk! Let's write {title} (Press {EOF} when finished)\n",); + stdin().read_to_string(&mut body).unwrap(); + + let post = create_post(connection, title, &body); + println!("\nSaved draft {title} with id {}", post.id); +} + +#[cfg(not(windows))] +const EOF: &str = "CTRL+D"; + +#[cfg(windows)] +const EOF: &str = "CTRL+Z"; \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs index cdc43ef..d4a7359 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -12,4 +12,18 @@ pub fn establish_connection() -> PgConnection { PgConnection::establish(&database_url).unwrap_or_else( |_| panic!("Error connecting to {}", database_url) ) +} + +use self::models::{NewPost, Post}; + +pub fn create_post(conn: &mut PgConnection, title: &str, body: &str) -> Post { + use crate::schema::posts; + + let new_post = NewPost {title, body}; + + diesel::insert_into(posts::table) + .values(&new_post) + .returning(Post::as_returning()) + .get_result(conn) + .expect("Error saving new post") } \ No newline at end of file diff --git a/src/models.rs b/src/models.rs index 3ec3840..15e53cf 100644 --- a/src/models.rs +++ b/src/models.rs @@ -8,4 +8,13 @@ pub struct Post { pub title: String, pub body: String, pub published: bool, +} + +use crate::schema::posts; + +#[derive(Insertable)] +#[diesel(table_name = posts)] +pub struct NewPost<'a> { + pub title: &'a str, + pub body: &'a str, } \ No newline at end of file