51 lines
1.4 KiB
Rust
51 lines
1.4 KiB
Rust
//! `SeaORM` Entity, @generated by sea-orm-codegen 1.1.19
|
|
|
|
use sea_orm::entity::prelude::*;
|
|
|
|
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)]
|
|
#[sea_orm(table_name = "content")]
|
|
pub struct Model {
|
|
#[sea_orm(primary_key)]
|
|
pub id: i32,
|
|
pub content: String,
|
|
pub r#type: String,
|
|
}
|
|
|
|
impl Model {
|
|
/// Get an entity that has the same content and returns its ID or creates a new entity and
|
|
/// returns its ID
|
|
pub async fn get_or_create(content: String, db: &DatabaseConnection) -> Result<i32, DbErr> {
|
|
let content_db = Entity::find()
|
|
.filter(Column::Content.eq(&content))
|
|
.one(db)
|
|
.await?;
|
|
|
|
let content_db = match content_db {
|
|
Some(x) => x,
|
|
None => {
|
|
let activeModel = ActiveModel {
|
|
id: sea_orm::ActiveValue::NotSet,
|
|
content: sea_orm::ActiveValue::Set(content),
|
|
r#type: sea_orm::ActiveValue::Set("".to_string()),
|
|
};
|
|
activeModel.insert(db).await.unwrap()
|
|
}
|
|
};
|
|
Ok(content_db.id)
|
|
}
|
|
}
|
|
|
|
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
|
|
pub enum Relation {
|
|
#[sea_orm(has_many = "super::messages::Entity")]
|
|
Messages,
|
|
}
|
|
|
|
impl Related<super::messages::Entity> for Entity {
|
|
fn to() -> RelationDef {
|
|
Relation::Messages.def()
|
|
}
|
|
}
|
|
|
|
impl ActiveModelBehavior for ActiveModel {}
|