modified task manager to add markdown, import_posts task
the import_posts task isn't perfect, we need some relation between the author_id and the author itself
This commit is contained in:
parent
80081b75d5
commit
3afff00b29
2
backend/task/.gitignore
vendored
2
backend/task/.gitignore
vendored
@ -1 +1,3 @@
|
|||||||
target/
|
target/
|
||||||
|
.env
|
||||||
|
app/
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
use chrono::{prelude::*, Duration};
|
use chrono::{prelude::*, Duration};
|
||||||
// use once_cell::sync::Lazy;
|
// use once_cell::sync::Lazy;
|
||||||
use sqlx::{postgres::PgPoolOptions, PgPool, Pool, Postgres};
|
use sqlx::{postgres::PgPoolOptions, Pool, Postgres};
|
||||||
use std::env;
|
use std::env;
|
||||||
|
|
||||||
mod tasks;
|
mod tasks;
|
||||||
@ -83,10 +83,12 @@ impl<'a> TaskManager<'a> {
|
|||||||
scheduler.add(job_scheduler::Job::new(j.schedule.parse().unwrap(), || {
|
scheduler.add(job_scheduler::Job::new(j.schedule.parse().unwrap(), || {
|
||||||
println!("Starting task name: {:?}", j.task_name);
|
println!("Starting task name: {:?}", j.task_name);
|
||||||
|
|
||||||
match j.task_id {
|
async {
|
||||||
1 => tasks::import_posts::import_posts("/app", &self.pool),
|
match j.task_id {
|
||||||
_ => panic!(),
|
1 => tasks::import_posts::import_posts("/app", &self.pool).await,
|
||||||
}
|
_ => panic!(),
|
||||||
|
}
|
||||||
|
};
|
||||||
}));
|
}));
|
||||||
})
|
})
|
||||||
.collect::<Vec<_>>();
|
.collect::<Vec<_>>();
|
||||||
|
@ -1,35 +1,56 @@
|
|||||||
|
use std::fs;
|
||||||
|
use std::path;
|
||||||
|
|
||||||
pub async fn import_posts(dir_path: &str, pool: &sqlx::Pool<sqlx::Postgres>) {
|
pub async fn import_posts(dir_path: &str, pool: &sqlx::Pool<sqlx::Postgres>) {
|
||||||
println!("hello from import_posts");
|
println!("hello from import_posts");
|
||||||
let entries = std::fs::read_dir(dir_path).unwrap();
|
let entries = fs::read_dir(dir_path).unwrap();
|
||||||
|
let options = MarkdownOptions {
|
||||||
|
options: markdown::Constructs::gfm(),
|
||||||
|
};
|
||||||
|
|
||||||
for f in entries {
|
for f in entries {
|
||||||
let file = f.unwrap();
|
let file = f.unwrap();
|
||||||
let file_path = file.path();
|
let file_path = file.path();
|
||||||
if file_path.is_file() {
|
if file_path.is_file() {
|
||||||
let file_name = file.file_name().to_str().unwrap();
|
let file_name = file.file_name();
|
||||||
|
let file_name_final = &file_name.to_str().unwrap();
|
||||||
let exists = sqlx::query_as::<_, FilenameExists>(
|
let exists = sqlx::query_as::<_, FilenameExists>(
|
||||||
"SELECT EXISTS(SELECT 1 FROM posts WHERE filename = $1)",
|
"SELECT EXISTS(SELECT 1 FROM posts WHERE filename = $1)",
|
||||||
)
|
)
|
||||||
.bind(file_name.clone())
|
.bind(file_name_final)
|
||||||
.fetch_one(pool)
|
.fetch_one(pool)
|
||||||
.await
|
.await
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.filename;
|
.filename;
|
||||||
|
|
||||||
if !exists.is_empty() {
|
if !exists.is_empty() {
|
||||||
let file_md_contents = std::fs::read_to_string(file).unwrap().as_str();
|
println!(
|
||||||
let content = markdown::to_html(file_md_contents);
|
"File does not exist! Inserting: {:?}",
|
||||||
|
file_path.file_name()
|
||||||
|
);
|
||||||
|
let file_md_contents = process_read_file(file_path, &options);
|
||||||
|
let content = markdown::to_html(&file_md_contents);
|
||||||
|
|
||||||
sqlx::query_as::<_, InsertPosts>("INSERT INTO posts (title, body) VALUES ($1, $2)")
|
sqlx::query_as::<_, InsertPosts>(
|
||||||
.bind(file_name.clone())
|
"INSERT INTO posts (title, body, filename, author_id) VALUES ($1, $2, $3, $4) RETURNING (title, body, filename, author_id)",
|
||||||
.fetch_one(pool)
|
)
|
||||||
.await
|
.bind(String::from("Hello world from Postgres!"))
|
||||||
.unwrap();
|
.bind(content)
|
||||||
|
.bind(file_name_final)
|
||||||
|
.bind(1)
|
||||||
|
.fetch_one(pool)
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn process_read_file<P: AsRef<path::Path>>(path: P, md_opts: &MarkdownOptions) -> String {
|
||||||
|
let file_contents = fs::read_to_string(path).unwrap();
|
||||||
|
markdown::to_html(file_contents.as_str())
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug, sqlx::FromRow)]
|
#[derive(Debug, sqlx::FromRow)]
|
||||||
struct FilenameExists {
|
struct FilenameExists {
|
||||||
filename: String,
|
filename: String,
|
||||||
@ -39,4 +60,10 @@ struct FilenameExists {
|
|||||||
struct InsertPosts {
|
struct InsertPosts {
|
||||||
title: String,
|
title: String,
|
||||||
body: String,
|
body: String,
|
||||||
|
filename: String,
|
||||||
|
author_id: i32,
|
||||||
|
}
|
||||||
|
|
||||||
|
struct MarkdownOptions {
|
||||||
|
options: markdown::Constructs,
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user