Skip to content

Commit e7151cd

Browse files
sgrifkillercup
authored andcommitted
Add getting started guide step 3 to the main repo
1 parent 8e388a7 commit e7151cd

10 files changed

Lines changed: 162 additions & 0 deletions

File tree

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[package]
2+
name = "diesel_demo"
3+
version = "0.1.0"
4+
authors = ["Sean Griffin <sean@seantheprogrammer.com>"]
5+
6+
[dependencies]
7+
diesel = "0.7.1"
8+
diesel_codegen = { version = "0.7.2", features = ["postgres"] }
9+
dotenv = "0.8.0"
10+
dotenv_macros = "0.9.0"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
DROP TABLE posts
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
CREATE TABLE posts (
2+
id SERIAL PRIMARY KEY,
3+
title VARCHAR NOT NULL,
4+
body TEXT NOT NULL,
5+
published BOOLEAN NOT NULL DEFAULT 'f'
6+
)
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
extern crate diesel_demo;
2+
extern crate diesel;
3+
4+
use diesel::prelude::*;
5+
use diesel_demo::*;
6+
use std::env::args;
7+
8+
fn main() {
9+
use diesel_demo::schema::posts::dsl::*;
10+
11+
let target = args().nth(1).expect("Expected a target to match against");
12+
let pattern = format!("%{}%", target);
13+
14+
let connection = establish_connection();
15+
let num_deleted = diesel::delete(posts.filter(title.like(pattern)))
16+
.execute(&connection)
17+
.expect("Error deleting posts");
18+
19+
println!("Deleted {} posts", num_deleted);
20+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
extern crate diesel_demo;
2+
extern crate diesel;
3+
4+
use diesel::prelude::*;
5+
use diesel_demo::*;
6+
use diesel_demo::models::Post;
7+
use std::env::args;
8+
9+
fn main() {
10+
use diesel_demo::schema::posts::dsl::{posts, published};
11+
12+
let id = args().nth(1).expect("publish_post requires a post id")
13+
.parse::<i32>().expect("Invalid ID");
14+
let connection = establish_connection();
15+
16+
let post = diesel::update(posts.find(id))
17+
.set(published.eq(true))
18+
.get_result::<Post>(&connection)
19+
.expect(&format!("Unable to find post {}", id));
20+
println!("Published post {}", post.title);
21+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
extern crate diesel_demo;
2+
extern crate diesel;
3+
4+
use diesel_demo::*;
5+
use diesel_demo::models::*;
6+
use diesel::prelude::*;
7+
8+
fn main() {
9+
use diesel_demo::schema::posts::dsl::*;
10+
11+
let connection = establish_connection();
12+
let results = posts.filter(published.eq(true))
13+
.limit(5)
14+
.load::<Post>(&connection)
15+
.expect("Error loading posts");
16+
17+
println!("Displaying {} posts", results.len());
18+
for post in results {
19+
println!("{}", post.title);
20+
println!("-----------\n");
21+
println!("{}", post.body);
22+
}
23+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
extern crate diesel_demo;
2+
extern crate diesel;
3+
4+
use self::diesel_demo::*;
5+
use std::io::{stdin, Read};
6+
7+
fn main() {
8+
let connection = establish_connection();
9+
10+
let mut title = String::new();
11+
let mut body = String::new();
12+
13+
println!("What would you like your title to be?");
14+
stdin().read_line(&mut title).unwrap();
15+
let title = title.trim_right(); // Remove the trailing newline
16+
17+
println!("\nOk! Let's write {} (Press {} when finished)\n", title, EOF);
18+
stdin().read_to_string(&mut body).unwrap();
19+
20+
let post = create_post(&connection, title, &body);
21+
println!("\nSaved draft {} with id {}", title, post.id);
22+
}
23+
24+
#[cfg(not(windows))]
25+
const EOF: &'static str = "CTRL+D";
26+
27+
#[cfg(windows)]
28+
const EOF: &'static str = "CTRL+Z";
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#![feature(custom_derive, custom_attribute, plugin)]
2+
#![plugin(diesel_codegen, dotenv_macros)]
3+
4+
#[macro_use] extern crate diesel;
5+
extern crate dotenv;
6+
7+
pub mod schema;
8+
pub mod models;
9+
10+
use diesel::prelude::*;
11+
use diesel::pg::PgConnection;
12+
use dotenv::dotenv;
13+
use std::env;
14+
15+
use self::models::{Post, NewPost};
16+
17+
pub fn establish_connection() -> PgConnection {
18+
dotenv().ok();
19+
20+
let database_url = env::var("DATABASE_URL")
21+
.expect("DATABASE_URL must be set");
22+
PgConnection::establish(&database_url)
23+
.expect(&format!("Error connecting to {}", database_url))
24+
}
25+
26+
pub fn create_post(conn: &PgConnection, title: &str, body: &str) -> Post {
27+
use schema::posts;
28+
29+
let new_post = NewPost {
30+
title: title,
31+
body: body,
32+
};
33+
34+
diesel::insert(&new_post).into(posts::table)
35+
.get_result(conn)
36+
.expect("Error saving new post")
37+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
use schema::posts;
2+
3+
#[derive(Queryable)]
4+
pub struct Post {
5+
pub id: i32,
6+
pub title: String,
7+
pub body: String,
8+
pub published: bool,
9+
}
10+
11+
#[insertable_into(posts)]
12+
pub struct NewPost<'a> {
13+
pub title: &'a str,
14+
pub body: &'a str,
15+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
infer_schema!(dotenv!("DATABASE_URL"));

0 commit comments

Comments
 (0)