1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
//! Repository for guest-persona writes (hand-written; user-owned).
//!
//! Holds the SQL the [`crate::application::service::GuestWriteService`]
//! orchestrates: mint, self-rename (last_connection_dt refreshed — the GC
//! liveness pointer), and touch. Runtime queries — no `.sqlx` cache.
use sqlx::{PgConnection, PgPool};
use uuid::Uuid;
/// Hand-written guest SQL. Services orchestrate; this holds SQL.
pub struct GuestRepository;
impl GuestRepository {
pub fn new() -> Self {
Self
}
/// Insert a fresh guest persona (active; last_connection_dt now).
pub async fn insert_guest(
conn: &mut PgConnection,
guest_id: Uuid,
name: &str,
) -> Result<(), sqlx::Error> {
sqlx::query(r#"
INSERT INTO messaging.mail_guests (id, name, last_connection_dt, active)
VALUES ($1, $2, NOW(), TRUE)
"#)
.bind(guest_id)
.bind(name)
.execute(&mut *conn)
.await?;
Ok(())
}
/// Rename a live guest + refresh the liveness pointer. False when the row
/// is gone or already soft-deleted.
pub async fn update_name(
conn: &mut PgConnection,
guest_id: Uuid,
new_name: &str,
) -> Result<bool, sqlx::Error> {
let res = sqlx::query(r#"
UPDATE messaging.mail_guests
SET name = $2, last_connection_dt = NOW()
WHERE id = $1 AND active AND (metadata->>'deleted_at') IS NULL
"#)
.bind(guest_id)
.bind(new_name)
.execute(&mut *conn)
.await?;
Ok(res.rows_affected() > 0)
}
/// Refresh the liveness pointer only (every guest-visible request path).
pub async fn touch(pool: &PgPool, guest_id: Uuid) -> Result<(), sqlx::Error> {
sqlx::query(r#"
UPDATE messaging.mail_guests SET last_connection_dt = NOW()
WHERE id = $1 AND active
"#)
.bind(guest_id)
.execute(pool)
.await?;
Ok(())
}
}
impl Default for GuestRepository {
fn default() -> Self {
Self::new()
}
}