Add demo feed

This commit is contained in:
Greg Shuflin 2025-02-03 15:46:28 -08:00
parent 4cce902a21
commit 2b632f0a93
2 changed files with 28 additions and 12 deletions

View File

@ -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");
}

View File

@ -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<chrono::Utc>,
last_checked_time: chrono::DateTime<chrono::Utc>,
categorization: Vec<String>,
pub feed_id: Uuid,
pub name: String,
pub url: Url,
pub user_id: Uuid,
pub added_time: chrono::DateTime<chrono::Utc>,
pub last_checked_time: chrono::DateTime<chrono::Utc>,
pub categorization: Vec<String>,
}
impl Feed {
@ -37,8 +37,7 @@ impl Feed {
pub async fn write_to_database(&self, mut db: Connection<Db>) -> Result<(), sqlx::Error> {
// Convert categorization to JSON value
let categorization_json = serde::json::to_value(&self.categorization)
.map_err(|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))
})?;