feat(db): Support CreateThread message
This commit is contained in:
parent
f281749b8c
commit
fc7ca2900d
2 changed files with 29 additions and 0 deletions
22
src/db.rs
22
src/db.rs
|
@ -1,6 +1,7 @@
|
|||
//! This module implements the database connection actor.
|
||||
|
||||
use actix::prelude::*;
|
||||
use diesel;
|
||||
use diesel::prelude::*;
|
||||
use diesel::r2d2::{Pool, ConnectionManager};
|
||||
use models::*;
|
||||
|
@ -57,3 +58,24 @@ impl Handler<GetThread> for DbExecutor {
|
|||
Ok((thread_result, post_list))
|
||||
}
|
||||
}
|
||||
|
||||
/// Message used to create a new thread
|
||||
pub struct CreateThread(pub NewThread);
|
||||
|
||||
impl Message for CreateThread {
|
||||
type Result = Result<Thread>;
|
||||
}
|
||||
|
||||
impl Handler<CreateThread> for DbExecutor {
|
||||
type Result = <CreateThread as Message>::Result;
|
||||
|
||||
fn handle(&mut self, msg: CreateThread, _: &mut Self::Context) -> Self::Result {
|
||||
use schema::threads;
|
||||
|
||||
let conn = self.0.get()?;
|
||||
|
||||
Ok(diesel::insert_into(threads::table)
|
||||
.values(&msg.0)
|
||||
.get_result(&conn)?)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -17,3 +17,10 @@ pub struct Post {
|
|||
pub body: String,
|
||||
pub posted: DateTime<Utc>,
|
||||
}
|
||||
|
||||
#[derive(Insertable)]
|
||||
#[table_name="threads"]
|
||||
pub struct NewThread {
|
||||
pub title: String,
|
||||
pub body: String,
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue