-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcandidate.rs
More file actions
63 lines (53 loc) · 1.56 KB
/
candidate.rs
File metadata and controls
63 lines (53 loc) · 1.56 KB
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
use chrono::{Local, NaiveDateTime};
use sqlx::{Result, SqliteExecutor, SqlitePool};
#[tracing::instrument(skip(ids, pool))]
pub async fn add(ids: impl Iterator<Item = &str>, pool: &SqlitePool) -> Result<()> {
let mut transaction = pool.begin().await?;
for id in ids {
sqlx::query!("INSERT INTO candidates (id) VALUES($1)", id)
.execute(&mut *transaction)
.await?;
}
transaction.commit().await
}
pub struct Candidate {
pub id: String,
pub verification_time: Option<NaiveDateTime>,
}
#[tracing::instrument(skip(executor))]
pub async fn get(id: &str, executor: impl SqliteExecutor<'_>) -> Result<Option<Candidate>> {
sqlx::query_as!(
Candidate,
r#"
SELECT id, verification_time as "verification_time: NaiveDateTime"
FROM candidates
WHERE id = $1 COLLATE NOCASE
"#,
id
)
.fetch_optional(executor)
.await
}
#[tracing::instrument(skip(executor))]
pub async fn verify(id: &str, executor: impl SqliteExecutor<'_>) -> Result<()> {
let now = Local::now().naive_local();
sqlx::query!(
r#"
UPDATE candidates
SET verification_time = $2
WHERE id = $1 COLLATE NOCASE
"#,
id,
now
)
.execute(executor)
.await?;
Ok(())
}
#[tracing::instrument(skip(executor))]
pub async fn delete(id: &str, executor: impl SqliteExecutor<'_>) -> Result<()> {
sqlx::query!("DELETE FROM candidates WHERE id = $1 COLLATE NOCASE", id)
.execute(executor)
.await?;
Ok(())
}