use axum::{Json, extract::State, http::StatusCode, response::IntoResponse}; use rss::Item as RssItem; use serde::Serialize; use std::error::Error; use turso::{Connection, Rows}; #[derive(Debug, Serialize)] pub struct Item { pub id: u64, pub channel_id: Option, pub title: Option, pub link: Option, pub content: Option, pub author: Option, pub pub_date: Option, } #[derive(Debug, Serialize)] pub struct ItemsList { pub data: Vec, } pub async fn add_item( conn: &Connection, channel_id: &u64, item: &RssItem, ) -> Result<(), Box> { conn.execute( "INSERT OR IGNORE INTO items (channel_id, title, link, content, author, pub_date) VALUES (?, ?, ?, ?, ?, ?)", ( channel_id.to_owned(), item.title().unwrap_or(""), item.link().unwrap_or(""), item.content().unwrap_or(""), item.author().unwrap_or(""), item.pub_date().unwrap_or(""), ), ) .await?; Ok(()) } pub async fn get_items( conn: &Connection, channel_id: Option<&u64>, ) -> Result, Box> { let mut rows: Rows = if let Some(channel_id) = channel_id { conn.query( "SELECT * FROM items WHERE channel_id = ?1", turso::params![channel_id], ) .await? } else { conn.query("SELECT * FROM items", ()).await? }; let mut items: Vec = vec![]; while let Some(row) = rows.next().await.unwrap() { items.push(Item { id: row.get(0).unwrap(), channel_id: row.get(1).unwrap_or(None), title: row.get(2).unwrap_or(None), link: row.get(3).unwrap_or(None), content: row.get(4).unwrap_or(None), author: row.get(5).unwrap_or(None), pub_date: row.get(6).unwrap_or(None), }); } Ok(items) } pub async fn list_items(State(conn): State) -> impl IntoResponse { let items = get_items(&conn, None).await.unwrap(); println!("{items:?}"); Json(ItemsList { data: items }) }