-
Notifications
You must be signed in to change notification settings - Fork 25
/
postgres.rs
183 lines (171 loc) · 4.73 KB
/
postgres.rs
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
use super::IAccountRepo;
use nettu_scheduler_domain::{Account, PEMKey, ID};
use serde_json::Value;
use sqlx::{
types::{Json, Uuid},
FromRow, PgPool,
};
use tracing::error;
pub struct PostgresAccountRepo {
pool: PgPool,
}
impl PostgresAccountRepo {
pub fn new(pool: PgPool) -> Self {
Self { pool }
}
}
#[derive(Debug, FromRow)]
pub struct AccountRaw {
account_uid: Uuid,
secret_api_key: String,
public_jwt_key: Option<String>,
settings: Value,
}
impl From<AccountRaw> for Account {
fn from(e: AccountRaw) -> Self {
Self {
id: e.account_uid.into(),
secret_api_key: e.secret_api_key,
public_jwt_key: e.public_jwt_key.map(|key| PEMKey::new(key).unwrap()),
settings: serde_json::from_value(e.settings).unwrap(),
}
}
}
#[async_trait::async_trait]
impl IAccountRepo for PostgresAccountRepo {
async fn insert(&self, account: &Account) -> anyhow::Result<()> {
sqlx::query!(
r#"
INSERT INTO accounts(account_uid, secret_api_key, public_jwt_key, settings)
VALUES($1, $2, $3, $4)
"#,
account.id.as_ref(),
account.secret_api_key,
account.public_jwt_key.clone().map(|key| key.inner()),
Json(&account.settings) as _
)
.execute(&self.pool)
.await
.map_err(|e| {
error!(
"Unable to insert account: {:?}. DB returned error: {:?}",
account, e
);
e
})?;
Ok(())
}
async fn save(&self, account: &Account) -> anyhow::Result<()> {
sqlx::query!(
r#"
UPDATE accounts
SET secret_api_key = $2,
public_jwt_key = $3,
settings = $4
WHERE account_uid = $1
"#,
account.id.as_ref(),
account.secret_api_key,
account.public_jwt_key.clone().map(|key| key.inner()),
Json(&account.settings) as _
)
.execute(&self.pool)
.await
.map_err(|e| {
error!(
"Unable to save account: {:?}. DB returned error: {:?}",
account, e
);
e
})?;
Ok(())
}
async fn find(&self, account_id: &ID) -> Option<Account> {
let res: Option<AccountRaw> = sqlx::query_as!(
AccountRaw,
r#"
SELECT * FROM accounts
WHERE account_uid = $1
"#,
account_id.as_ref(),
)
.fetch_optional(&self.pool)
.await
.map_err(|e| {
error!(
"Find account with id: {:?} failed. DB returned error: {:?}",
account_id, e
);
e
})
.ok()?;
res.map(|account| account.into())
}
async fn find_many(&self, accounts_ids: &[ID]) -> anyhow::Result<Vec<Account>> {
let ids = accounts_ids
.iter()
.map(|id| *id.as_ref())
.collect::<Vec<_>>();
let accounts_raw: Vec<AccountRaw> = sqlx::query_as!(
AccountRaw,
"
SELECT * FROM accounts
WHERE account_uid = ANY($1)
",
&ids
)
.fetch_all(&self.pool)
.await
.map_err(|e| {
error!(
"Find accounts with ids: {:?} failed. DB returned error: {:?}",
accounts_ids, e
);
e
})?;
Ok(accounts_raw.into_iter().map(|acc| acc.into()).collect())
}
async fn delete(&self, account_id: &ID) -> Option<Account> {
let res: Option<AccountRaw> = sqlx::query_as!(
AccountRaw,
"
DELETE FROM accounts
WHERE account_uid = $1
RETURNING *
",
account_id.as_ref()
)
.fetch_optional(&self.pool)
.await
.map_err(|e| {
error!(
"Delete account with id: {:?} failed. DB returned error: {:?}",
account_id, e
);
e
})
.ok()?;
res.map(|acc| acc.into())
}
async fn find_by_apikey(&self, api_key: &str) -> Option<Account> {
let res: Option<AccountRaw> = sqlx::query_as!(
AccountRaw,
"
SELECT * FROM accounts
WHERE secret_api_key = $1
",
api_key
)
.fetch_optional(&self.pool)
.await
.map_err(|e| {
error!(
"Find account with api_key: {:?} failed. DB returned error: {:?}",
api_key, e
);
e
})
.ok()?;
res.map(|acc| acc.into())
}
}