mirror of
https://gitlab.com/MisterBiggs/diesel_tutorial.git
synced 2025-09-19 03:42:37 +00:00
finished tut
This commit is contained in:
17
src/bin/delete_post.rs
Normal file
17
src/bin/delete_post.rs
Normal 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
28
src/bin/get_post.rs
Normal 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
22
src/bin/publish_post.rs
Normal 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
25
src/bin/write_post.rs
Normal 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";
|
14
src/lib.rs
14
src/lib.rs
@@ -13,3 +13,17 @@ pub fn establish_connection() -> PgConnection {
|
||||
|_| 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")
|
||||
}
|
@@ -9,3 +9,12 @@ pub struct Post {
|
||||
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,
|
||||
}
|
Reference in New Issue
Block a user