Clean up server.rs, add DB and Client aliases

This commit is contained in:
Julia Lange 2026-01-22 11:39:33 -08:00
parent 7bb4cf4230
commit f5fc83a471
Signed by: Julia
SSH key fingerprint: SHA256:5DJcfxa5/fKCYn57dcabJa2vN2e6eT0pBerYi5SUbto
5 changed files with 153 additions and 128 deletions

View file

@ -1,5 +1,6 @@
use crate::{
Result,
AdapterPool,
Item,
item::UnparsedItem,
Channel,
@ -9,7 +10,6 @@ use crate::{
},
user::UserId,
};
use sqlx::SqlitePool;
#[derive(Copy, Clone)]
pub struct FeedId(i64);
@ -38,19 +38,19 @@ impl Feed {
pub fn title(&self) -> &str { &self.title }
pub async fn get(
pool: &SqlitePool, id: FeedId
pool: &AdapterPool, id: FeedId
) -> Result<Self> {
let feed = sqlx::query_as!(
UnparsedFeed,
"SELECT id, title FROM feeds WHERE id = ?",
id.0
).fetch_one(pool).await?.parse();
).fetch_one(&pool.0).await?.parse();
feed
}
pub async fn create(
pool: &SqlitePool, user_id: UserId, title: &str
pool: &AdapterPool, user_id: UserId, title: &str
) -> Result<Self> {
let int_id = i64::from(user_id);
let new_feed = sqlx::query_as!(
@ -59,37 +59,37 @@ impl Feed {
VALUES (?, ?)
RETURNING id as `id!`, title",
int_id, title
).fetch_one(pool).await?.parse();
).fetch_one(&pool.0).await?.parse();
new_feed
}
pub async fn update_title(
pool: &SqlitePool, id: FeedId, new_title: &str
pool: &AdapterPool, id: FeedId, new_title: &str
) -> Result<()> {
sqlx::query!(
"UPDATE feeds SET title = ? WHERE id = ?",
new_title, id.0
).execute(pool).await?;
).execute(&pool.0).await?;
Ok(())
}
pub async fn add_channel(
&self, pool: &SqlitePool, channel_id: ChannelId
&self, pool: &AdapterPool, channel_id: ChannelId
) -> Result<()> {
let int_channel_id = i64::from(channel_id);
sqlx::query!(
"INSERT INTO feed_channels (feed_id, channel_id)
VALUES (?, ?)",
self.id.0, int_channel_id
).execute(pool).await?;
).execute(&pool.0).await?;
Ok(())
}
pub async fn get_items(
&self, pool: &SqlitePool, limit: u8, offset: u32
&self, pool: &AdapterPool, limit: u8, offset: u32
) -> Result<Vec<Item>> {
let items: Result<Vec<Item>> = sqlx::query_as!(
UnparsedItem,
@ -98,13 +98,13 @@ impl Feed {
ORDER BY score DESC
LIMIT ? OFFSET ?",
self.id.0, limit, offset
).fetch_all(pool).await?.into_iter().map(UnparsedItem::parse).collect();
).fetch_all(&pool.0).await?.into_iter().map(UnparsedItem::parse).collect();
items
}
pub async fn get_channels(
&self, pool: &SqlitePool
&self, pool: &AdapterPool
) -> Result<Vec<Channel>> {
let channels: Result<Vec<Channel>> = sqlx::query_as!(
UnparsedChannel,
@ -113,7 +113,7 @@ impl Feed {
JOIN feed_channels fc on c.id = fc.channel_id
WHERE fc.feed_id = ?",
self.id.0
).fetch_all(pool).await?.into_iter()
).fetch_all(&pool.0).await?.into_iter()
.map(UnparsedChannel::parse).collect();
channels
@ -123,21 +123,21 @@ impl Feed {
#[cfg(test)]
mod tests {
use super::*;
use crate::User;
use sqlx::SqlitePool;
use crate::{User, Adapter, AdapterBuilder};
async fn setup_test_db() -> SqlitePool {
let pool = SqlitePool::connect("sqlite::memory:").await.unwrap();
sqlx::migrate!().run(&pool).await.unwrap();
pool
async fn setup_adapter() -> Adapter {
AdapterBuilder::new()
.database_url("sqlite::memory:")
.create().await.unwrap()
}
#[tokio::test]
async fn create_feed() {
let pool = setup_test_db().await;
let user = User::create(&pool, "Alice").await.unwrap();
let adapter = setup_adapter().await;
let pool = adapter.get_pool();
let user = User::create(pool, "Alice").await.unwrap();
let feed = Feed::create(&pool, user.id(), "Tech News").await.unwrap();
let feed = Feed::create(pool, user.id(), "Tech News").await.unwrap();
assert_eq!(feed.title(), "Tech News");
assert!(feed.id().0 > 0);
@ -145,13 +145,14 @@ mod tests {
#[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();
let adapter = setup_adapter().await;
let pool = adapter.get_pool();
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();
Feed::update_title(pool, feed.id(), "Technology").await.unwrap();
let updated = Feed::get(&pool, feed.id()).await.unwrap();
let updated = Feed::get(pool, feed.id()).await.unwrap();
assert_eq!(updated.title(), "Technology");
}
}