db, add item and functions to get items
Creates item. It's pretty barebowns until I implement fetching. I also added get functions for feed and channel to get items with.
This commit is contained in:
parent
09b26c1b39
commit
55c3e967bc
5 changed files with 179 additions and 2 deletions
|
|
@ -3,7 +3,11 @@ use chrono::{DateTime, Utc};
|
|||
use crate::{
|
||||
Result,
|
||||
AdapterPool,
|
||||
db::ChannelKey,
|
||||
db::{
|
||||
ChannelKey,
|
||||
Item,
|
||||
item::UnparsedItem,
|
||||
},
|
||||
};
|
||||
|
||||
pub struct UnparsedChannel {
|
||||
|
|
@ -74,6 +78,17 @@ impl Channel {
|
|||
link_str, link_str // We use the url as a placeholder title
|
||||
).fetch_one(&pool.0).await?.parse()
|
||||
}
|
||||
|
||||
pub async fn get_items(&self, pool: &AdapterPool) -> Result<Vec<Item>> {
|
||||
sqlx::query_as!(
|
||||
UnparsedItem,
|
||||
"SELECT id as `id!`, channel_id, fetched_at, title, description,
|
||||
content
|
||||
FROM items
|
||||
WHERE channel_id = ?",
|
||||
self.key.0
|
||||
).fetch_all(&pool.0).await?.into_iter().map(UnparsedItem::parse).collect()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
|
@ -81,7 +96,7 @@ mod tests {
|
|||
use super::*;
|
||||
use crate::{
|
||||
test_utils::{
|
||||
FEED1, FEED2, CHANNEL_TITLE, CHANNEL_DESC,
|
||||
FEED1, FEED2, CHANNEL_TITLE, CHANNEL_DESC, ITEM_GUID, ITEM_GUID2,
|
||||
setup_adapter,
|
||||
setup_channel,
|
||||
},
|
||||
|
|
@ -176,4 +191,18 @@ mod tests {
|
|||
|
||||
assert_eq!(channels.len(), 2);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn get_items() {
|
||||
let adapter = setup_adapter().await;
|
||||
let pool = adapter.get_pool();
|
||||
let channel = setup_channel(pool).await;
|
||||
|
||||
Item::get_or_create(pool, channel.key(), ITEM_GUID).await.unwrap();
|
||||
Item::get_or_create(pool, channel.key(), ITEM_GUID2).await.unwrap();
|
||||
|
||||
let items = channel.get_items(pool).await.unwrap();
|
||||
|
||||
assert_eq!(items.len(), 2);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue