From 4cce902a2147efb595a088b5f426ca1446a57d74 Mon Sep 17 00:00:00 2001 From: Greg Shuflin Date: Mon, 3 Feb 2025 04:29:16 -0800 Subject: [PATCH] Moving code around (broken) --- src/demo.rs | 20 ++++++++++++++++---- src/feeds.rs | 49 +++++++++++++++++++++++++++---------------------- src/main.rs | 3 +-- 3 files changed, 44 insertions(+), 28 deletions(-) diff --git a/src/demo.rs b/src/demo.rs index 8df271d..c3ec75e 100644 --- a/src/demo.rs +++ b/src/demo.rs @@ -2,9 +2,12 @@ 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().to_string(); + 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(); @@ -12,7 +15,7 @@ pub async fn setup_demo_data(pool: &sqlx::SqlitePool) { "INSERT INTO users (id, username, password_hash, email, display_name, created_at, admin) VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7)", ) - .bind(&admin_id) + .bind(&admin_id_str) .bind("admin") .bind(&admin_hash) .bind(Option::::None) @@ -24,14 +27,15 @@ pub async fn setup_demo_data(pool: &sqlx::SqlitePool) { .expect("Failed to create admin user"); // Create demo user - let demo_id = Uuid::new_v4().to_string(); + 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) + .bind(&demo_id_str) .bind("demo") .bind(&demo_hash) .bind(Option::::None) @@ -42,5 +46,13 @@ pub async fn setup_demo_data(pool: &sqlx::SqlitePool) { .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, + ); + println!("Successfully set up demo data"); } diff --git a/src/feeds.rs b/src/feeds.rs index 9fe1bbb..76dd467 100644 --- a/src/feeds.rs +++ b/src/feeds.rs @@ -34,6 +34,31 @@ impl Feed { categorization: Vec::new(), } } + + pub async fn write_to_database(&self, mut db: Connection) -> Result<(), sqlx::Error> { + // Convert categorization to JSON value + let categorization_json = serde::json::to_value(&self.categorization) + .map_err(|e| { + eprintln!("Failed to serialize categorization: {}", e); + sqlx::Error::Decode(Box::new(e)) + })?; + + 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(self.feed_id.to_string()) + .bind(&self.name) + .bind(self.url.as_str()) + .bind(self.user_id.to_string()) + .bind(self.added_time.to_rfc3339()) + .bind(self.last_checked_time.to_rfc3339()) + .bind(&categorization_json.to_string()) + .execute(&mut **db) + .await?; + + Ok(()) + } } #[derive(Debug, Deserialize)] @@ -46,7 +71,7 @@ pub struct NewFeed { #[post("/feeds", data = "")] pub async fn create_feed( - mut db: Connection, + db: Connection, new_feed: Json, user: AuthenticatedUser, ) -> Result, Status> { @@ -73,27 +98,7 @@ pub async fn create_feed( let mut feed = Feed::new(name, new_feed.url, user.user_id); feed.categorization = new_feed.categorization; - // Convert categorization to JSON value - let categorization_json = serde::json::to_value(&feed.categorization).map_err(|e| { - eprintln!("Failed to serialize categorization: {}", e); - Status::InternalServerError - })?; - - let query = 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(&categorization_json.to_string()) - .execute(&mut **db) - .await; - - match query { + match feed.write_to_database(db).await { Ok(_) => Ok(Json(feed)), Err(e) => { eprintln!("Database error: {}", e); diff --git a/src/main.rs b/src/main.rs index 79892dd..2e26668 100644 --- a/src/main.rs +++ b/src/main.rs @@ -100,7 +100,6 @@ fn rocket() -> _ { }; let figment = rocket::Config::figment().merge(("databases.rss_data.url", db_url)); - let demo = args.demo; rocket::custom(figment) .mount( @@ -127,6 +126,6 @@ fn rocket() -> _ { .attach(Template::fairing()) .attach(Db::init()) .attach(AdHoc::try_on_ignite("DB Setup", move |rocket| async move { - setup_database(demo, rocket).await + setup_database(args.demo, rocket).await })) }