use eyre::Result; use foxchat::FoxError; use sqlx::PgExecutor; use ulid::Ulid; pub struct Channel { pub id: String, pub guild_id: String, pub name: String, pub topic: Option, } pub async fn create_channel( executor: impl PgExecutor<'_>, guild_id: &str, name: &str, topic: Option, ) -> Result { let channel = sqlx::query_as!( Channel, "insert into channels (id, guild_id, name, topic) values ($1, $2, $3, $4) returning *", Ulid::new().to_string(), guild_id, name, topic ) .fetch_one(executor) .await?; Ok(channel) } pub async fn get_channel(executor: impl PgExecutor<'_>, channel_id: &str) -> Result { let channel = sqlx::query_as!(Channel, "select * from channels where id = $1", channel_id) .fetch_one(executor) .await .map_err(|e| match e { sqlx::Error::RowNotFound => FoxError::NotInGuild, _ => { tracing::error!("database error: {}", e); return FoxError::DatabaseError; } })?; Ok(channel) }