repetition works

This commit is contained in:
2026-04-14 15:19:46 +03:00
parent a1217f90ec
commit 13156da39f
5 changed files with 281 additions and 60 deletions
+35 -24
View File
@@ -1,10 +1,6 @@
use std::collections::HashMap;
use chrono::{DateTime, Utc};
use iced::time::now;
use rusqlite::{Connection, ToSql};
use rusqlite::types::ToSqlOutput;
use crate::lang::{CardSet, CardStatistics, DictionaryElement};
use crate::lang::CardStatistics;
use crate::repetitions::CardSetSettings;
use rusqlite::Connection;
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();
@@ -12,8 +8,9 @@ pub fn load_stats_of_set(set: &CardSetSettings, connection: &Connection) -> Vec<
Ok(CardStatistics {
id: row.get(0)?,
word_id: row.get(1)?,
set_id: set.id,
score: row.get(2)?,
last_open: row.get(2)?,
last_open: row.get(3)?,
})
}).unwrap();
@@ -25,50 +22,64 @@ pub fn load_stats_of_set(set: &CardSetSettings, connection: &Connection) -> Vec<
buffer
}
pub fn add_set(set: &mut CardSetSettings, connection: &Connection) {
pub fn add_stat(stat: &mut CardStatistics, connection: &Connection) {
let index = connection
.query_row(
"INSERT INTO card_set (name, forward, backward, filter) VALUES (?1, ?2, ?3, ?4) RETURNING id",
"INSERT INTO card_stats (word_id, set_id, score, last_opened) VALUES (?1, ?2, ?3, ?4) RETURNING id",
(
&set.name,
&set.forward,
&set.backward,
&set.filter,
&stat.word_id,
&stat.set_id,
&stat.score,
&stat.last_open,
),
|row| row.get(0)
)
.unwrap_or_else(|e| {println!("{}", e); 0});
set.id = index;
stat.id = index;
}
pub fn update_card_set(set: &mut CardSetSettings, connection: &Connection){
if set.id == 0 {
add_set(set, &connection);
pub fn update_stat(stat: &mut CardStatistics, connection: &Connection){
if stat.id == 0 {
add_stat(stat, &connection);
}
else {
connection
.execute(
"UPDATE card_set SET name = ?1, forward = ?2, backward = ?3, filter = ?4 WHERE id = ?5",
"UPDATE card_stats SET word_id = ?1, set_id = ?2, score = ?3, last_opened = ?4 WHERE id = ?5",
(
&set.name,
&set.forward,
&set.backward,
&set.filter,
&set.id
&stat.word_id,
&stat.set_id,
&stat.score,
&stat.last_open,
&stat.id
),
)
.unwrap_or_else(|e| {println!("{}", e); 0});
}
}
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});
}
pub fn delete_set(set: &CardSetSettings, connection: &Connection) {
if set.id == 0 {
return;
}
connection
.execute("DELETE FROM card_set WHERE id = ?1", (&set.id,))
.execute("DELETE FROM card_stats WHERE id = ?1", (&set.id,))
.unwrap_or_else(|e| {
println!("{}", e);
0