wip refactoring
This commit is contained in:
@@ -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")]
|
||||
|
||||
@@ -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)]
|
||||
|
||||
Reference in New Issue
Block a user