foxchat/chat/src/db/message.rs

34 lines
873 B
Rust
Raw Normal View History

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