From 8183feb2291aff8a4dd70969493daed5f7dfc628 Mon Sep 17 00:00:00 2001 From: Gabriel Date: Fri, 25 Jul 2025 18:40:50 -0400 Subject: [PATCH] handle duplicate feeds --- src/db.rs | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/src/db.rs b/src/db.rs index 4756da4..47a21a9 100644 --- a/src/db.rs +++ b/src/db.rs @@ -25,7 +25,7 @@ const FEEDS_TABLE_CREATE: &str = "CREATE TABLE IF NOT EXISTS 'feeds' ( 'title' TEXT NOT NULL, 'description' TEXT, 'icon' BLOB, - 'url' text, + 'url' text not null unique, 'subscribed' INTEGER NOT NULL default 0, 'last_updated' TEXT , PRIMARY KEY('feedID') @@ -86,22 +86,31 @@ pub fn initialize() { } -pub fn add_feed(url: &str) { +pub fn add_feed(url: &str) -> Option { let conn = Connection::open(get_db_path()).unwrap(); let feed = load_rss(url).unwrap(); let new_feed = feed.clone(); let time = Utc::now().to_rfc2822(); - conn.execute( + match conn.execute( "insert into feeds(title,url,description,last_updated) values(?1,?2,?3,?4)", [feed.title, url.to_string(), feed.description, time], - ) - .unwrap(); + ) { + Ok(_) => { + + } + Err(e) => { + println!("Couldn't add feed:{}\nError:{}",url,e); + return None; + } + } + let mut stmt = conn.prepare("select feedID from feeds where url=?1").unwrap(); let id: usize = stmt.query_row([url],|row| { row.get(0) }).unwrap(); //need to get the feed_id from the DB and then make sure items are mapped to feed store_items(new_feed,id); + Some(id) } pub fn store_items(feed: rss::Channel,feed_id: usize) {