ref(backend/api/get/get_post_list) add category, header_img

This commit is contained in:
lumither committed 2024-05-12 23:02:33 +08:00
1 parent fd8b760b7f
commit 1a2932a194
4 files changed
+64 -26

No files matched your search

+1
View File
@@ -15,3 +15,4 @@ uuid = { version = "1.8.0", features = ["v4", "fast-rng", "serde"] }
sqlx = { version = "0.7.4", features = ["runtime-tokio", "tls-native-tls", "postgres", "chrono", "uuid"] } sqlx = { version = "0.7.4", features = ["runtime-tokio", "tls-native-tls", "postgres", "chrono", "uuid"] }
chrono = { version = "0.4.38", features = ["serde", "alloc"] } chrono = { version = "0.4.38", features = ["serde", "alloc"] }
regex = "1.10.4" regex = "1.10.4"
futures = { version = "0.3.30", features = ["executor"] }
+49 -24
View File
@@ -1,40 +1,65 @@
use axum::extract::State; use axum::extract::State;
use axum::http::StatusCode;
use axum::Json; use axum::Json;
use chrono::{DateTime, Utc}; use chrono::{DateTime, Utc};
use futures::future::join_all;
use serde_json::{json, Map, Value}; use serde_json::{json, Map, Value};
use sqlx::{PgPool, Row}; use sqlx::{PgPool, query, Row};
use sqlx::postgres::PgRow;
use uuid::Uuid; use uuid::Uuid;
pub async fn get_post_list( pub async fn get_post_list(
State(db_connection): State<PgPool>, State(db_connection): State<PgPool>,
) -> Result<Json<Value>, Json<Value>> { ) -> Result<Json<Value>, (StatusCode, Json<Value>)> {
let post_list = let post_list = sqlx::query(
sqlx::query("select post_id, title, summary, last_update, first_update from post") "select post_id, title, sub_title, summary, last_update, first_update, header_img, category from post",
.fetch_all(&db_connection) )
.await .fetch_all(&db_connection)
.unwrap(); .await
.unwrap();
let mut res = Map::new(); let mut res = Map::new();
let posts: Vec<Value> = post_list let posts: Vec<_> = post_list
.into_iter() .into_iter()
.map(|post| { .map(|post| analyze_post(&db_connection, post))
let mut tmp = Map::new();
let post_id: Uuid = post.get("post_id");
let title: String = post.get("title");
let summary: String = post.get("summary");
let last_update: DateTime<Utc> = post.get("last_update");
let first_update: DateTime<Utc> = post.get("first_update");
tmp.insert("post_id".to_string(), json!(post_id.to_string()));
tmp.insert("title".to_string(), json!(title));
tmp.insert("summary".to_string(), json!(summary));
tmp.insert("last_update".to_string(), json!(last_update));
tmp.insert("first_update".to_string(), json!(first_update));
Value::from(tmp)
})
.collect(); .collect();
let posts = join_all(posts).await;
res.insert("posts".to_string(), Value::from(posts)); res.insert("posts".to_string(), Value::from(posts));
Ok(Json::from(Value::from(res))) Ok(Json::from(Value::from(res)))
} }
async fn analyze_post(db_connection: &PgPool, post: PgRow) -> Value {
let mut tmp = Map::new();
let post_id: Uuid = post.get("post_id");
let title: String = post.get("title");
let category: String = post.get("category");
let header_img: String = post.get("header_img");
let sub_title: String = post.get("sub_title");
let summary: String = post.get("summary");
let last_update: DateTime<Utc> = post.get("last_update");
let first_update: DateTime<Utc> = post.get("first_update");
let tags_future = query("SELECT tag FROM Tag WHERE post_id = $1")
.bind(post_id)
.fetch_all(db_connection);
let tags: Vec<String> = match tags_future.await {
Ok(res) => res.iter().map(|tag| tag.get::<String, _>("tag")).collect(),
Err(_) => {
vec![]
}
};
tmp.insert("post_id".to_string(), json!(post_id.to_string()));
tmp.insert("title".to_string(), json!(title));
tmp.insert("category".to_string(), json!(category));
tmp.insert("header_img".to_string(), json!(header_img));
tmp.insert("sub_title".to_string(), json!(sub_title));
tmp.insert("summary".to_string(), json!(summary));
tmp.insert("last_update".to_string(), json!(last_update));
tmp.insert("first_update".to_string(), json!(first_update));
tmp.insert("tags".to_string(), json!(tags));
Value::from(tmp)
}
+12 -2
View File
@@ -59,8 +59,8 @@ pub async fn refresh(
// table Post // table Post
match query( match query(
r##" r##"
INSERT INTO Post (post_id, title, summary, content, last_update, first_update, sub_title) INSERT INTO Post (post_id, title, summary, content, last_update, first_update, sub_title, category, header_img)
VALUES ($1, $2, $3, $4, $5, $6, $7) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)
"##, "##,
) )
.bind(Uuid::from_str(post["post_id"].as_str().unwrap()).unwrap()) .bind(Uuid::from_str(post["post_id"].as_str().unwrap()).unwrap())
@@ -94,6 +94,16 @@ VALUES ($1, $2, $3, $4, $5, $6, $7)
.and_then(|sub_title| sub_title.as_str()) .and_then(|sub_title| sub_title.as_str())
.unwrap_or(""), .unwrap_or(""),
) )
.bind(
meta.get("category")
.and_then(|category| category.as_str())
.unwrap_or("N/A"),
)
.bind(
meta.get("header_img")
.and_then(|header_img| header_img.as_str())
.unwrap_or(""),
)
.execute(&db_connection) .execute(&db_connection)
.await .await
{ {
+2
View File
@@ -5,6 +5,8 @@ CREATE TABLE IF NOT EXISTS Post
( (
post_id UUID PRIMARY KEY, post_id UUID PRIMARY KEY,
title VARCHAR(255) NOT NULL, title VARCHAR(255) NOT NULL,
category VARCHAR(255) NOT NULL,
header_img TEXT,
summary TEXT, summary TEXT,
sub_title TEXT, sub_title TEXT,
content TEXT NOT NULL, content TEXT NOT NULL,