rovenue/cache/
identity.rs1use rusqlite::params;
2
3use crate::error::RovenueResult;
4
5use super::CacheStore;
6
7#[derive(Debug, Clone)]
8pub struct IdentityRow {
9 pub rovenue_id: String,
10 pub app_user_id: Option<String>,
11 pub synced: bool,
12 pub created_at_ms: u64,
13}
14
15pub struct IdentityRepo<'a> {
16 store: &'a CacheStore,
17}
18
19impl<'a> IdentityRepo<'a> {
20 pub fn new(store: &'a CacheStore) -> Self {
21 Self { store }
22 }
23
24 pub fn load(&self) -> RovenueResult<Option<IdentityRow>> {
25 self.store.with_conn(|c| {
26 let mut stmt = c.prepare(
27 "SELECT rovenue_id, app_user_id, synced, created_at_ms FROM identity WHERE id = 1",
28 )?;
29 let mut rows = stmt.query([])?;
30 if let Some(r) = rows.next()? {
31 Ok(Some(IdentityRow {
32 rovenue_id: r.get(0)?,
33 app_user_id: r.get(1)?,
34 synced: r.get::<_, i64>(2)? != 0,
35 created_at_ms: r.get::<_, i64>(3)? as u64,
36 }))
37 } else {
38 Ok(None)
39 }
40 })
41 }
42
43 pub fn save(&self, row: &IdentityRow) -> RovenueResult<()> {
44 self.store.with_conn(|c| {
45 c.execute(
46 "INSERT INTO identity (id, rovenue_id, app_user_id, synced, created_at_ms)
47 VALUES (1, ?1, ?2, ?3, ?4)
48 ON CONFLICT(id) DO UPDATE SET
49 rovenue_id = excluded.rovenue_id,
50 app_user_id = excluded.app_user_id,
51 synced = excluded.synced,
52 created_at_ms = excluded.created_at_ms",
53 params![
54 row.rovenue_id,
55 row.app_user_id,
56 row.synced as i64,
57 row.created_at_ms as i64
58 ],
59 )?;
60 Ok(())
61 })
62 }
63}