diff --git a/backend/Cargo.toml b/backend/Cargo.toml index 94c4830..6831286 100644 --- a/backend/Cargo.toml +++ b/backend/Cargo.toml @@ -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"] } diff --git a/backend/src/api/get/get_post_list.rs b/backend/src/api/get/get_post_list.rs index 777b5d7..329a807 100644 --- a/backend/src/api/get/get_post_list.rs +++ b/backend/src/api/get/get_post_list.rs @@ -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, -) -> Result, Json> { - let post_list = - sqlx::query("select post_id, title, summary, last_update, first_update from post") - .fetch_all(&db_connection) - .await - .unwrap(); +) -> Result, (StatusCode, Json)> { + 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 = 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 = post.get("last_update"); - let first_update: DateTime = 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 = post.get("last_update"); + let first_update: DateTime = 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 = match tags_future.await { + Ok(res) => res.iter().map(|tag| tag.get::("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) +} diff --git a/backend/src/api/post/refresh.rs b/backend/src/api/post/refresh.rs index 6e9b416..ac3c318 100644 --- a/backend/src/api/post/refresh.rs +++ b/backend/src/api/post/refresh.rs @@ -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 { diff --git a/backend/src/model/mod.rs b/backend/src/model/mod.rs index 04f359d..b477e57 100644 --- a/backend/src/model/mod.rs +++ b/backend/src/model/mod.rs @@ -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,