mirror of
https://github.com/Lumither/nocturne.git
synced 2026-10-17 10:26:21 +00:00
feat(backend/api/get/post/<post_id>) implement api
This commit is contained in:
3 files changed
+72
-7
No files matched your search
@@ -1,12 +1,69 @@
|
||||
use std::str::FromStr;
|
||||
|
||||
use axum::extract::{Path, State};
|
||||
use axum::Json;
|
||||
use serde_json::Value;
|
||||
use sqlx::PgPool;
|
||||
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<PgPool>,
|
||||
State(db_connection): State<PgPool>,
|
||||
Path(post_id): Path<String>,
|
||||
) -> Result<Json<Value>, Json<Value>> {
|
||||
dbg!(&post_id);
|
||||
todo!()
|
||||
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::<String, &str>("title")),
|
||||
);
|
||||
res.insert(
|
||||
"summary".to_string(),
|
||||
json!(meta.get::<String, &str>("summary")),
|
||||
);
|
||||
res.insert(
|
||||
"content".to_string(),
|
||||
json!(meta.get::<String, &str>("content")),
|
||||
);
|
||||
res.insert(
|
||||
"last_update".to_string(),
|
||||
json!(meta.get::<NaiveDateTime, &str>("last_update")),
|
||||
);
|
||||
res.insert(
|
||||
"first_update".to_string(),
|
||||
json!(meta.get::<NaiveDateTime, &str>("first_update")),
|
||||
);
|
||||
|
||||
let tags: Vec<String> = 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)))
|
||||
}
|
||||
Reference in new issue
Block a user