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)
}

71
chat/src/db/guild.rs Normal file
View file

@ -0,0 +1,71 @@
use eyre::Result;
use foxchat::FoxError;
use sqlx::PgExecutor;
use ulid::Ulid;
pub struct Guild {
pub id: String,
pub owner_id: String,
pub name: String,
}
pub async fn create_guild(
executor: impl PgExecutor<'_>,
owner_id: &str,
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,
name
)
.fetch_one(executor)
.await?;
Ok(guild)
}
pub async fn join_guild(
executor: impl PgExecutor<'_>,
guild_id: &str,
user_id: &str,
) -> 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
)
.execute(executor)
.await?;
Ok(())
}
pub async fn get_guild(
executor: impl PgExecutor<'_>,
guild_id: &str,
user_id: &str,
) -> 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
)
.fetch_one(executor)
.await
.map_err(|e| match e {
sqlx::Error::RowNotFound => FoxError::NotInGuild,
_ => {
tracing::error!("database error: {}", e);
return FoxError::DatabaseError;
}
})?;
Ok(guild)
}

33
chat/src/db/message.rs Normal file
View file

@ -0,0 +1,33 @@
use chrono::{DateTime, Utc};
use eyre::Result;
use foxchat::model::http::channel::CreateMessageParams;
use sqlx::PgExecutor;
use ulid::Ulid;
pub struct Message {
pub id: String,
pub channel_id: String,
pub author_id: String,
pub updated_at: DateTime<Utc>,
pub content: String,
}
pub async fn create_message(
executor: impl PgExecutor<'_>,
channel_id: &str,
user_id: &str,
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,
params.content,
)
.fetch_one(executor)
.await?;
Ok(message)
}

View file

@ -1,3 +1,7 @@
pub mod guild;
pub mod channel;
pub mod message;
use eyre::{OptionExt, Result};
use rsa::pkcs1::{EncodeRsaPrivateKey, EncodeRsaPublicKey, LineEnding};
use rsa::{RsaPrivateKey, RsaPublicKey};