Moving code around (broken)

This commit is contained in:
Greg Shuflin 2025-02-03 04:29:16 -08:00
parent 85b5167805
commit 4cce902a21
3 changed files with 44 additions and 28 deletions

View File

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

View File

@ -34,6 +34,31 @@ impl Feed {
categorization: Vec::new(),
}
}
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| {
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 = "<new_feed>")]
pub async fn create_feed(
mut db: Connection<Db>,
db: Connection<Db>,
new_feed: Json<NewFeed>,
user: AuthenticatedUser,
) -> Result<Json<Feed>, 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);

View File

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