Finalize feed, tests which don't need channel creation
This commit is contained in:
parent
e07a98ddbb
commit
d7123fb153
1 changed files with 68 additions and 10 deletions
|
|
@ -36,35 +36,58 @@ impl Feed {
|
||||||
pub fn id(&self) -> FeedId { self.id }
|
pub fn id(&self) -> FeedId { self.id }
|
||||||
pub fn title(&self) -> &str { &self.title }
|
pub fn title(&self) -> &str { &self.title }
|
||||||
|
|
||||||
pub async fn create(
|
pub async fn get(
|
||||||
pool: &SqlitePool, id: UserId, name: &str
|
pool: &SqlitePool, id: FeedId
|
||||||
) -> Result<Self> {
|
) -> Result<Self> {
|
||||||
let int_id = i64::from(id);
|
let feed = sqlx::query_as!(
|
||||||
|
UnparsedFeed,
|
||||||
|
"SELECT id, title FROM feeds WHERE id = ?",
|
||||||
|
id.0
|
||||||
|
).fetch_one(pool).await?.parse();
|
||||||
|
|
||||||
|
feed
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn create(
|
||||||
|
pool: &SqlitePool, user_id: UserId, title: &str
|
||||||
|
) -> Result<Self> {
|
||||||
|
let int_id = i64::from(user_id);
|
||||||
let new_feed = sqlx::query_as!(
|
let new_feed = sqlx::query_as!(
|
||||||
UnparsedFeed,
|
UnparsedFeed,
|
||||||
"INSERT INTO feeds (user_id, title)
|
"INSERT INTO feeds (user_id, title)
|
||||||
VALUES (?, ?)
|
VALUES (?, ?)
|
||||||
RETURNING id as `id!`, title",
|
RETURNING id as `id!`, title",
|
||||||
int_id, name
|
int_id, title
|
||||||
).fetch_one(pool).await?.parse();
|
).fetch_one(pool).await?.parse();
|
||||||
|
|
||||||
new_feed
|
new_feed
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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(())
|
||||||
|
}
|
||||||
|
|
||||||
pub async fn add_channel(
|
pub async fn add_channel(
|
||||||
pool: &SqlitePool, id: FeedId, channel_id: ChannelId
|
&self, pool: &SqlitePool, channel_id: ChannelId
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
sqlx::query!(
|
sqlx::query!(
|
||||||
"INSERT INTO feed_channels (feed_id, channel_id)
|
"INSERT INTO feed_channels (feed_id, channel_id)
|
||||||
VALUES (?, ?)",
|
VALUES (?, ?)",
|
||||||
id.0, channel_id.0
|
self.id.0, channel_id.0
|
||||||
).execute(pool).await?;
|
).execute(pool).await?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn get_items(
|
pub async fn get_items(
|
||||||
pool: &SqlitePool, id: FeedId, limit: u8, offset: u32
|
&self, pool: &SqlitePool, limit: u8, offset: u32
|
||||||
) -> Result<Vec<Item>> {
|
) -> Result<Vec<Item>> {
|
||||||
let items = sqlx::query_as!(
|
let items = sqlx::query_as!(
|
||||||
Item,
|
Item,
|
||||||
|
|
@ -72,14 +95,14 @@ impl Feed {
|
||||||
WHERE feed_id = ? AND archived = FALSE
|
WHERE feed_id = ? AND archived = FALSE
|
||||||
ORDER BY score DESC
|
ORDER BY score DESC
|
||||||
LIMIT ? OFFSET ?",
|
LIMIT ? OFFSET ?",
|
||||||
id.0, limit, offset
|
self.id.0, limit, offset
|
||||||
).fetch_all(pool).await?;
|
).fetch_all(pool).await?;
|
||||||
|
|
||||||
Ok(items)
|
Ok(items)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn get_channels(
|
pub async fn get_channels(
|
||||||
pool: &SqlitePool, id: FeedId
|
&self, pool: &SqlitePool
|
||||||
) -> Result<Vec<Channel>> {
|
) -> Result<Vec<Channel>> {
|
||||||
let channels: Result<Vec<Channel>> = sqlx::query_as!(
|
let channels: Result<Vec<Channel>> = sqlx::query_as!(
|
||||||
UnparsedChannel,
|
UnparsedChannel,
|
||||||
|
|
@ -87,7 +110,7 @@ impl Feed {
|
||||||
FROM channels c
|
FROM channels c
|
||||||
JOIN feed_channels fc on c.id = fc.channel_id
|
JOIN feed_channels fc on c.id = fc.channel_id
|
||||||
WHERE fc.feed_id = ?",
|
WHERE fc.feed_id = ?",
|
||||||
id.0
|
self.id.0
|
||||||
).fetch_all(pool).await?.into_iter()
|
).fetch_all(pool).await?.into_iter()
|
||||||
.map(UnparsedChannel::parse).collect();
|
.map(UnparsedChannel::parse).collect();
|
||||||
|
|
||||||
|
|
@ -95,3 +118,38 @@ impl Feed {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[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");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue