this repo has no description
1use sqlx::PgPool;
2
3use super::super::{DeviceData, OAuthError};
4
5pub async fn create_device(
6 pool: &PgPool,
7 device_id: &str,
8 data: &DeviceData,
9) -> Result<(), OAuthError> {
10 sqlx::query!(
11 r#"
12 INSERT INTO oauth_device (id, session_id, user_agent, ip_address, last_seen_at)
13 VALUES ($1, $2, $3, $4, $5)
14 "#,
15 device_id,
16 data.session_id,
17 data.user_agent,
18 data.ip_address,
19 data.last_seen_at,
20 )
21 .execute(pool)
22 .await?;
23
24 Ok(())
25}
26
27pub async fn get_device(pool: &PgPool, device_id: &str) -> Result<Option<DeviceData>, OAuthError> {
28 let row = sqlx::query!(
29 r#"
30 SELECT session_id, user_agent, ip_address, last_seen_at
31 FROM oauth_device
32 WHERE id = $1
33 "#,
34 device_id
35 )
36 .fetch_optional(pool)
37 .await?;
38
39 Ok(row.map(|r| DeviceData {
40 session_id: r.session_id,
41 user_agent: r.user_agent,
42 ip_address: r.ip_address,
43 last_seen_at: r.last_seen_at,
44 }))
45}
46
47pub async fn update_device_last_seen(
48 pool: &PgPool,
49 device_id: &str,
50) -> Result<(), OAuthError> {
51 sqlx::query!(
52 r#"
53 UPDATE oauth_device
54 SET last_seen_at = NOW()
55 WHERE id = $1
56 "#,
57 device_id
58 )
59 .execute(pool)
60 .await?;
61
62 Ok(())
63}
64
65pub async fn delete_device(pool: &PgPool, device_id: &str) -> Result<(), OAuthError> {
66 sqlx::query!(
67 r#"
68 DELETE FROM oauth_device WHERE id = $1
69 "#,
70 device_id
71 )
72 .execute(pool)
73 .await?;
74
75 Ok(())
76}
77
78pub async fn upsert_account_device(
79 pool: &PgPool,
80 did: &str,
81 device_id: &str,
82) -> Result<(), OAuthError> {
83 sqlx::query!(
84 r#"
85 INSERT INTO oauth_account_device (did, device_id, created_at, updated_at)
86 VALUES ($1, $2, NOW(), NOW())
87 ON CONFLICT (did, device_id) DO UPDATE SET updated_at = NOW()
88 "#,
89 did,
90 device_id
91 )
92 .execute(pool)
93 .await?;
94
95 Ok(())
96}