use crate::{ Result, AdapterPool, db::{ ChannelKey, ItemKey, }, }; use chrono::{DateTime, Utc}; pub struct UnparsedItem { pub id: i64, pub channel_id: i64, pub fetched_at: Option, pub title: Option, pub description: Option, pub content: Option, } impl UnparsedItem { pub fn parse(self) -> Result { Ok(Item { key: ItemKey(self.id), channel_id: ChannelKey(self.channel_id), fetched_at: match self.fetched_at { Some(dt_str) => Some(DateTime::parse_from_rfc2822(&dt_str)? .with_timezone(&Utc)), None => None, }, title: self.title, description: self.description, content: self.content, }) } } pub struct Item { key: ItemKey, channel_id: ChannelKey, fetched_at: Option>, title: Option, description: Option, content: Option, } impl Item { pub fn key(&self) -> ItemKey { self.key } pub fn channel(&self) -> ChannelKey { self.channel_id } pub fn title(&self) -> Option<&str> { self.title.as_deref() } pub fn description(&self) -> Option<&str> { self.description.as_deref() } pub fn content(&self) -> Option<&str> { self.content.as_deref() } pub async fn get_or_create( pool: &AdapterPool, from_channel: ChannelKey, guid: &str ) -> Result { let item = sqlx::query_as!( UnparsedItem, "INSERT INTO items (channel_id, guid) VALUES (?, ?) ON CONFLICT(channel_id, guid) DO UPDATE SET channel_id = channel_id RETURNING id as `id!`, channel_id, fetched_at, title, description, content", from_channel.0, guid ).fetch_one(&pool.0).await?.parse(); item } } #[cfg(test)] mod tests { use super::*; use crate::test_utils::{ ITEM_GUID, ITEM_TITLE, ITEM_DESC, ITEM_CONT, setup_adapter, setup_channel, }; use chrono::TimeZone; // UnparsedItem tests #[test] fn parse_unparsed_item() { const ITEM_ID: i64 = 1; const CHANNEL_ID: i64 = 1; let date: DateTime = Utc.with_ymd_and_hms(2020,1,1,0,0,0).unwrap(); let raw_item = UnparsedItem { id: ITEM_ID, channel_id: CHANNEL_ID, fetched_at: Some(date.to_rfc2822()), title: Some(ITEM_TITLE.to_string()), description: Some(ITEM_DESC.to_string()), content: Some(ITEM_CONT.to_string()), }; let item = raw_item.parse().unwrap(); assert_eq!(item.key.0, ITEM_ID); assert_eq!(item.channel_id.0, CHANNEL_ID); assert_eq!(item.fetched_at, Some(date)); assert_eq!(item.title, Some(ITEM_TITLE.to_string())); assert_eq!(item.description, Some(ITEM_DESC.to_string())); assert_eq!(item.content, Some(ITEM_CONT.to_string())); } // Item Tests #[tokio::test] async fn get_or_create_duplicate() { let adapter = setup_adapter().await; let pool = adapter.get_pool(); let channel = setup_channel(pool).await; let item1 = Item::get_or_create(pool, channel.key(), ITEM_GUID).await.unwrap(); let item2 = Item::get_or_create(pool, channel.key(), ITEM_GUID).await.unwrap(); assert_eq!(item1.key(), item2.key()); } }