foxchat/chat/src/db/message.rs
2024-01-20 23:09:19 +01:00

33 lines
873 B
Rust

use chrono::{DateTime, Utc};
use eyre::Result;
use foxchat::{Id, model::http::channel::CreateMessageParams, id::{ChannelType, UserType, MessageType}};
use sqlx::PgExecutor;
use ulid::Ulid;
pub struct Message {
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: &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.0,
user_id.0,
params.content,
)
.fetch_one(executor)
.await?;
Ok(message)
}