35 lines
802 B
Rust
35 lines
802 B
Rust
use crate::schema::posts::{self, dsl::posts as table_posts};
|
|
use diesel::{QueryResult, RunQueryDsl, SqliteConnection};
|
|
use rocket::FromForm;
|
|
use serde::Serialize;
|
|
|
|
#[derive(Queryable, Serialize)]
|
|
pub struct Post {
|
|
pub id: i32,
|
|
pub author: String,
|
|
pub email: String,
|
|
pub title: String,
|
|
pub content: String,
|
|
}
|
|
|
|
impl Post {
|
|
pub fn get_all(conn: &SqliteConnection) -> QueryResult<Vec<Post>> {
|
|
posts::table.load::<Post>(conn)
|
|
}
|
|
}
|
|
|
|
#[derive(Insertable, FromForm)]
|
|
#[table_name = "posts"]
|
|
pub struct NewPost {
|
|
pub author: String,
|
|
pub email: String,
|
|
pub title: String,
|
|
pub content: String,
|
|
}
|
|
|
|
impl NewPost {
|
|
pub fn insert(&self, conn: &SqliteConnection) -> QueryResult<usize> {
|
|
diesel::insert_into(table_posts).values(self).execute(conn)
|
|
}
|
|
}
|