wip: impl scheduler, import posts task
This commit is contained in:
42
backend/task/src/tasks/import_posts.rs
Normal file
42
backend/task/src/tasks/import_posts.rs
Normal file
@ -0,0 +1,42 @@
|
||||
pub async fn import_posts(dir_path: &str, pool: &sqlx::Pool<sqlx::Postgres>) {
|
||||
println!("hello from import_posts");
|
||||
let entries = std::fs::read_dir(dir_path).unwrap();
|
||||
|
||||
for f in entries {
|
||||
let file = f.unwrap();
|
||||
let file_path = file.path();
|
||||
if file_path.is_file() {
|
||||
let file_name = file.file_name().to_str().unwrap();
|
||||
let exists = sqlx::query_as::<_, FilenameExists>(
|
||||
"SELECT EXISTS(SELECT 1 FROM posts WHERE filename = $1)",
|
||||
)
|
||||
.bind(file_name.clone())
|
||||
.fetch_one(pool)
|
||||
.await
|
||||
.unwrap()
|
||||
.filename;
|
||||
|
||||
if !exists.is_empty() {
|
||||
let file_md_contents = std::fs::read_to_string(file).unwrap().as_str();
|
||||
let content = markdown::to_html(file_md_contents);
|
||||
|
||||
sqlx::query_as::<_, InsertPosts>("INSERT INTO posts (title, body) VALUES ($1, $2)")
|
||||
.bind(file_name.clone())
|
||||
.fetch_one(pool)
|
||||
.await
|
||||
.unwrap();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, sqlx::FromRow)]
|
||||
struct FilenameExists {
|
||||
filename: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, sqlx::FromRow)]
|
||||
struct InsertPosts {
|
||||
title: String,
|
||||
body: String,
|
||||
}
|
Reference in New Issue
Block a user