add basic guild create + message create endpoints

This commit is contained in:
sam 2024-01-20 16:43:03 +01:00
parent 5b23095520
commit e57bff00c2
Signed by: sam
GPG key ID: B4EF20DDE721CAA1
27 changed files with 367 additions and 36 deletions

47
chat/src/db/channel.rs Normal file
View file

@ -0,0 +1,47 @@
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<String>,
}
pub async fn create_channel(
executor: impl PgExecutor<'_>,
guild_id: &str,
name: &str,
topic: Option<String>,
) -> Result<Channel> {
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<Channel, FoxError> {
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)
}