Skip to main content

max / makenotwork

1.5 KB · 53 lines History Blame Raw
1 //! WHOIS/domain-expiry check storage.
2
3 use super::{Result, SqlitePool, WhoisResult};
4 use tracing::instrument;
5
6 #[derive(Debug, Clone, sqlx::FromRow, serde::Serialize)]
7 pub struct WhoisCheckRow {
8 pub id: i64,
9 pub target: String,
10 pub domain: String,
11 pub registrar: Option<String>,
12 pub expiry_date: Option<String>,
13 pub days_remaining: Option<i64>,
14 pub nameservers: Option<String>,
15 pub checked_at: String,
16 pub error: Option<String>,
17 }
18
19 #[instrument(skip_all)]
20 pub async fn insert_whois_check(pool: &SqlitePool, result: &WhoisResult) -> Result<i64> {
21 let nameservers = serde_json::to_string(&result.nameservers).unwrap_or_default();
22
23 let row = sqlx::query(
24 "INSERT INTO whois_checks (target, domain, registrar, expiry_date, days_remaining, nameservers, checked_at, error)
25 VALUES (?, ?, ?, ?, ?, ?, ?, ?)",
26 )
27 .bind(&result.target)
28 .bind(&result.domain)
29 .bind(&result.registrar)
30 .bind(&result.expiry_date)
31 .bind(result.days_remaining)
32 .bind(&nameservers)
33 .bind(&result.checked_at)
34 .bind(&result.error)
35 .execute(pool)
36 .await?;
37 Ok(row.last_insert_rowid())
38 }
39
40 #[instrument(skip_all)]
41 pub async fn get_latest_whois_check(
42 pool: &SqlitePool,
43 target: &str,
44 ) -> Result<Option<WhoisCheckRow>> {
45 Ok(sqlx::query_as::<_, WhoisCheckRow>(
46 "SELECT id, target, domain, registrar, expiry_date, days_remaining, nameservers, checked_at, error
47 FROM whois_checks WHERE target = ? ORDER BY id DESC LIMIT 1",
48 )
49 .bind(target)
50 .fetch_optional(pool)
51 .await?)
52 }
53