rss-reader/src/demo.rs
2025-02-03 15:46:28 -08:00

76 lines
2.2 KiB
Rust

use chrono;
use sqlx;
use uuid::Uuid;
use crate::feeds::Feed;
pub async fn setup_demo_data(pool: &sqlx::SqlitePool) {
// Create admin user
let admin_id = Uuid::new_v4();
let admin_id_str = admin_id.to_string();
let admin_hash = bcrypt::hash("admin", bcrypt::DEFAULT_COST).unwrap();
let now = chrono::Utc::now().to_rfc3339();
sqlx::query(
"INSERT INTO users (id, username, password_hash, email, display_name, created_at, admin)
VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7)",
)
.bind(&admin_id_str)
.bind("admin")
.bind(&admin_hash)
.bind(Option::<String>::None)
.bind(Option::<String>::None)
.bind(&now)
.bind(true)
.execute(pool)
.await
.expect("Failed to create admin user");
// Create demo user
let demo_id = Uuid::new_v4();
let demo_id_str = demo_id.to_string();
let demo_hash = bcrypt::hash("demo", bcrypt::DEFAULT_COST).unwrap();
sqlx::query(
"INSERT INTO users (id, username, password_hash, email, display_name, created_at, admin)
VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7)",
)
.bind(&demo_id_str)
.bind("demo")
.bind(&demo_hash)
.bind(Option::<String>::None)
.bind(Option::<String>::None)
.bind(&now)
.bind(false)
.execute(pool)
.await
.expect("Failed to create demo user");
let feed = Feed::new(
"BBC News".to_string(),
"https://feeds.bbci.co.uk/news/world/us_and_canada/rss.xml"
.parse()
.unwrap(),
demo_id,
);
// TODO: This insert logic is substantially the same as Feed::write_to_database.
// Should find a way to unify these two code paths to avoid duplication.
sqlx::query(
"INSERT INTO feeds (feed_id, name, url, user_id, added_time, last_checked_time, categorization)
VALUES (?1, ?2, ?3, ?4, ?5, ?6, json(?7))",
)
.bind(feed.feed_id.to_string())
.bind(&feed.name)
.bind(feed.url.as_str())
.bind(feed.user_id.to_string())
.bind(feed.added_time.to_rfc3339())
.bind(feed.last_checked_time.to_rfc3339())
.bind("[]") // empty categorization array as JSON
.execute(pool)
.await
.expect("Failed to create demo feed");
println!("Successfully set up demo data");
}