2026-01-20 16:08:36 -08:00
|
|
|
use crate::{
|
|
|
|
|
Result,
|
|
|
|
|
Item,
|
|
|
|
|
Channel,
|
2026-01-20 16:36:14 -08:00
|
|
|
channel::{
|
|
|
|
|
UnparsedChannel,
|
|
|
|
|
ChannelId,
|
|
|
|
|
},
|
|
|
|
|
user::UserId,
|
2026-01-20 16:08:36 -08:00
|
|
|
};
|
|
|
|
|
use sqlx::SqlitePool;
|
|
|
|
|
|
2026-01-21 12:47:47 -08:00
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
|
pub struct FeedId(i64);
|
2026-01-20 16:08:36 -08:00
|
|
|
impl From<FeedId> for i64 { fn from(id: FeedId) -> Self { id.0 } }
|
|
|
|
|
|
2026-01-20 16:36:14 -08:00
|
|
|
pub struct UnparsedFeed {
|
|
|
|
|
pub id: i64,
|
|
|
|
|
pub title: String,
|
|
|
|
|
}
|
|
|
|
|
impl UnparsedFeed {
|
|
|
|
|
pub fn parse(self) -> Result<Feed> {
|
|
|
|
|
Ok(Feed {
|
|
|
|
|
id: FeedId(self.id),
|
|
|
|
|
title: self.title,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-20 16:08:36 -08:00
|
|
|
pub struct Feed {
|
2026-01-21 12:47:47 -08:00
|
|
|
id: FeedId,
|
|
|
|
|
title: String,
|
2026-01-20 16:08:36 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Feed {
|
2026-01-21 12:47:47 -08:00
|
|
|
pub fn id(&self) -> FeedId { self.id }
|
|
|
|
|
pub fn title(&self) -> &str { &self.title }
|
|
|
|
|
|
2026-01-21 13:04:51 -08:00
|
|
|
pub async fn get(
|
|
|
|
|
pool: &SqlitePool, id: FeedId
|
|
|
|
|
) -> Result<Self> {
|
|
|
|
|
let feed = sqlx::query_as!(
|
|
|
|
|
UnparsedFeed,
|
|
|
|
|
"SELECT id, title FROM feeds WHERE id = ?",
|
|
|
|
|
id.0
|
|
|
|
|
).fetch_one(pool).await?.parse();
|
|
|
|
|
|
|
|
|
|
feed
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-20 16:36:14 -08:00
|
|
|
pub async fn create(
|
2026-01-21 13:04:51 -08:00
|
|
|
pool: &SqlitePool, user_id: UserId, title: &str
|
2026-01-20 16:36:14 -08:00
|
|
|
) -> Result<Self> {
|
2026-01-21 13:04:51 -08:00
|
|
|
let int_id = i64::from(user_id);
|
2026-01-20 16:36:14 -08:00
|
|
|
let new_feed = sqlx::query_as!(
|
|
|
|
|
UnparsedFeed,
|
|
|
|
|
"INSERT INTO feeds (user_id, title)
|
|
|
|
|
VALUES (?, ?)
|
2026-01-21 12:47:47 -08:00
|
|
|
RETURNING id as `id!`, title",
|
2026-01-21 13:04:51 -08:00
|
|
|
int_id, title
|
2026-01-20 16:36:14 -08:00
|
|
|
).fetch_one(pool).await?.parse();
|
|
|
|
|
|
|
|
|
|
new_feed
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-21 13:04:51 -08:00
|
|
|
pub async fn update_title(
|
|
|
|
|
pool: &SqlitePool, id: FeedId, new_title: &str
|
|
|
|
|
) -> Result<()> {
|
|
|
|
|
sqlx::query!(
|
|
|
|
|
"UPDATE feeds SET title = ? WHERE id = ?",
|
|
|
|
|
new_title, id.0
|
|
|
|
|
).execute(pool).await?;
|
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-20 16:36:14 -08:00
|
|
|
pub async fn add_channel(
|
2026-01-21 13:04:51 -08:00
|
|
|
&self, pool: &SqlitePool, channel_id: ChannelId
|
2026-01-20 16:36:14 -08:00
|
|
|
) -> Result<()> {
|
|
|
|
|
sqlx::query!(
|
|
|
|
|
"INSERT INTO feed_channels (feed_id, channel_id)
|
|
|
|
|
VALUES (?, ?)",
|
2026-01-21 13:04:51 -08:00
|
|
|
self.id.0, channel_id.0
|
2026-01-20 16:36:14 -08:00
|
|
|
).execute(pool).await?;
|
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-20 16:08:36 -08:00
|
|
|
pub async fn get_items(
|
2026-01-21 13:04:51 -08:00
|
|
|
&self, pool: &SqlitePool, limit: u8, offset: u32
|
2026-01-20 16:08:36 -08:00
|
|
|
) -> Result<Vec<Item>> {
|
|
|
|
|
let items = sqlx::query_as!(
|
|
|
|
|
Item,
|
2026-01-20 16:36:14 -08:00
|
|
|
"SELECT item_id as id FROM feed_items
|
2026-01-20 16:08:36 -08:00
|
|
|
WHERE feed_id = ? AND archived = FALSE
|
|
|
|
|
ORDER BY score DESC
|
|
|
|
|
LIMIT ? OFFSET ?",
|
2026-01-21 13:04:51 -08:00
|
|
|
self.id.0, limit, offset
|
2026-01-20 16:08:36 -08:00
|
|
|
).fetch_all(pool).await?;
|
|
|
|
|
|
|
|
|
|
Ok(items)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub async fn get_channels(
|
2026-01-21 13:04:51 -08:00
|
|
|
&self, pool: &SqlitePool
|
2026-01-20 16:08:36 -08:00
|
|
|
) -> Result<Vec<Channel>> {
|
|
|
|
|
let channels: Result<Vec<Channel>> = sqlx::query_as!(
|
|
|
|
|
UnparsedChannel,
|
|
|
|
|
"SELECT c.id as `id!`, c.title, c.link, c.description, c.last_fetched
|
|
|
|
|
FROM channels c
|
|
|
|
|
JOIN feed_channels fc on c.id = fc.channel_id
|
|
|
|
|
WHERE fc.feed_id = ?",
|
2026-01-21 13:04:51 -08:00
|
|
|
self.id.0
|
2026-01-20 16:08:36 -08:00
|
|
|
).fetch_all(pool).await?.into_iter()
|
|
|
|
|
.map(UnparsedChannel::parse).collect();
|
|
|
|
|
|
|
|
|
|
channels
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-21 13:04:51 -08:00
|
|
|
#[cfg(test)]
|
|
|
|
|
mod tests {
|
|
|
|
|
use super::*;
|
|
|
|
|
use crate::User;
|
|
|
|
|
use sqlx::SqlitePool;
|
|
|
|
|
|
|
|
|
|
async fn setup_test_db() -> SqlitePool {
|
|
|
|
|
let pool = SqlitePool::connect("sqlite::memory:").await.unwrap();
|
|
|
|
|
sqlx::migrate!().run(&pool).await.unwrap();
|
|
|
|
|
pool
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
|
async fn create_feed() {
|
|
|
|
|
let pool = setup_test_db().await;
|
|
|
|
|
let user = User::create(&pool, "Alice").await.unwrap();
|
|
|
|
|
|
|
|
|
|
let feed = Feed::create(&pool, user.id(), "Tech News").await.unwrap();
|
|
|
|
|
|
|
|
|
|
assert_eq!(feed.title(), "Tech News");
|
|
|
|
|
assert!(feed.id().0 > 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
|
async fn test_update_title() {
|
|
|
|
|
let pool = setup_test_db().await;
|
|
|
|
|
let user = User::create(&pool, "Alice").await.unwrap();
|
|
|
|
|
let feed = Feed::create(&pool, user.id(), "Tech News").await.unwrap();
|
|
|
|
|
|
|
|
|
|
Feed::update_title(&pool, feed.id(), "Technology").await.unwrap();
|
|
|
|
|
|
|
|
|
|
let updated = Feed::get(&pool, feed.id()).await.unwrap();
|
|
|
|
|
assert_eq!(updated.title(), "Technology");
|
|
|
|
|
}
|
|
|
|
|
}
|