1.6 KiB · text History d76cd01
use sea_orm::entity::prelude::*;
use serde::{Deserialize, Serialize};

use crate::render::CardSpec;

#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq, Serialize, Deserialize)]
#[sea_orm(table_name = "snippets")]
pub struct Model {
    #[sea_orm(primary_key, auto_increment = false)]
    pub id: Uuid,
    pub user_id: Uuid,
    #[sea_orm(unique)]
    pub slug: String,
    pub title: String,
    pub language: String,
    #[sea_orm(column_type = "Text")]
    pub code: String,
    pub chrome: String,
    pub palette: String,
    pub highlight: String,
    pub transparent: bool,
    pub padding: i32,
    pub line_numbers: bool,
    pub created_at: DateTimeWithTimeZone,
    pub updated_at: DateTimeWithTimeZone,
}

impl Model {
    pub fn spec(&self) -> CardSpec {
        CardSpec {
            code: self.code.clone(),
            title: self.title.clone(),
            language: self.language.clone(),
            chrome: self.chrome.clone(),
            palette: self.palette.clone(),
            highlight: self.highlight.clone(),
            transparent: self.transparent,
            padding: self.padding.max(0) as u32,
            line_numbers: self.line_numbers,
        }
    }
}

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {
    #[sea_orm(
        belongs_to = "super::users::Entity",
        from = "Column::UserId",
        to = "super::users::Column::Id",
        on_update = "Cascade",
        on_delete = "Cascade"
    )]
    Users,
}

impl Related<super::users::Entity> for Entity {
    fn to() -> RelationDef {
        Relation::Users.def()
    }
}

impl ActiveModelBehavior for ActiveModel {}