added publish date

This commit is contained in:
2025-07-17 19:35:45 -04:00
parent 7875c23b67
commit 71b3b1f42d

View File

@@ -2,6 +2,7 @@ use std::fs;
use std::io::Read; use std::io::Read;
use crate::utils::task_log; use crate::utils::task_log;
use chrono::{DateTime, FixedOffset, Utc};
use serde::{Deserialize, Deserializer}; use serde::{Deserialize, Deserializer};
pub fn register(pool: &sqlx::Pool<sqlx::Postgres>) { pub fn register(pool: &sqlx::Pool<sqlx::Postgres>) {
@@ -19,21 +20,15 @@ async fn import_posts(
// Start task logging // Start task logging
let task = task_log::start(1, pool).await?; let task = task_log::start(1, pool).await?;
// Setup markdown options
let options = MarkdownOptions { let options = MarkdownOptions {
options: markdown::Constructs::gfm(), options: markdown::Constructs::gfm(),
}; };
// Read directory contents
let entries = fs::read_dir(dir_path)?; let entries = fs::read_dir(dir_path)?;
// Process each file
for entry_result in entries { for entry_result in entries {
let file = entry_result?; let file = entry_result?;
let file_path = file.path(); let file_path = file.path();
// Skip non-file entries
if !file_path.is_file() { if !file_path.is_file() {
continue; continue;
} }
@@ -71,22 +66,23 @@ async fn import_posts(
let content = let content =
markdown::to_html_with_options(&document.content, &markdown::Options::default()); markdown::to_html_with_options(&document.content, &markdown::Options::default());
println!("{:?}", content);
let title = document.metadata.title; let title = document.metadata.title;
let pub_date =
DateTime::parse_from_str(document.metadata.date.as_ref(), "%Y-%m-%d %H:%M:%S %z")?;
let content_final = content.unwrap(); let content_final = content.unwrap();
// Insert into database // Insert into database
let results = sqlx::query_as::<_, InsertPosts>( let results = sqlx::query_as::<_, InsertPosts>(
"INSERT INTO posts (title, body, filename, author_id) VALUES ($1, $2, $3, $4) RETURNING title, body, filename, author_id" "INSERT INTO posts (title, body, filename, publish_date, author_id) VALUES ($1, $2, $3, $4, $5) RETURNING title, body, filename, author_id"
) )
.bind(title) .bind(title)
.bind(content_final) .bind(content_final)
.bind(file_name_str) .bind(file_name_str)
.bind(pub_date)
.bind(1) // Consider making author_id a parameter .bind(1) // Consider making author_id a parameter
.fetch_one(pool) .fetch_one(pool)
.await?; .await?;
println!("{:?}", results);
println!("Successfully imported: {}", file_name_str); println!("Successfully imported: {}", file_name_str);
} else { } else {