From 162ba7843047b1545cbe8cc235267dbbd65c0265 Mon Sep 17 00:00:00 2001 From: Julia Lange Date: Tue, 20 Jan 2026 16:36:14 -0800 Subject: [PATCH] Adding fill functions --- .../20260115003047_initial_schema.sql | 1 + koucha/src/channel.rs | 4 +- koucha/src/feed.rs | 51 ++++++++++++++++++- 3 files changed, 53 insertions(+), 3 deletions(-) diff --git a/koucha/migrations/20260115003047_initial_schema.sql b/koucha/migrations/20260115003047_initial_schema.sql index d437df3..25122e7 100644 --- a/koucha/migrations/20260115003047_initial_schema.sql +++ b/koucha/migrations/20260115003047_initial_schema.sql @@ -32,6 +32,7 @@ CREATE TABLE items ( CREATE TABLE feeds ( id INTEGER PRIMARY KEY, user_id INTEGER NOT NULL, + title TEXT NOT NULL, FOREIGN KEY (user_id) REFERENCES users(id) ); diff --git a/koucha/src/channel.rs b/koucha/src/channel.rs index 73eada0..6d130ff 100644 --- a/koucha/src/channel.rs +++ b/koucha/src/channel.rs @@ -40,7 +40,7 @@ impl FetchedRSSItem { } pub struct FetchedRSSChannel { title: String, - link: Url, + link: Url, description: String, items: Vec, @@ -96,6 +96,8 @@ pub struct Channel { } impl Channel { + + pub async fn get_all(pool: &SqlitePool) -> Result> { let channels: Result> = sqlx::query_as!( UnparsedChannel, diff --git a/koucha/src/feed.rs b/koucha/src/feed.rs index aaafa90..8e04a0d 100644 --- a/koucha/src/feed.rs +++ b/koucha/src/feed.rs @@ -2,7 +2,11 @@ use crate::{ Result, Item, Channel, - channel::UnparsedChannel, + channel::{ + UnparsedChannel, + ChannelId, + }, + user::UserId, }; use sqlx::SqlitePool; @@ -10,17 +14,60 @@ pub struct FeedId(pub i64); impl From for FeedId { fn from(id: i64) -> Self { FeedId(id) } } impl From for i64 { fn from(id: FeedId) -> Self { id.0 } } +pub struct UnparsedFeed { + pub id: i64, + pub title: String, + pub user_id: i64 +} +impl UnparsedFeed { + pub fn parse(self) -> Result { + Ok(Feed { + id: FeedId(self.id), + title: self.title, + user_id: UserId(self.user_id), + }) + } +} + pub struct Feed { pub id: FeedId, + pub title: String, + pub user_id: UserId, } impl Feed { + pub async fn create( + pool: &SqlitePool, id: UserId, name: &str + ) -> Result { + let new_feed = sqlx::query_as!( + UnparsedFeed, + "INSERT INTO feeds (user_id, title) + VALUES (?, ?) + RETURNING id as `id!`, user_id, title", + id.0, name + ).fetch_one(pool).await?.parse(); + + new_feed + } + + pub async fn add_channel( + pool: &SqlitePool, id: FeedId, channel_id: ChannelId + ) -> Result<()> { + sqlx::query!( + "INSERT INTO feed_channels (feed_id, channel_id) + VALUES (?, ?)", + id.0, channel_id.0 + ).execute(pool).await?; + + Ok(()) + } + pub async fn get_items( pool: &SqlitePool, id: FeedId, limit: u8, offset: u32 ) -> Result> { let items = sqlx::query_as!( Item, - "SELECT item_id as id FROM feed_items + "SELECT item_id as id FROM feed_items WHERE feed_id = ? AND archived = FALSE ORDER BY score DESC LIMIT ? OFFSET ?",