Skip to content

Commit f30016e

Browse files
DarkThroneJohnTitor
authored andcommitted
Fix DB article code examples (#140)
This change fixes issue #139 and renders the code example
1 parent 58341ec commit f30016e

File tree

1 file changed

+81
-81
lines changed

1 file changed

+81
-81
lines changed

examples/og_databases/src/main.rs

Lines changed: 81 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1,98 +1,98 @@
11
// <actor>
2-
// use actix::prelude::*;
2+
use actix::prelude::*;
33

4-
// struct DbExecutor(SqliteConnection);
4+
struct DbExecutor(SqliteConnection);
55

6-
// impl Actor for DbExecutor {
7-
// type Context = SyncContext<Self>;
8-
// }
6+
impl Actor for DbExecutor {
7+
type Context = SyncContext<Self>;
8+
}
99
// </actor>
1010

1111
// <message>
12-
// struct CreateUser {
13-
// name: String,
14-
// }
12+
struct CreateUser {
13+
name: String,
14+
}
1515

16-
// impl Message for CreateUser {
17-
// type Result = Result<User, Error>;
18-
// }
16+
impl Message for CreateUser {
17+
type Result = Result<User, Error>;
18+
}
1919
// </message>
2020

2121
// <handler>
22-
// impl Handler<CreateUser> for DbExecutor {
23-
// type Result = Result<User, Error>;
24-
25-
// fn handle(&mut self, msg: CreateUser, _: &mut Self::Context) -> Self::Result {
26-
// use self::schema::users::dsl::*;
27-
28-
// // Create insertion model
29-
// let uuid = format!("{}", uuid::Uuid::new_v4());
30-
// let new_user = models::NewUser {
31-
// id: &uuid,
32-
// name: &msg.name,
33-
// };
34-
35-
// // normal diesel operations
36-
// diesel::insert_into(users)
37-
// .values(&new_user)
38-
// .execute(&self.0)
39-
// .expect("Error inserting person");
40-
41-
// let mut items = users
42-
// .filter(id.eq(&uuid))
43-
// .load::<models::User>(&self.0)
44-
// .expect("Error loading person");
45-
46-
// Ok(items.pop().unwrap())
47-
// }
48-
// }
22+
impl Handler<CreateUser> for DbExecutor {
23+
type Result = Result<User, Error>;
24+
25+
fn handle(&mut self, msg: CreateUser, _: &mut Self::Context) -> Self::Result {
26+
use self::schema::users::dsl::*;
27+
28+
// Create insertion model
29+
let uuid = format!("{}", uuid::Uuid::new_v4());
30+
let new_user = models::NewUser {
31+
id: &uuid,
32+
name: &msg.name,
33+
};
34+
35+
// normal diesel operations
36+
diesel::insert_into(users)
37+
.values(&new_user)
38+
.execute(&self.0)
39+
.expect("Error inserting person");
40+
41+
let mut items = users
42+
.filter(id.eq(&uuid))
43+
.load::<models::User>(&self.0)
44+
.expect("Error loading person");
45+
46+
Ok(items.pop().unwrap())
47+
}
48+
}
4949
// </handler>
5050

5151
// <main>
52-
// /// This is state where we will store *DbExecutor* address.
53-
// struct State {
54-
// db: Addr<DbExecutor>,
55-
// }
56-
57-
// fn main() {
58-
// let sys = actix::System::new("diesel-example");
59-
60-
// // Start 3 parallel db executors
61-
// let addr = SyncArbiter::start(3, || {
62-
// DbExecutor(SqliteConnection::establish("test.db").unwrap())
63-
// });
64-
65-
// // Start http server
66-
// HttpServer::new(move || {
67-
// App::with_state(State { db: addr.clone() })
68-
// .resource("/{name}", |r| r.method(Method::GET).a(index))
69-
// })
70-
// .bind("127.0.0.1:8080")
71-
// .unwrap()
72-
// .start()
73-
// .unwrap();
74-
75-
// println!("Started http server: 127.0.0.1:8080");
76-
// let _ = sys.run();
77-
// }
52+
/// This is state where we will store *DbExecutor* address.
53+
struct State {
54+
db: Addr<DbExecutor>,
55+
}
56+
57+
fn main() {
58+
let sys = actix::System::new("diesel-example");
59+
60+
// Start 3 parallel db executors
61+
let addr = SyncArbiter::start(3, || {
62+
DbExecutor(SqliteConnection::establish("test.db").unwrap())
63+
});
64+
65+
// Start http server
66+
HttpServer::new(move || {
67+
App::with_state(State { db: addr.clone() })
68+
.resource("/{name}", |r| r.method(Method::GET).a(index))
69+
})
70+
.bind("127.0.0.1:8080")
71+
.unwrap()
72+
.start()
73+
.unwrap();
74+
75+
println!("Started http server: 127.0.0.1:8080");
76+
let _ = sys.run();
77+
}
7878
// </main>
7979

8080
// <index>
81-
// /// Async handler
82-
// fn index(req: &HttpRequest<State>) -> Box<Future<Item = HttpResponse, Error = Error>> {
83-
// let name = &req.match_info()["name"];
84-
85-
// // Send message to `DbExecutor` actor
86-
// req.state()
87-
// .db
88-
// .send(CreateUser {
89-
// name: name.to_owned(),
90-
// })
91-
// .from_err()
92-
// .and_then(|res| match res {
93-
// Ok(user) => Ok(HttpResponse::Ok().json(user)),
94-
// Err(_) => Ok(HttpResponse::InternalServerError().into()),
95-
// })
96-
// .responder()
97-
// }
81+
/// Async handler
82+
fn index(req: &HttpRequest<State>) -> Box<Future<Item = HttpResponse, Error = Error>> {
83+
let name = &req.match_info()["name"];
84+
85+
// Send message to `DbExecutor` actor
86+
req.state()
87+
.db
88+
.send(CreateUser {
89+
name: name.to_owned(),
90+
})
91+
.from_err()
92+
.and_then(|res| match res {
93+
Ok(user) => Ok(HttpResponse::Ok().json(user)),
94+
Err(_) => Ok(HttpResponse::InternalServerError().into()),
95+
})
96+
.responder()
97+
}
9898
// </index>

0 commit comments

Comments
 (0)