2026-04-14 15:19:46 +03:00
|
|
|
use crate::lang::CardStatistics;
|
2026-04-13 22:25:58 +03:00
|
|
|
use crate::repetitions::CardSetSettings;
|
2026-04-14 15:19:46 +03:00
|
|
|
use rusqlite::Connection;
|
2026-04-13 22:25:58 +03:00
|
|
|
|
|
|
|
|
pub fn load_stats_of_set(set: &CardSetSettings, connection: &Connection) -> Vec<CardStatistics> {
|
|
|
|
|
let mut stmt = connection.prepare("SELECT id, word_id, score, last_opened FROM card_stats WHERE set_id = ?1").unwrap();
|
|
|
|
|
let iter = stmt.query_map((set.id,), |row| {
|
|
|
|
|
Ok(CardStatistics {
|
|
|
|
|
id: row.get(0)?,
|
|
|
|
|
word_id: row.get(1)?,
|
2026-04-14 15:19:46 +03:00
|
|
|
set_id: set.id,
|
2026-04-13 22:25:58 +03:00
|
|
|
score: row.get(2)?,
|
2026-04-14 15:19:46 +03:00
|
|
|
last_open: row.get(3)?,
|
2026-04-13 22:25:58 +03:00
|
|
|
})
|
|
|
|
|
}).unwrap();
|
|
|
|
|
|
|
|
|
|
let mut buffer = vec![];
|
|
|
|
|
for word in iter {
|
|
|
|
|
buffer.push(word.unwrap());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
buffer
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-14 15:19:46 +03:00
|
|
|
pub fn add_stat(stat: &mut CardStatistics, connection: &Connection) {
|
2026-04-13 22:25:58 +03:00
|
|
|
let index = connection
|
|
|
|
|
.query_row(
|
2026-04-14 15:19:46 +03:00
|
|
|
"INSERT INTO card_stats (word_id, set_id, score, last_opened) VALUES (?1, ?2, ?3, ?4) RETURNING id",
|
2026-04-13 22:25:58 +03:00
|
|
|
(
|
2026-04-14 15:19:46 +03:00
|
|
|
&stat.word_id,
|
|
|
|
|
&stat.set_id,
|
|
|
|
|
&stat.score,
|
|
|
|
|
&stat.last_open,
|
2026-04-13 22:25:58 +03:00
|
|
|
),
|
|
|
|
|
|row| row.get(0)
|
|
|
|
|
)
|
|
|
|
|
.unwrap_or_else(|e| {println!("{}", e); 0});
|
|
|
|
|
|
2026-04-14 15:19:46 +03:00
|
|
|
stat.id = index;
|
2026-04-13 22:25:58 +03:00
|
|
|
}
|
|
|
|
|
|
2026-04-14 15:19:46 +03:00
|
|
|
pub fn update_stat(stat: &mut CardStatistics, connection: &Connection){
|
|
|
|
|
if stat.id == 0 {
|
|
|
|
|
add_stat(stat, &connection);
|
2026-04-13 22:25:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
else {
|
|
|
|
|
connection
|
|
|
|
|
.execute(
|
2026-04-14 15:19:46 +03:00
|
|
|
"UPDATE card_stats SET word_id = ?1, set_id = ?2, score = ?3, last_opened = ?4 WHERE id = ?5",
|
2026-04-13 22:25:58 +03:00
|
|
|
(
|
2026-04-14 15:19:46 +03:00
|
|
|
&stat.word_id,
|
|
|
|
|
&stat.set_id,
|
|
|
|
|
&stat.score,
|
|
|
|
|
&stat.last_open,
|
|
|
|
|
&stat.id
|
2026-04-13 22:25:58 +03:00
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
.unwrap_or_else(|e| {println!("{}", e); 0});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-14 15:19:46 +03:00
|
|
|
|
|
|
|
|
pub fn update_stat_score(stat: &CardStatistics, connection: &Connection){
|
|
|
|
|
connection
|
|
|
|
|
.execute(
|
|
|
|
|
"UPDATE card_stats SET score = ?1, last_opened = ?2 WHERE id = ?3",
|
|
|
|
|
(
|
|
|
|
|
&stat.score,
|
|
|
|
|
&stat.last_open,
|
|
|
|
|
&stat.id
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
.unwrap_or_else(|e| {println!("{}", e); 0});
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-13 22:25:58 +03:00
|
|
|
pub fn delete_set(set: &CardSetSettings, connection: &Connection) {
|
|
|
|
|
if set.id == 0 {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
connection
|
2026-04-14 15:19:46 +03:00
|
|
|
.execute("DELETE FROM card_stats WHERE id = ?1", (&set.id,))
|
2026-04-13 22:25:58 +03:00
|
|
|
.unwrap_or_else(|e| {
|
|
|
|
|
println!("{}", e);
|
|
|
|
|
0
|
|
|
|
|
});
|
|
|
|
|
}
|