chatthing/crates/backend/src/chat.rs

29 lines
651 B
Rust

use axum::{
extract::{Path, State},
response::Html,
Json,
};
use maud::{html, Render};
use sqlx::{Pool, Postgres};
use uuid::Uuid;
use crate::model::{Chat, Message};
pub async fn get(
Path(url_path): Path<String>,
State(pool): State<Pool<Postgres>>,
) -> Json<Vec<Message>> {
let chat = sqlx::query_as!(Chat, r#"select * from chats where url_path = $1"#, url_path)
.fetch_one(&pool)
.await
.unwrap();
let messages = sqlx::query_as!(
Message,
r#"select * from messages where chat_id = $1"#,
chat.id
)
.fetch_all(&pool)
.await
.unwrap();
Json(messages)
}