Koucha/koucha/src/db/item.rs

69 lines
1.5 KiB
Rust
Raw Normal View History

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<String>,
pub description: Option<String>,
pub content: Option<String>,
}
impl UnparsedItem {
pub fn parse(self) -> Result<Item> {
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<Utc>,
title: Option<String>,
description: Option<String>,
content: Option<String>,
}
impl Item {
pub async fn get_or_create(
pool: &AdapterPool, from_channel: ChannelId, guid: &str,
fetched_at: DateTime<Utc>
) -> Result<Self> {
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
}
}