use crate::{ Result, AdapterPool, db::{ ChannelId, ItemId, } }; use chrono::{DateTime, Utc}; pub struct UnparsedItem { pub id: i64, pub channel_id: i64, pub guid: String, pub fetched_at: String, pub title: Option, pub description: Option, pub content: Option, } impl UnparsedItem { pub fn parse(self) -> Result { Ok(Item { id: ItemId(self.id), channel_id: ChannelId(self.channel_id), guid: self.guid, fetched_at: DateTime::parse_from_rfc2822(&self.fetched_at)?.with_timezone(&Utc), title: self.title, description: self.description, content: self.content, }) } } pub struct Item { id: ItemId, channel_id: ChannelId, guid: String, fetched_at: DateTime, title: Option, description: Option, content: Option, } impl Item { pub async fn get_or_create( pool: &AdapterPool, from_channel: ChannelId, guid: &str, fetched_at: DateTime ) -> Result { let int_channel_id = i64::from(from_channel); let last_fetched = fetched_at.to_rfc2822(); let item = sqlx::query_as!( UnparsedItem, "INSERT INTO items (channel_id, guid, fetched_at) VALUES(?, ?, ?) ON CONFLICT(id) DO UPDATE SET id = id RETURNING id as `id!`, channel_id, guid, fetched_at, title, description, content", int_channel_id, guid, last_fetched ).fetch_one(&pool.0).await?.parse(); item } }