Compare commits

...

2 Commits

Author SHA1 Message Date
Greg Shuflin
7ecf598aa8 Start to implemenet pulling the feed in demo.rs 2025-02-05 16:38:59 -08:00
Greg Shuflin
286f4273bf Use Uuid consistently in db methods 2025-02-05 16:36:18 -08:00
3 changed files with 17 additions and 14 deletions

View File

@ -1,6 +1,7 @@
use crate::feeds::Feed;
use crate::poll_utils::fetch_new_entries;
use crate::user::User;
use tracing::info;
use tracing::{info, warn};
struct DemoFeed {
name: &'static str,
@ -76,22 +77,24 @@ pub async fn setup_demo_data(pool: &sqlx::SqlitePool) {
})
.collect();
for feed in feeds {
for feed in feeds.iter() {
feed.write_to_database(pool)
.await
.expect("Failed to create demo feed");
}
/*
for feed in feeds {
let url = feed.url;
let entries = match fetch_new_entries(&url).await {
for feed in feeds.iter() {
let url = &feed.url;
let entries = match fetch_new_entries(url).await {
Ok(entries) => entries,
Err(e) => {
warn!(error=%e, feed_url=url.as_str(), "Error populating feed");
continue;
}
};
update_entry_db(&entries, &feed_id, &mut db).await?;
//update_entry_db(&entries, &feed_id, &mut db).await?;
}
*/
info!("Successfully set up demo data");
}

View File

@ -26,7 +26,7 @@ pub struct EntryStateUpdate {
read: Option<bool>,
}
async fn read_entries(feed_id: &str, db: &mut SqliteConnection) -> Result<Vec<Entry>, Status> {
async fn read_entries(feed_id: &Uuid, db: &mut SqliteConnection) -> Result<Vec<Entry>, Status> {
let rows = sqlx::query!(
r#"
SELECT
@ -86,13 +86,13 @@ pub async fn poll_feed(
feed_id: Uuid,
user: AuthenticatedUser,
) -> Result<Json<FeedPollResponse>, Status> {
let feed_id = feed_id.to_string();
let user_id = user.user_id.to_string();
let feed_id_str = feed_id.to_string();
// Get the feed URL from the database, ensuring it belongs to the authenticated user
let feed = sqlx::query!(
r#"SELECT url, last_checked_time as "last_checked_time: chrono::DateTime<chrono::Utc>" FROM feeds WHERE feed_id = ? AND user_id = ?"#,
feed_id,
feed_id_str,
user_id
)
.fetch_optional(&mut **db)

View File

@ -23,7 +23,6 @@ pub struct Entry {
pub marked_read: Option<DateTime<Utc>>,
}
///
/// Perform the request to fetch from the remote feed url
pub async fn fetch_new_entries(url: &Url) -> Result<Vec<Entry>, Status> {
let feed_data = fetch_feed(url).await.map_err(|_| Status::BadGateway)?;
@ -53,7 +52,7 @@ pub async fn fetch_new_entries(url: &Url) -> Result<Vec<Entry>, Status> {
pub async fn update_entry_db(
entries: &Vec<Entry>,
feed_id: &str,
feed_id: &Uuid,
db: &mut SqliteConnection,
) -> Result<(), Status> {
// Start a transaction for batch update
@ -65,6 +64,7 @@ pub async fn update_entry_db(
let now = Utc::now().to_rfc3339();
// Update the feed's last_checked_time
let feed_id = feed_id.to_string();
sqlx::query!(
"UPDATE feeds SET last_checked_time = ? WHERE feed_id = ?",
now,