presentation-rust-webapps/simple_text_board/src/models.rs

39 lines
958 B
Rust
Raw Normal View History

2021-01-12 17:55:25 +01:00
use crate::schema::posts::{self, dsl::posts as table_posts};
use chrono::NaiveDateTime;
2021-04-14 17:07:10 +02:00
use diesel::{QueryResult, RunQueryDsl, SqliteConnection};
use serde::Serialize;
2021-01-12 17:55:25 +01:00
#[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<'_> {
2021-04-14 23:02:09 +02:00
pub fn insert(&self, conn: &SqliteConnection) -> QueryResult<usize> {
2021-01-12 17:55:25 +01:00
diesel::insert_into(table_posts).values(self).execute(conn)
}
}
2021-04-14 17:07:10 +02:00
#[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)
}
}