Move to DB module for visibility; separate db and client code

This commit is contained in:
Julia Lange 2026-01-22 13:49:49 -08:00
parent f5fc83a471
commit 3748606e21
Signed by: Julia
SSH key fingerprint: SHA256:5DJcfxa5/fKCYn57dcabJa2vN2e6eT0pBerYi5SUbto
9 changed files with 340 additions and 399 deletions

68
koucha/src/db/item.rs Normal file
View 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
}
}