1
0
mirror of https://gitlab.com/MisterBiggs/diesel_tutorial.git synced 2025-09-18 19:32:40 +00:00

finished tut

This commit is contained in:
2025-05-09 19:24:23 -06:00
parent ab9a94ac1e
commit c1b7403fda
6 changed files with 115 additions and 0 deletions

17
src/bin/delete_post.rs Normal file
View File

@@ -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);
}

28
src/bin/get_post.rs Normal file
View File

@@ -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::<i32>()
.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),
}
}

22
src/bin/publish_post.rs Normal file
View File

@@ -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::<i32>()
.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);
}

25
src/bin/write_post.rs Normal file
View File

@@ -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";

View File

@@ -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")
}

View File

@@ -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,
}