Moving code around (broken)
This commit is contained in:
parent
85b5167805
commit
4cce902a21
20
src/demo.rs
20
src/demo.rs
@ -2,9 +2,12 @@ use chrono;
|
|||||||
use sqlx;
|
use sqlx;
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
use crate::feeds::Feed;
|
||||||
|
|
||||||
pub async fn setup_demo_data(pool: &sqlx::SqlitePool) {
|
pub async fn setup_demo_data(pool: &sqlx::SqlitePool) {
|
||||||
// Create admin user
|
// 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 admin_hash = bcrypt::hash("admin", bcrypt::DEFAULT_COST).unwrap();
|
||||||
let now = chrono::Utc::now().to_rfc3339();
|
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)
|
"INSERT INTO users (id, username, password_hash, email, display_name, created_at, admin)
|
||||||
VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7)",
|
VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7)",
|
||||||
)
|
)
|
||||||
.bind(&admin_id)
|
.bind(&admin_id_str)
|
||||||
.bind("admin")
|
.bind("admin")
|
||||||
.bind(&admin_hash)
|
.bind(&admin_hash)
|
||||||
.bind(Option::<String>::None)
|
.bind(Option::<String>::None)
|
||||||
@ -24,14 +27,15 @@ pub async fn setup_demo_data(pool: &sqlx::SqlitePool) {
|
|||||||
.expect("Failed to create admin user");
|
.expect("Failed to create admin user");
|
||||||
|
|
||||||
// Create demo 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();
|
let demo_hash = bcrypt::hash("demo", bcrypt::DEFAULT_COST).unwrap();
|
||||||
|
|
||||||
sqlx::query(
|
sqlx::query(
|
||||||
"INSERT INTO users (id, username, password_hash, email, display_name, created_at, admin)
|
"INSERT INTO users (id, username, password_hash, email, display_name, created_at, admin)
|
||||||
VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7)",
|
VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7)",
|
||||||
)
|
)
|
||||||
.bind(&demo_id)
|
.bind(&demo_id_str)
|
||||||
.bind("demo")
|
.bind("demo")
|
||||||
.bind(&demo_hash)
|
.bind(&demo_hash)
|
||||||
.bind(Option::<String>::None)
|
.bind(Option::<String>::None)
|
||||||
@ -42,5 +46,13 @@ pub async fn setup_demo_data(pool: &sqlx::SqlitePool) {
|
|||||||
.await
|
.await
|
||||||
.expect("Failed to create demo user");
|
.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");
|
println!("Successfully set up demo data");
|
||||||
}
|
}
|
||||||
|
49
src/feeds.rs
49
src/feeds.rs
@ -34,6 +34,31 @@ impl Feed {
|
|||||||
categorization: Vec::new(),
|
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)]
|
#[derive(Debug, Deserialize)]
|
||||||
@ -46,7 +71,7 @@ pub struct NewFeed {
|
|||||||
|
|
||||||
#[post("/feeds", data = "<new_feed>")]
|
#[post("/feeds", data = "<new_feed>")]
|
||||||
pub async fn create_feed(
|
pub async fn create_feed(
|
||||||
mut db: Connection<Db>,
|
db: Connection<Db>,
|
||||||
new_feed: Json<NewFeed>,
|
new_feed: Json<NewFeed>,
|
||||||
user: AuthenticatedUser,
|
user: AuthenticatedUser,
|
||||||
) -> Result<Json<Feed>, Status> {
|
) -> 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);
|
let mut feed = Feed::new(name, new_feed.url, user.user_id);
|
||||||
feed.categorization = new_feed.categorization;
|
feed.categorization = new_feed.categorization;
|
||||||
|
|
||||||
// Convert categorization to JSON value
|
match feed.write_to_database(db).await {
|
||||||
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 {
|
|
||||||
Ok(_) => Ok(Json(feed)),
|
Ok(_) => Ok(Json(feed)),
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
eprintln!("Database error: {}", e);
|
eprintln!("Database error: {}", e);
|
||||||
|
@ -100,7 +100,6 @@ fn rocket() -> _ {
|
|||||||
};
|
};
|
||||||
|
|
||||||
let figment = rocket::Config::figment().merge(("databases.rss_data.url", db_url));
|
let figment = rocket::Config::figment().merge(("databases.rss_data.url", db_url));
|
||||||
let demo = args.demo;
|
|
||||||
|
|
||||||
rocket::custom(figment)
|
rocket::custom(figment)
|
||||||
.mount(
|
.mount(
|
||||||
@ -127,6 +126,6 @@ fn rocket() -> _ {
|
|||||||
.attach(Template::fairing())
|
.attach(Template::fairing())
|
||||||
.attach(Db::init())
|
.attach(Db::init())
|
||||||
.attach(AdHoc::try_on_ignite("DB Setup", move |rocket| async move {
|
.attach(AdHoc::try_on_ignite("DB Setup", move |rocket| async move {
|
||||||
setup_database(demo, rocket).await
|
setup_database(args.demo, rocket).await
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user