39 lines
958 B
Rust
39 lines
958 B
Rust
use crate::schema::posts::{self, dsl::posts as table_posts};
|
|
use chrono::NaiveDateTime;
|
|
use diesel::{QueryResult, RunQueryDsl, SqliteConnection};
|
|
use serde::Serialize;
|
|
|
|
#[derive(Insertable)]
|
|
#[table_name = "posts"]
|
|
pub struct NewPost<'a> {
|
|
pub parent: Option<&'a i32>,
|
|
pub timestamp: NaiveDateTime,
|
|
pub author: &'a str,
|
|
pub email: &'a str,
|
|
pub title: &'a str,
|
|
pub content: &'a str,
|
|
}
|
|
|
|
impl NewPost<'_> {
|
|
pub fn insert(&self, conn: &SqliteConnection) -> QueryResult<usize> {
|
|
diesel::insert_into(table_posts).values(self).execute(conn)
|
|
}
|
|
}
|
|
|
|
#[derive(Debug, Queryable, Serialize)]
|
|
pub struct Post {
|
|
pub id: i32,
|
|
pub parent: Option<i32>,
|
|
pub timestamp: NaiveDateTime,
|
|
pub author: String,
|
|
pub email: String,
|
|
pub title: String,
|
|
pub content: String,
|
|
}
|
|
|
|
impl Post {
|
|
pub fn get_all(connection: &SqliteConnection) -> QueryResult<Vec<Post>> {
|
|
posts::table.load::<Post>(connection)
|
|
}
|
|
}
|