From 2b632f0a9360a0497a162ba1ebfbac5b1a739963 Mon Sep 17 00:00:00 2001 From: Greg Shuflin Date: Mon, 3 Feb 2025 15:46:28 -0800 Subject: [PATCH] Add demo feed --- src/demo.rs | 17 +++++++++++++++++ src/feeds.rs | 23 +++++++++++------------ 2 files changed, 28 insertions(+), 12 deletions(-) diff --git a/src/demo.rs b/src/demo.rs index c3ec75e..0e90179 100644 --- a/src/demo.rs +++ b/src/demo.rs @@ -54,5 +54,22 @@ pub async fn setup_demo_data(pool: &sqlx::SqlitePool) { 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"); } diff --git a/src/feeds.rs b/src/feeds.rs index 76dd467..0ea8110 100644 --- a/src/feeds.rs +++ b/src/feeds.rs @@ -12,13 +12,13 @@ use crate::Db; #[derive(Debug, Serialize)] #[serde(crate = "rocket::serde")] pub struct Feed { - feed_id: Uuid, - name: String, - url: Url, - user_id: Uuid, - added_time: chrono::DateTime, - last_checked_time: chrono::DateTime, - categorization: Vec, + pub feed_id: Uuid, + pub name: String, + pub url: Url, + pub user_id: Uuid, + pub added_time: chrono::DateTime, + pub last_checked_time: chrono::DateTime, + pub categorization: Vec, } impl Feed { @@ -37,11 +37,10 @@ impl Feed { 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)) - })?; + 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)