use sqlx::{FromRow, Row, types::chrono}; #[derive(Debug, FromRow)] pub struct PostRecord { pub created: chrono::DateTime, pub indexed: chrono::DateTime, pub author: String, pub rkey: String, pub cid: String, // TODO: other fields like reply to etc } #[derive(Debug, FromRow)] pub struct RepostRecord { pub created: chrono::DateTime, pub indexed: chrono::DateTime, pub author: String, pub rkey: String, pub subject: String, pub cid: String, // TODO: other fields like reply to etc } pub async fn insert_post(post: PostRecord, pool: &sqlx::SqlitePool) { let result = sqlx::query( "INSERT INTO posts (created, indexed, author, rkey, cid) VALUES ($1, $2, $3, $4, $5)", ) .bind(post.created) .bind(post.indexed) .bind(post.author) .bind(post.rkey) .bind(post.cid) .execute(pool) .await; if result.is_err() { println!("Error inserting post into the database: {result:?}"); } } pub async fn insert_repost(repost: RepostRecord, pool: &sqlx::SqlitePool) { let result = sqlx::query( "INSERT INTO reposts (created, indexed, author, rkey, subject, cid) VALUES ($1, $2, $3, $4, $5, $6)", ) .bind(repost.created) .bind(repost.indexed) .bind(repost.author) .bind(repost.rkey) .bind(repost.subject) .bind(repost.cid) .execute(pool) .await; if result.is_err() { println!("Error inserting repost into the database: {result:?}"); } } pub async fn delete_post(rkey: String, pool: &sqlx::SqlitePool) { let result = sqlx::query("DELETE FROM posts WHERE rkey = $1") .bind(rkey) .execute(pool) .await; if result.is_err() { println!("Error deleting post from the database: {result:?}"); } } pub async fn delete_repost(rkey: String, pool: &sqlx::SqlitePool) { let result = sqlx::query("DELETE FROM reposts WHERE rkey = $1") .bind(rkey) .execute(pool) .await; if result.is_err() { println!("Error deleting repost from the database: {result:?}"); } } pub async fn get_follow_by_rkey(rkey: &String, pool: &sqlx::SqlitePool) -> String { let result = sqlx::query("SELECT subject FROM follows where rkey = ? LIMIT 1") .bind(rkey) .fetch_one(pool) .await; match result { Ok(row) => { if row.len() == 0 { println!("did not find subject in follows with rkey: {rkey}"); return "".to_string(); } let subject = row.get::(0); return subject; } Err(e) => { println!("error getting follow {e}"); return "".to_string(); } } }