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"] }
chrono = { version = "0.4.38", features = ["serde", "alloc"] }
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::http::StatusCode;
use axum::Json;
use chrono::{DateTime, Utc};
use futures::future::join_all;
use serde_json::{json, Map, Value};
use sqlx::{PgPool, Row};
use sqlx::{PgPool, query, Row};
use sqlx::postgres::PgRow;
use uuid::Uuid;
pub async fn get_post_list(
State(db_connection): State<PgPool>,
) -> Result<Json<Value>, Json<Value>> {
let post_list =
sqlx::query("select post_id, title, summary, last_update, first_update from post")
.fetch_all(&db_connection)
.await
.unwrap();
) -> Result<Json<Value>, (StatusCode, Json<Value>)> {
let post_list = sqlx::query(
"select post_id, title, sub_title, summary, last_update, first_update, header_img, category from post",
)
.fetch_all(&db_connection)
.await
.unwrap();
let mut res = Map::new();
let posts: Vec<Value> = post_list
let posts: Vec<_> = post_list
.into_iter()
.map(|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)
})
.map(|post| analyze_post(&db_connection, post))
.collect();
let posts = join_all(posts).await;
res.insert("posts".to_string(), Value::from(posts));
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
match query(
r##"
INSERT INTO Post (post_id, title, summary, content, last_update, first_update, sub_title)
VALUES ($1, $2, $3, $4, $5, $6, $7)
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, $8, $9)
"##,
)
.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())
.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)
.await
{
+2
View File
@@ -5,6 +5,8 @@ CREATE TABLE IF NOT EXISTS Post
(
post_id UUID PRIMARY KEY,
title VARCHAR(255) NOT NULL,
category VARCHAR(255) NOT NULL,
header_img TEXT,
summary TEXT,
sub_title TEXT,
content TEXT NOT NULL,