DB integration ACTUALLY WORKS wow! 🎉

This commit is contained in:
Gabriel 2025-07-03 20:57:10 -04:00
parent 7d06ba9404
commit 2534cd2730

View file

@ -1,12 +1,10 @@
use std::path::PathBuf; use std::path::PathBuf;
use std::str::FromStr; use chrono::FixedOffset;
use rusqlite::{ Connection, Result};
use crate::files;
use rusqlite::{params, Connection, Result};
use super::files::*; use super::files::*;
use super::net::*; use super::net::*;
use chrono::Utc; //Maybe use a different time? use chrono::Utc; //Maybe use a different time?
use chrono::DateTime;
/* /*
Cache is the in-memory database Cache is the in-memory database
@ -24,7 +22,7 @@ fn get_db_path() -> PathBuf {
get_data_directory().join(DB_LOCATION) get_data_directory().join(DB_LOCATION)
} }
const feeds_table_create: &str = "CREATE TABLE IF NOT EXISTS 'feeds' ( const FEEDS_TABLE_CREATE: &str = "CREATE TABLE IF NOT EXISTS 'feeds' (
'feedID' INTEGER NOT NULL, 'feedID' INTEGER NOT NULL,
'title' TEXT NOT NULL, 'title' TEXT NOT NULL,
'description' TEXT, 'description' TEXT,
@ -35,12 +33,12 @@ const feeds_table_create: &str = "CREATE TABLE IF NOT EXISTS 'feeds' (
PRIMARY KEY('feedID') PRIMARY KEY('feedID')
);"; );";
const feeds_index_create: &str = "CREATE INDEX IF NOT EXISTS 'subscribed_feeds_idx' ON 'feeds' ( const FEEDS_INDEX_CREATE: &str = "CREATE INDEX IF NOT EXISTS 'subscribed_feeds_idx' ON 'feeds' (
'feedID' ASC 'feedID' ASC
) WHERE 'subscribed' = 1;"; ) WHERE 'subscribed' = 1;";
/* */ /* */
const items_table_create: &str = "CREATE TABLE IF NOT EXISTS 'items' ( const ITEMS_TABLE_CREATE: &str = "CREATE TABLE IF NOT EXISTS 'items' (
'itemID' INTEGER NOT NULL, 'itemID' INTEGER NOT NULL,
'title' TEXT NOT NULL, 'title' TEXT NOT NULL,
'icon' BLOB, 'icon' BLOB,
@ -50,17 +48,17 @@ const items_table_create: &str = "CREATE TABLE IF NOT EXISTS 'items' (
'read' INTEGER DEFAULT 0, 'read' INTEGER DEFAULT 0,
PRIMARY KEY('itemID') PRIMARY KEY('itemID')
);"; );";
const items_index_create: &str = "CREATE INDEX IF NOT EXISTS 'items_idx' on 'items'('itemID' ASC);"; const ITEMS_INDEX_CREATE: &str = "CREATE INDEX IF NOT EXISTS 'items_idx' on 'items'('itemID' ASC);";
pub fn initialize() { pub fn initialize() {
let path = get_db_path(); let path = get_db_path();
println!("Database at {} initialized", path.as_os_str().display()); println!("Database at {} initialized", path.as_os_str().display());
let conn = Connection::open(path).unwrap(); let conn = Connection::open(path).unwrap();
conn.execute(feeds_table_create, []).unwrap(); conn.execute(FEEDS_TABLE_CREATE, []).unwrap();
conn.execute(feeds_index_create, []).unwrap(); conn.execute(FEEDS_INDEX_CREATE, []).unwrap();
conn.execute(items_table_create, []).unwrap(); conn.execute(ITEMS_TABLE_CREATE, []).unwrap();
conn.execute(items_index_create, []).unwrap(); conn.execute(ITEMS_INDEX_CREATE, []).unwrap();
conn.close(); conn.close().unwrap();
println!("Database Initialized.") println!("Database Initialized.")
} }
@ -74,7 +72,7 @@ pub fn add_feed(url: &str) {
[feed.title, url.to_owned(), feed.description, time], [feed.title, url.to_owned(), feed.description, time],
) )
.unwrap(); .unwrap();
conn.close(); conn.close().unwrap();
store_items(new_feed); store_items(new_feed);
} }
@ -92,7 +90,7 @@ pub fn store_items(feed: rss::Channel) {
) )
.unwrap(); .unwrap();
}); });
conn.close(); conn.close().unwrap();
} }
pub fn return_item() -> String { pub fn return_item() -> String {
@ -116,13 +114,20 @@ pub fn return_item() -> String {
} }
pub struct Feed { pub struct Feed {
pub feedID: u8, pub feed_id: u8,
pub title: String, pub title: String,
pub description: Option<String>, pub description: Option<String>,
pub icon: Option<String>, pub icon: Option<String>,
pub url: String, pub url: String,
pub subscribed: bool, pub subscribed: bool,
pub last_updated: Option<String>, pub last_updated: Option<DateTime<Utc>>,
}
fn time_string_conversion(str: String) -> Option<DateTime<Utc>>{
match DateTime::parse_from_rfc2822(&str) {
Ok(dt) => {Some(dt.to_utc())},
Err(_) => {None}
}
} }
pub fn get_feeds() -> Vec<Feed> { pub fn get_feeds() -> Vec<Feed> {
@ -131,13 +136,13 @@ pub fn get_feeds() -> Vec<Feed> {
let rows: Result<Vec<Feed>> = stmt let rows: Result<Vec<Feed>> = stmt
.query_map([], |row| { .query_map([], |row| {
Ok(Feed { Ok(Feed {
feedID: row.get(0).unwrap(), feed_id: row.get(0).unwrap(),
title: row.get(1).unwrap(), title: row.get(1).unwrap(),
description: row.get(2).unwrap(), description: row.get(2).unwrap(),
icon: row.get(3).unwrap(), icon: row.get(3).unwrap(),
url: row.get(4).unwrap(), url: row.get(4).unwrap(),
subscribed: row.get::<_,bool>(5).unwrap(), subscribed: row.get::<_,bool>(5).unwrap(),
last_updated: row.get(6).unwrap(), last_updated:time_string_conversion(row.get(6).unwrap()),
}) })
}).unwrap().collect(); }).unwrap().collect();
match rows { match rows {
@ -167,8 +172,8 @@ pub fn update_feeds() {
let url = feed.unwrap().url.clone(); let url = feed.unwrap().url.clone();
urls.push(url); urls.push(url);
} }
stmt.finalize(); stmt.finalize().unwrap();
conn.close(); conn.close().unwrap();
for u in urls { for u in urls {
store_items(load_rss(&u).unwrap()); store_items(load_rss(&u).unwrap());