Skip to content

Commit 92a071e

Browse files
committed
store created_at and is_default for client records
1 parent 34b8785 commit 92a071e

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

coreclient/src/clients/persistence.rs

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
//
33
// SPDX-License-Identifier: AGPL-3.0-or-later
44

5+
use chrono::{DateTime, Utc};
56
use phnxtypes::{codec::PhnxCodec, identifiers::AsClientId};
67
use rusqlite::{params, types::FromSql, Connection, OptionalExtension, ToSql};
78
use serde::{Deserialize, Serialize};
@@ -47,7 +48,7 @@ impl Storable for UserCreationState {
4748
const CREATE_TABLE_STATEMENT: &'static str = "
4849
CREATE TABLE IF NOT EXISTS user_creation_state (
4950
client_id BLOB PRIMARY KEY,
50-
state BLOB NOT NULL
51+
state BLOB NOT NULL,
5152
);";
5253

5354
fn from_row(row: &rusqlite::Row) -> anyhow::Result<Self, rusqlite::Error> {
@@ -81,9 +82,11 @@ impl UserCreationState {
8182
impl Storable for ClientRecord {
8283
const CREATE_TABLE_STATEMENT: &'static str = "
8384
CREATE TABLE IF NOT EXISTS client_record (
84-
client_id BLOB PRIMARY KEY,
85-
record_state TEXT NOT NULL CHECK (record_state IN ('in_progress', 'finished'))
86-
);";
85+
client_id BLOB NOT NULL PRIMARY KEY,
86+
record_state TEXT NOT NULL CHECK (record_state IN ('in_progress', 'finished')),
87+
created_at DATETIME NOT NULL DEFAULT (strftime('%Y-%m-%dT%H:%M:%SZ', 'now', 'utc')),
88+
is_default BOOLEAN NOT NULL DEFAULT FALSE
89+
)";
8790

8891
fn from_row(row: &rusqlite::Row) -> anyhow::Result<Self, rusqlite::Error> {
8992
let record_state_str: String = row.get(1)?;
@@ -92,9 +95,13 @@ impl Storable for ClientRecord {
9295
"finished" => ClientRecordState::Finished,
9396
_ => return Err(rusqlite::Error::InvalidQuery),
9497
};
98+
let created_at: DateTime<Utc> = row.get(2)?;
99+
let is_default: bool = row.get(3)?;
95100
Ok(Self {
96101
as_client_id: row.get(0)?,
97102
client_record_state,
103+
created_at,
104+
is_default,
98105
})
99106
}
100107
}
@@ -136,8 +143,15 @@ impl ClientRecord {
136143
ClientRecordState::Finished => "finished",
137144
};
138145
connection.execute(
139-
"INSERT OR REPLACE INTO client_record (client_id, record_state) VALUES (?1, ?2)",
140-
params![self.as_client_id, record_state_str],
146+
"INSERT OR REPLACE INTO client_record
147+
(client_id, record_state, created_at, is_default)
148+
VALUES (?1, ?2, ?3, ?4)",
149+
params![
150+
self.as_client_id,
151+
record_state_str,
152+
self.created_at,
153+
self.is_default,
154+
],
141155
)?;
142156
Ok(())
143157
}

coreclient/src/clients/store.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,13 +178,17 @@ pub enum ClientRecordState {
178178
pub struct ClientRecord {
179179
pub as_client_id: AsClientId,
180180
pub client_record_state: ClientRecordState,
181+
pub created_at: DateTime<Utc>,
182+
pub is_default: bool,
181183
}
182184

183185
impl ClientRecord {
184186
pub(super) fn new(as_client_id: AsClientId) -> Self {
185187
Self {
186188
as_client_id,
187189
client_record_state: ClientRecordState::InProgress,
190+
created_at: Utc::now(),
191+
is_default: false,
188192
}
189193
}
190194

0 commit comments

Comments
 (0)