wip refactoring

This commit is contained in:
2026-03-28 01:14:02 +00:00
parent eeee568048
commit b0968a4f28
5 changed files with 143 additions and 105 deletions
+24
View File
@@ -11,6 +11,30 @@ pub struct Model {
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")]