wip: impl scheduler, import posts task

This commit is contained in:
2024-09-22 04:10:29 -04:00
parent 4cb3983a9b
commit 80081b75d5
5 changed files with 102 additions and 29 deletions

View 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,
}