blog/backend/src/db/models.rs

72 lines
1.6 KiB
Rust

use chrono::prelude::*;
use diesel::prelude::*;
use crate::db::schema;
#[derive(Identifiable, Queryable, Debug)]
#[diesel(table_name = schema::tags)]
pub struct Tag {
pub id: i32,
pub name: String,
}
#[derive(Insertable, Debug)]
#[diesel(table_name = schema::tags)]
pub struct NewTag<'a> {
pub name: &'a str,
}
#[derive(Identifiable, Queryable, Debug)]
#[diesel(table_name = schema::posts)]
pub struct Post {
pub id: i32,
pub name: String,
pub slug: String,
pub description: String,
pub content: String,
pub published_at: NaiveDate,
pub edited_at: Option<NaiveDate>,
pub active: bool,
}
#[derive(Insertable, Debug)]
#[diesel(table_name = schema::posts)]
pub struct NewPost<'a> {
pub name: &'a str,
pub slug: &'a str,
pub description: &'a str,
pub content: &'a str,
pub published_at: NaiveDate,
pub edited_at: Option<NaiveDate>,
pub active: bool,
}
#[derive(AsChangeset, Debug)]
#[diesel(table_name = schema::posts)]
pub struct UpdatePost<'a> {
pub name: Option<&'a str>,
pub slug: Option<&'a str>,
pub description: Option<&'a str>,
pub content: Option<&'a str>,
pub published_at: Option<NaiveDate>,
pub edited_at: Option<Option<NaiveDate>>,
pub active: Option<bool>,
}
#[derive(Associations, Identifiable, Queryable, Debug)]
#[diesel(belongs_to(Post))]
#[diesel(belongs_to(Tag))]
#[diesel(table_name = schema::post_tags)]
pub struct PostTag {
pub id: i32,
pub post_id: i32,
pub tag_id: i32,
}
#[derive(Insertable, Debug)]
#[diesel(table_name = schema::post_tags)]
pub struct NewPostTag {
pub post_id: i32,
pub tag_id: i32,
}