mirror of
https://github.com/Lumither/nocturne.git
synced 2026-10-17 18:36:21 +00:00
feat(idx) finish idx system
This commit is contained in:
3 files changed
+85
-1
No files matched your search
@@ -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)
|
||||
|
||||
@@ -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<Post> = 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> = 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)
|
||||
|
||||
@@ -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);
|
||||
Reference in new issue
Block a user