robson-gateway-webhook 0.1.0

Rust async agent orchestrator for automated development workflows
Documentation
use anyhow::Result;
use chrono::Utc;
use sea_orm::entity::prelude::*;
use sea_orm::ActiveValue::Set;
use serde::{Deserialize, Serialize};

/// A registered webhook subscriber that receives HTTP POST callbacks
/// when a MessageDelivered event occurs.
#[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel, Serialize, Deserialize)]
#[sea_orm(table_name = "subscribers")]
pub struct Model {
    #[sea_orm(primary_key)]
    pub id: i32,
    #[sea_orm(unique)]
    pub callback_url: String,
    pub description: Option<String>,
    pub created_at: String,
}

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {}

impl ActiveModelBehavior for ActiveModel {}

impl Model {
    pub async fn register(
        db: &DatabaseConnection,
        callback_url: &str,
        description: Option<&str>,
    ) -> Result<i32> {
        let now = Utc::now().to_rfc3339();
        let active = ActiveModel {
            callback_url: Set(callback_url.to_string()),
            description: Set(description.map(|s| s.to_string())),
            created_at: Set(now),
            ..Default::default()
        };
        let result = active.insert(db).await?;
        Ok(result.id)
    }

    pub async fn find_all(db: &DatabaseConnection) -> Result<Vec<Model>> {
        let subs = Entity::find().all(db).await?;
        Ok(subs)
    }

    pub async fn delete(db: &DatabaseConnection, id: i32) -> Result<u64> {
        let result = Entity::delete_by_id(id).exec(db).await?;
        Ok(result.rows_affected)
    }
}