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
+71
-6
No files matched your search
+5
-1
@@ -17,4 +17,8 @@ ON CONFLICT (post_id)
|
|||||||
summary = excluded.summary,
|
summary = excluded.summary,
|
||||||
content = excluded.content,
|
content = excluded.content,
|
||||||
last_update = excluded.last_update,
|
last_update = excluded.last_update,
|
||||||
first_update = excluded.first_update
|
first_update = excluded.first_update;
|
||||||
|
|
||||||
|
SELECT *
|
||||||
|
FROM Post
|
||||||
|
WHERE post_id = $1;
|
||||||
@@ -1,12 +1,69 @@
|
|||||||
|
use std::str::FromStr;
|
||||||
|
|
||||||
use axum::extract::{Path, State};
|
use axum::extract::{Path, State};
|
||||||
use axum::Json;
|
use axum::Json;
|
||||||
use serde_json::Value;
|
use chrono::NaiveDateTime;
|
||||||
use sqlx::PgPool;
|
use serde_json::{json, Map, Value};
|
||||||
|
use sqlx::{PgPool, query, Row};
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
pub async fn get_post(
|
pub async fn get_post(
|
||||||
State(_db_connection): State<PgPool>,
|
State(db_connection): State<PgPool>,
|
||||||
Path(post_id): Path<String>,
|
Path(post_id): Path<String>,
|
||||||
) -> Result<Json<Value>, Json<Value>> {
|
) -> Result<Json<Value>, Json<Value>> {
|
||||||
dbg!(&post_id);
|
let meta = query(
|
||||||
todo!()
|
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)))
|
||||||
}
|
}
|
||||||
@@ -6,3 +6,7 @@ CREATE TABLE IF NOT EXISTS Tag
|
|||||||
|
|
||||||
INSERT INTO Tag
|
INSERT INTO Tag
|
||||||
VALUES ($1, $2);
|
VALUES ($1, $2);
|
||||||
|
|
||||||
|
SELECT *
|
||||||
|
FROM Tag
|
||||||
|
WHERE post_id = $1;
|
||||||
Reference in new issue
Block a user