chat: add Id<T> type

This commit is contained in:
sam 2024-01-20 23:09:19 +01:00
parent e57bff00c2
commit ce543e7ee1
Signed by: sam
GPG key ID: B4EF20DDE721CAA1
8 changed files with 146 additions and 43 deletions

View file

@ -1,18 +1,18 @@
use eyre::Result;
use foxchat::FoxError;
use foxchat::{FoxError, Id, id::{ChannelType, GuildType}};
use sqlx::PgExecutor;
use ulid::Ulid;
pub struct Channel {
pub id: String,
pub guild_id: String,
pub id: Id<ChannelType>,
pub guild_id: Id<GuildType>,
pub name: String,
pub topic: Option<String>,
}
pub async fn create_channel(
executor: impl PgExecutor<'_>,
guild_id: &str,
guild_id: &Id<GuildType>,
name: &str,
topic: Option<String>,
) -> Result<Channel> {
@ -20,7 +20,7 @@ pub async fn create_channel(
Channel,
"insert into channels (id, guild_id, name, topic) values ($1, $2, $3, $4) returning *",
Ulid::new().to_string(),
guild_id,
guild_id.0,
name,
topic
)
@ -30,8 +30,8 @@ pub async fn create_channel(
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)
pub async fn get_channel(executor: impl PgExecutor<'_>, channel_id: &Id<ChannelType>) -> Result<Channel, FoxError> {
let channel = sqlx::query_as!(Channel, "select * from channels where id = $1", channel_id.0)
.fetch_one(executor)
.await
.map_err(|e| match e {