Skip to main content

robson_core/entities/
gateway.rs

1use anyhow::Result;
2use sea_orm::entity::prelude::*;
3use serde::{Deserialize, Serialize};
4
5/// A registered communication gateway (e.g. "slack", "webhook").
6///
7/// Each `Conversation` row references a `Gateway` via `gateway_id`, which tells the
8/// SensoriumLoop delivery loop which gateway to use when routing `ProcessEvent`s back
9/// to the originating channel.
10#[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel, Serialize, Deserialize)]
11#[sea_orm(table_name = "gateways")]
12pub struct Model {
13    #[sea_orm(primary_key)]
14    pub id: i32,
15    #[sea_orm(unique)]
16    pub name: String,
17}
18
19#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
20pub enum Relation {}
21
22impl ActiveModelBehavior for ActiveModel {}
23
24impl Model {
25    /// Look up a gateway by its unique name (e.g. `"slack"`, `"webhook"`).
26    pub async fn find_by_name(db: &DatabaseConnection, name: &str) -> Result<Option<Model>> {
27        Ok(Entity::find().filter(Column::Name.eq(name)).one(db).await?)
28    }
29
30    /// Look up a gateway by primary key.
31    pub async fn find_by_id(db: &DatabaseConnection, id: i32) -> Result<Option<Model>> {
32        Ok(Entity::find_by_id(id).one(db).await?)
33    }
34
35    /// Return all registered gateways.
36    pub async fn find_all(db: &DatabaseConnection) -> Result<Vec<Model>> {
37        Ok(Entity::find().all(db).await?)
38    }
39}