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 {

View file

@ -1,24 +1,24 @@
use eyre::Result;
use foxchat::FoxError;
use foxchat::{FoxError, Id, id::{GuildType, UserType}};
use sqlx::PgExecutor;
use ulid::Ulid;
pub struct Guild {
pub id: String,
pub owner_id: String,
pub id: Id<GuildType>,
pub owner_id: Id<UserType>,
pub name: String,
}
pub async fn create_guild(
executor: impl PgExecutor<'_>,
owner_id: &str,
owner_id: &Id<UserType>,
name: &str,
) -> Result<Guild> {
let guild = sqlx::query_as!(
Guild,
"insert into guilds (id, owner_id, name) values ($1, $2, $3) returning *",
Ulid::new().to_string(),
owner_id,
owner_id.0,
name
)
.fetch_one(executor)
@ -29,13 +29,13 @@ pub async fn create_guild(
pub async fn join_guild(
executor: impl PgExecutor<'_>,
guild_id: &str,
user_id: &str,
guild_id: &Id<GuildType>,
user_id: &Id<UserType>,
) -> Result<()> {
sqlx::query!(
"insert into guilds_users (guild_id, user_id) values ($1, $2) on conflict (guild_id, user_id) do nothing",
guild_id,
user_id
guild_id.0,
user_id.0
)
.execute(executor)
.await?;
@ -45,16 +45,14 @@ pub async fn join_guild(
pub async fn get_guild(
executor: impl PgExecutor<'_>,
guild_id: &str,
user_id: &str,
guild_id: &Id<GuildType>,
user_id: &Id<UserType>,
) -> Result<Guild, FoxError> {
println!("guild id: {}, user id: {}", guild_id, user_id);
let guild = sqlx::query_as!(
Guild,
"select g.* from guilds_users u join guilds g on u.guild_id = g.id where u.guild_id = $1 and u.user_id = $2",
guild_id,
user_id
guild_id.0,
user_id.0
)
.fetch_one(executor)
.await

View file

@ -1,29 +1,29 @@
use chrono::{DateTime, Utc};
use eyre::Result;
use foxchat::model::http::channel::CreateMessageParams;
use foxchat::{Id, model::http::channel::CreateMessageParams, id::{ChannelType, UserType, MessageType}};
use sqlx::PgExecutor;
use ulid::Ulid;
pub struct Message {
pub id: String,
pub channel_id: String,
pub author_id: String,
pub id: Id<MessageType>,
pub channel_id: Id<ChannelType>,
pub author_id: Id<UserType>,
pub updated_at: DateTime<Utc>,
pub content: String,
}
pub async fn create_message(
executor: impl PgExecutor<'_>,
channel_id: &str,
user_id: &str,
channel_id: &Id<ChannelType>,
user_id: &Id<UserType>,
params: CreateMessageParams,
) -> Result<Message> {
let message = sqlx::query_as!(
Message,
"insert into messages (id, channel_id, author_id, content) values ($1, $2, $3, $4) returning *",
Ulid::new().to_string(),
channel_id,
user_id,
channel_id.0,
user_id.0,
params.content,
)
.fetch_one(executor)