use anyhow::Result;
use chrono::Utc;
use sea_orm::entity::prelude::*;
use sea_orm::ActiveValue::Set;
use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel, Serialize, Deserialize)]
#[sea_orm(table_name = "allowed_channels")]
pub struct Model {
#[sea_orm(primary_key)]
pub id: i32,
#[sea_orm(unique)]
pub channel_id: String,
pub channel_name: String,
pub created_at: String,
}
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {}
impl ActiveModelBehavior for ActiveModel {}
impl Model {
pub async fn insert(
db: &DatabaseConnection,
channel_id: &str,
channel_name: &str,
) -> Result<()> {
let now = Utc::now().to_rfc3339();
let active = ActiveModel {
channel_id: Set(channel_id.to_string()),
channel_name: Set(channel_name.to_string()),
created_at: Set(now),
..Default::default()
};
active.insert(db).await?;
Ok(())
}
pub async fn is_allowed(db: &DatabaseConnection, channel_id: &str) -> Result<bool> {
let found = Entity::find()
.filter(Column::ChannelId.eq(channel_id))
.one(db)
.await?;
Ok(found.is_some())
}
pub async fn find_all(db: &DatabaseConnection) -> Result<Vec<Model>> {
let rows = Entity::find().all(db).await?;
Ok(rows)
}
pub async fn delete_by_channel_id(db: &DatabaseConnection, channel_id: &str) -> Result<u64> {
let result = Entity::delete_many()
.filter(Column::ChannelId.eq(channel_id))
.exec(db)
.await?;
Ok(result.rows_affected)
}
}