From 70016f508f812860091b6a924b3aaed782d0d7f7 Mon Sep 17 00:00:00 2001 From: Lumither Tao Date: Fri, 3 May 2024 22:42:14 +0800 Subject: [PATCH] feat(idx) finish idx system --- backend/post.sql | 2 + backend/src/api/post/refresh.rs | 81 ++++++++++++++++++++++++++++++++- backend/tag.sql | 3 ++ 3 files changed, 85 insertions(+), 1 deletion(-) diff --git a/backend/post.sql b/backend/post.sql index 3877d12..9460010 100644 --- a/backend/post.sql +++ b/backend/post.sql @@ -7,6 +7,8 @@ CREATE TABLE IF NOT EXISTS Post last_update TIMESTAMP ); +drop table if exists post; + INSERT INTO Post (post_id, title, summary, content, last_update) VALUES ($1, $2, $3, $4, $5) ON CONFLICT (post_id) diff --git a/backend/src/api/post/refresh.rs b/backend/src/api/post/refresh.rs index 6d59e60..e0b1d20 100644 --- a/backend/src/api/post/refresh.rs +++ b/backend/src/api/post/refresh.rs @@ -7,6 +7,7 @@ use axum::Json; use serde_json::{json, Value}; use sqlx::{PgPool, query}; +use crate::markdown::meta::parse_meta; use crate::model::Post; pub async fn refresh( @@ -27,12 +28,57 @@ pub async fn refresh( let md_list = search_md(Path::new(&data_repo_dir)); + // dump old idx + query("DROP TABLE IF EXISTS Post;") + .execute(&db_connection) + .await + .unwrap(); + query("DROP TABLE IF EXISTS Tag;") + .execute(&db_connection) + .await + .unwrap(); + + // generate new idx + let post_list: Vec = md_list .into_iter() .map(|post| Post::from_path(post).unwrap()) .collect(); + query( + r##" +CREATE TABLE IF NOT EXISTS Post +( + post_id UUID PRIMARY KEY, + title VARCHAR(255) NOT NULL, + summary TEXT NOT NULL, + content TEXT NOT NULL, + last_update TIMESTAMP +); + "##, + ) + .execute(&db_connection) + .await + .unwrap(); + + query( + r##" +CREATE TABLE IF NOT EXISTS Tag +( + post_id UUID NOT NULL, + tag VARCHAR(255) NOT NULL +); + "##, + ) + .execute(&db_connection) + .await + .unwrap(); + + // todo: concurrency for post in post_list { + let meta = parse_meta(&post.content); + + // table Post match query( r##" INSERT INTO Post (post_id, title, summary, content, last_update) @@ -47,7 +93,7 @@ ON CONFLICT (post_id) .bind(post.post_id) .bind(post.title) .bind(post.summary) - .bind(post.content) + .bind(&post.content) .bind(post.last_update) .execute(&db_connection) .await @@ -60,6 +106,39 @@ ON CONFLICT (post_id) )); } } + + // table Tag + if let Some(tags_string) = meta.get("tags").to_owned() { + // parse tags string + let parsed: Value = serde_json::from_str(tags_string).unwrap(); + let string_array = parsed.as_array().unwrap(); + let tags: Vec = string_array + .iter() + .map(|value| value.as_str().unwrap().to_string()) + .collect(); + + for tag in tags { + match query( + r#" +INSERT INTO Tag +VALUES ($1, $2); + "#, + ) + .bind(post.post_id) + .bind(tag) + .execute(&db_connection) + .await + { + Ok(_) => {} + Err(e) => { + return Err(( + StatusCode::INTERNAL_SERVER_ERROR, + Json::from(json!(format!("{{error: {}}}", e.to_string()))), + )); + } + } + } + } } Ok(StatusCode::OK) diff --git a/backend/tag.sql b/backend/tag.sql index 6c84b76..be061aa 100644 --- a/backend/tag.sql +++ b/backend/tag.sql @@ -3,3 +3,6 @@ CREATE TABLE IF NOT EXISTS Tag post_id UUID NOT NULL, tag VARCHAR(255) NOT NULL ); + +INSERT INTO Tag +VALUES ($1, $2); \ No newline at end of file