wip refactoring

This commit is contained in:
2026-03-29 10:58:26 +00:00
parent b0968a4f28
commit 97cdbdec0b
4 changed files with 63 additions and 43 deletions
+22
View File
@@ -10,6 +10,28 @@ pub struct Model {
pub id_discord: i64,
}
impl Model {
pub async fn get_or_create(id: i64, db: &DatabaseConnection) -> Result<i32, DbErr> {
let channel = Entity::find()
.filter(Column::IdDiscord.eq(id))
.one(db)
.await
.unwrap();
let channel = match channel {
Some(x) => x,
None => {
let activeModel = ActiveModel {
id: sea_orm::ActiveValue::NotSet,
id_discord: sea_orm::ActiveValue::Set(id as i64),
};
activeModel.insert(db).await.unwrap()
}
};
Ok(channel.id)
}
}
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {
#[sea_orm(has_many = "super::messages::Entity")]
+25
View File
@@ -15,6 +15,7 @@ pub struct Model {
impl Model {
pub async fn get_by_discord_id(
id: i64,
name: String,
db: &DatabaseConnection,
) -> Result<Option<Model>, DbErr> {
let server_db_id = Entity::find()
@@ -30,6 +31,30 @@ impl Model {
Err(e) => Err(e),
}
}
pub async fn get_or_create(
id: i64,
name: String,
db: &DatabaseConnection,
) -> Result<i32, DbErr> {
let server_db = Entity::find()
.filter(Column::IdDiscord.eq(id))
.one(db)
.await
.unwrap();
let reply = match server_db {
Some(x) => x.id,
None => {
let activeModel = ActiveModel {
id: sea_orm::ActiveValue::NotSet,
name: sea_orm::ActiveValue::Set(name),
id_discord: sea_orm::ActiveValue::Set(id),
};
activeModel.insert(db).await.unwrap().id
}
};
Ok(reply)
}
}
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]