use std::str::FromStr; use axum::extract::{Path, State}; use axum::Json; use chrono::NaiveDateTime; use serde_json::{json, Map, Value}; use sqlx::{PgPool, query, Row}; use uuid::Uuid; pub async fn get_post( State(db_connection): State, Path(post_id): Path, ) -> Result, Json> { let meta = query( r#" SELECT * FROM Post WHERE post_id = $1; "#, ) .bind(Uuid::from_str(post_id.as_str()).expect("Failed to parse UUID")) .fetch_one(&db_connection) .await .expect("Failed to execute db query"); let tags = query( r#" SELECT tag FROM Tag WHERE post_id = $1; "#, ) .bind(Uuid::from_str(post_id.as_str()).expect("Failed to parse UUID")) .fetch_all(&db_connection) .await .expect("Failed to execute db query"); let mut res = Map::new(); res.insert( "title".to_string(), json!(meta.get::("title")), ); res.insert( "summary".to_string(), json!(meta.get::("summary")), ); res.insert( "content".to_string(), json!(meta.get::("content")), ); res.insert( "last_update".to_string(), json!(meta.get::("last_update")), ); res.insert( "first_update".to_string(), json!(meta.get::("first_update")), ); let tags: Vec = tags .iter() .map(|tag| { let tag: String = tag.get("tag"); tag }) .collect(); res.insert("tags".to_string(), Value::from(tags)); Ok(Json::from(Value::from(res))) }