Skip to content

Commit 7cbc872

Browse files
committed
store the timestamp when a card is added
1 parent c845c91 commit 7cbc872

4 files changed

Lines changed: 13 additions & 6 deletions

File tree

src/db.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ impl Database {
6464
}
6565

6666
/// Add a new card to the database.
67-
pub fn add_card(&self, card: &Card) -> Fallible<()> {
67+
pub fn add_card(&self, card: &Card, now: Timestamp) -> Fallible<()> {
6868
log::debug!("Adding new card: {}", card.hash());
6969
let card_row = match card.content() {
7070
CardContent::Basic { question, answer } => CardRow {
@@ -75,6 +75,7 @@ impl Database {
7575
answer: answer.to_string(),
7676
cloze_start: 0,
7777
cloze_end: 0,
78+
added_at: now,
7879
},
7980
CardContent::Cloze { text, start, end } => CardRow {
8081
card_hash: card.hash(),
@@ -84,6 +85,7 @@ impl Database {
8485
answer: "".to_string(),
8586
cloze_start: *start,
8687
cloze_end: *end,
88+
added_at: now,
8789
},
8890
};
8991
let mut conn = self.acquire();
@@ -180,10 +182,11 @@ struct CardRow {
180182
answer: String,
181183
cloze_start: usize,
182184
cloze_end: usize,
185+
added_at: Timestamp,
183186
}
184187

185188
fn insert_card(tx: &Transaction, card: &CardRow) -> Fallible<()> {
186-
let sql = "insert into cards (card_hash, card_type, deck_name, question, answer, cloze_start, cloze_end) values (?, ?, ?, ?, ?, ?, ?);";
189+
let sql = "insert into cards (card_hash, card_type, deck_name, question, answer, cloze_start, cloze_end, added_at) values (?, ?, ?, ?, ?, ?, ?, ?);";
187190
tx.execute(
188191
sql,
189192
(
@@ -194,6 +197,7 @@ fn insert_card(tx: &Transaction, card: &CardRow) -> Fallible<()> {
194197
&card.answer,
195198
card.cloze_start,
196199
card.cloze_end,
200+
&card.added_at,
197201
),
198202
)?;
199203
Ok(())

src/drill/server.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ use crate::types::hash::Hash;
4848
use crate::types::timestamp::Timestamp;
4949

5050
pub async fn start_server(directory: PathBuf, today: Date) -> Fallible<()> {
51+
let session_started_at = Timestamp::now();
52+
5153
if !directory.exists() {
5254
return fail("directory does not exist.");
5355
}
@@ -83,7 +85,7 @@ pub async fn start_server(directory: PathBuf, today: Date) -> Fallible<()> {
8385
// the database.
8486
for card in all_cards.iter() {
8587
if !db_hashes.contains(&card.hash()) {
86-
db.add_card(card)?;
88+
db.add_card(card, session_started_at)?;
8789
}
8890
}
8991

@@ -103,7 +105,7 @@ pub async fn start_server(directory: PathBuf, today: Date) -> Fallible<()> {
103105
directory,
104106
macros,
105107
total_cards: due_today.len(),
106-
session_started_at: Timestamp::now(),
108+
session_started_at,
107109
mutable: Arc::new(Mutex::new(MutableState {
108110
reveal: false,
109111
db,

src/schema.sql

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ create table cards (
77
question text not null,
88
answer text not null,
99
cloze_start integer not null,
10-
cloze_end integer not null
10+
cloze_end integer not null,
11+
added_at text not null
1112
) strict;
1213

1314
create table sessions (

src/types/timestamp.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use rusqlite::types::ValueRef;
2323

2424
use crate::types::date::Date;
2525

26-
#[derive(Clone)]
26+
#[derive(Clone, Copy)]
2727
pub struct Timestamp(DateTime<Utc>);
2828

2929
impl Timestamp {

0 commit comments

Comments
 (0)