clean up 'update_items'
This commit is contained in:
parent
3748606e21
commit
0bb9a81d60
4 changed files with 104 additions and 34 deletions
|
|
@ -4,16 +4,16 @@ use crate::{
|
|||
db::{
|
||||
ChannelId,
|
||||
ItemId,
|
||||
}
|
||||
},
|
||||
fetch::FetchedRSSItem,
|
||||
};
|
||||
use chrono::{DateTime, Utc};
|
||||
|
||||
pub struct UnparsedItem {
|
||||
pub id: i64,
|
||||
pub channel_id: i64,
|
||||
pub guid: String,
|
||||
pub fetched_at: String,
|
||||
|
||||
pub fetched_at: Option<String>,
|
||||
|
||||
pub title: Option<String>,
|
||||
pub description: Option<String>,
|
||||
pub content: Option<String>,
|
||||
|
|
@ -24,8 +24,10 @@ impl UnparsedItem {
|
|||
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),
|
||||
fetched_at: self.fetched_at.as_deref()
|
||||
.map(DateTime::parse_from_rfc2822)
|
||||
.transpose()?
|
||||
.map(|dt| dt.with_timezone(&Utc)),
|
||||
|
||||
title: self.title,
|
||||
description: self.description,
|
||||
|
|
@ -37,32 +39,55 @@ impl UnparsedItem {
|
|||
pub struct Item {
|
||||
id: ItemId,
|
||||
channel_id: ChannelId,
|
||||
guid: String,
|
||||
fetched_at: DateTime<Utc>,
|
||||
|
||||
fetched_at: Option<DateTime<Utc>>,
|
||||
title: Option<String>,
|
||||
description: Option<String>,
|
||||
content: Option<String>,
|
||||
}
|
||||
|
||||
impl Item {
|
||||
pub fn id(&self) -> ItemId { self.id }
|
||||
pub fn channel(&self) -> ChannelId { 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: ChannelId, guid: &str,
|
||||
fetched_at: DateTime<Utc>
|
||||
pool: &AdapterPool, from_channel: ChannelId, guid: &str
|
||||
) -> 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(?, ?, ?)
|
||||
"INSERT INTO items (channel_id, guid)
|
||||
VALUES(?, ?)
|
||||
ON CONFLICT(id) DO UPDATE SET id = id
|
||||
RETURNING id as `id!`, channel_id, guid, fetched_at, title, description,
|
||||
RETURNING id as `id!`, channel_id, fetched_at, title, description,
|
||||
content",
|
||||
int_channel_id, guid, last_fetched
|
||||
int_channel_id, guid
|
||||
).fetch_one(&pool.0).await?.parse();
|
||||
|
||||
item
|
||||
}
|
||||
|
||||
pub async fn update_content(
|
||||
&self, pool: &AdapterPool, fetched: &FetchedRSSItem, fetched_at: &DateTime<Utc>
|
||||
) -> Result<()> {
|
||||
let title = fetched.title();
|
||||
let description = fetched.description();
|
||||
let content = fetched.content();
|
||||
let string_fetched_at = fetched_at.to_rfc2822();
|
||||
|
||||
sqlx::query!(
|
||||
"UPDATE items
|
||||
SET title = ?, description = ?, content = ?,
|
||||
fetched_at = ?
|
||||
WHERE id = ?",
|
||||
title, description, content, string_fetched_at,
|
||||
self.id.0
|
||||
).execute(&pool.0).await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue