2026-08-24 11:35:37 +03:00
|
|
|
use crate::core::srs::WordOpenMode;
|
2026-09-04 11:18:23 +03:00
|
|
|
use crate::data::entities::HistoryNote;
|
|
|
|
|
use crate::data::repositories::{DataRepository, DataRepositoryInner};
|
|
|
|
|
use crate::data_provider::{SqliteDbLayer, TableAccessQuerySet};
|
|
|
|
|
use rusqlite::types::{FromSql, FromSqlResult, ToSqlOutput, ValueRef};
|
|
|
|
|
use rusqlite::{Error, Params, Row, ToSql};
|
2026-08-28 17:44:14 +03:00
|
|
|
|
2026-09-04 11:18:23 +03:00
|
|
|
pub type HistoryRepository = DataRepository<HistoryNote, HistoryDataLayer>;
|
|
|
|
|
pub type HistoryRepositoryInner = DataRepositoryInner<HistoryNote, HistoryDataLayer>;
|
|
|
|
|
pub type HistoryDataLayer = SqliteDbLayer<HistoryNote, HistoryQuerySet>;
|
2026-07-29 12:57:46 +03:00
|
|
|
|
2026-09-04 11:18:23 +03:00
|
|
|
#[derive(Clone)]
|
|
|
|
|
pub struct HistoryQuerySet;
|
|
|
|
|
impl TableAccessQuerySet<HistoryNote> for HistoryQuerySet {
|
|
|
|
|
fn load() -> &'static str {
|
2026-09-07 10:59:55 +03:00
|
|
|
"SELECT rowid, deck_id, word_id, timestamp, answer, before, after, fsrs FROM history"
|
2026-09-04 11:18:23 +03:00
|
|
|
}
|
2026-07-29 12:57:46 +03:00
|
|
|
|
2026-09-04 11:18:23 +03:00
|
|
|
fn save() -> &'static str {
|
|
|
|
|
todo!("History is readonly")
|
|
|
|
|
}
|
2026-07-29 12:57:46 +03:00
|
|
|
|
2026-09-04 11:18:23 +03:00
|
|
|
fn add() -> &'static str {
|
2026-09-07 10:59:55 +03:00
|
|
|
"INSERT INTO history (deck_id, state_id, timestamp, answer, before, after, fsrs) VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7)"
|
2026-09-04 11:18:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn name() -> &'static str {
|
|
|
|
|
"history"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn from_sql(row: &Row) -> Result<HistoryNote, Error> {
|
|
|
|
|
Ok(HistoryNote {
|
|
|
|
|
id: row.get(0)?,
|
|
|
|
|
timestamp: row.get(3)?,
|
2026-09-07 10:53:09 +03:00
|
|
|
deck_id: row.get(1)?,
|
2026-09-04 11:18:23 +03:00
|
|
|
word_id: row.get(2)?,
|
|
|
|
|
answer: row.get(4)?,
|
|
|
|
|
before: row.get(5)?,
|
|
|
|
|
after: row.get(6)?,
|
|
|
|
|
fsrs: row.get(7)?,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn insert_params(entity: &HistoryNote) -> impl Params {
|
|
|
|
|
(
|
2026-09-07 10:53:09 +03:00
|
|
|
entity.deck_id,
|
2026-09-04 11:18:23 +03:00
|
|
|
entity.word_id,
|
|
|
|
|
entity.timestamp,
|
|
|
|
|
entity.answer,
|
|
|
|
|
entity.before,
|
|
|
|
|
entity.after,
|
|
|
|
|
entity.fsrs,
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn update_params(entity: &HistoryNote) -> impl Params {
|
|
|
|
|
(
|
2026-09-07 10:53:09 +03:00
|
|
|
entity.deck_id,
|
2026-09-04 11:18:23 +03:00
|
|
|
entity.word_id,
|
|
|
|
|
entity.timestamp,
|
|
|
|
|
entity.answer,
|
|
|
|
|
entity.before,
|
|
|
|
|
entity.after,
|
|
|
|
|
entity.fsrs,
|
|
|
|
|
)
|
2026-07-29 12:57:46 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-04 11:18:23 +03:00
|
|
|
impl FromSql for WordOpenMode {
|
|
|
|
|
fn column_result(value: ValueRef<'_>) -> FromSqlResult<Self> {
|
|
|
|
|
if let ValueRef::Integer(s) = value {
|
|
|
|
|
return match s {
|
|
|
|
|
1 => Ok(WordOpenMode::Hard),
|
|
|
|
|
2 => Ok(WordOpenMode::Ok),
|
|
|
|
|
3 => Ok(WordOpenMode::Easy),
|
|
|
|
|
_ => Ok(WordOpenMode::None),
|
2026-08-19 23:37:59 +03:00
|
|
|
}
|
2026-07-29 12:57:46 +03:00
|
|
|
}
|
2026-09-04 11:18:23 +03:00
|
|
|
Ok(WordOpenMode::None)
|
2026-07-29 12:57:46 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-04 11:18:23 +03:00
|
|
|
impl ToSql for WordOpenMode {
|
|
|
|
|
fn to_sql(&self) -> rusqlite::Result<ToSqlOutput<'_>> {
|
|
|
|
|
Ok(ToSqlOutput::from(match self {
|
|
|
|
|
WordOpenMode::Easy => 3,
|
|
|
|
|
WordOpenMode::Ok => 2,
|
|
|
|
|
WordOpenMode::Hard => 1,
|
|
|
|
|
WordOpenMode::None => 0
|
|
|
|
|
}))
|
|
|
|
|
}
|
|
|
|
|
}
|