Move to DB module for visibility; separate db and client code
This commit is contained in:
parent
f5fc83a471
commit
3748606e21
9 changed files with 340 additions and 399 deletions
68
koucha/src/db/item.rs
Normal file
68
koucha/src/db/item.rs
Normal file
|
|
@ -0,0 +1,68 @@
|
|||
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
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue