commit reviews to database

This commit is contained in:
Michael Zhang 2023-06-11 15:08:20 -05:00
parent 6c0f35bf01
commit a1907591c6
6 changed files with 78 additions and 20 deletions

View file

@ -38,8 +38,6 @@ pub async fn process_kradfile(
}
_ => continue,
};
println!("kanji: {}, radicals: {:?}", kanji, radicals);
}
Ok(())

View file

@ -31,7 +31,5 @@ async fn main() -> Result<()> {
// Kradfile
kradfile::process_kradfile(&pool, opt.in_dir.join("kradfile.utf8")).await?;
println!("Hello, world!");
Ok(())
}

View file

@ -70,7 +70,6 @@ pub async fn get_kanji(
options: Option<GetKanjiOptions>,
) -> Result<GetKanjiResult, String> {
let opts = options.unwrap_or_default();
println!("opts: {opts:?}");
let looking_for_character_clause = match opts.character {
None => String::new(),
@ -160,8 +159,6 @@ pub async fn get_kanji(
},
);
}
println!("SRS MAP: {srs_info_map:?}");
};
// Put it all together

View file

@ -3,7 +3,7 @@ use std::time::Duration;
use sqlx::{Row, SqlitePool};
use tauri::State;
use crate::utils::Ticks;
use crate::{kanji::KanjiDb, utils::Ticks};
pub struct SrsDb(pub SqlitePool);
@ -78,10 +78,48 @@ pub struct AddSrsItemOptions {
#[tauri::command]
pub async fn add_srs_item(
db: State<'_, SrsDb>,
options: Option<AddSrsItemOptions>,
kanji_db: State<'_, KanjiDb>,
srs_db: State<'_, SrsDb>,
options: AddSrsItemOptions,
) -> Result<(), String> {
let opts = options.unwrap_or_default();
// Fetch meanings
let rows = sqlx::query(
r#"
SELECT Meaning
FROM KanjiMeaningSet
JOIN KanjiSet ON KanjiMeaningSet.Kanji_ID = KanjiSet.ID
WHERE KanjiSet.Character = ?
"#,
)
.bind(&options.character)
.fetch_all(&kanji_db.0)
.await
.map_err(|err| err.to_string())?;
let meanings = rows
.into_iter()
.map(|row| row.get::<String, _>("Meaning").to_owned())
.collect::<Vec<_>>();
let meanings = meanings.join(",");
println!("meanings: {:?}", meanings);
// Fetch readings
let row = sqlx::query(
"SELECT OnYomi, KunYomi, Nanori FROM KanjiSet WHERE Character = ?",
)
.bind(&options.character)
.fetch_one(&kanji_db.0)
.await
.map_err(|err| err.to_string())?;
let onyomi_reading: String = row.get("OnYomi");
let kunyomi_reading: String = row.get("KunYomi");
let nanori_reading: String = row.get("Nanori");
let readings = onyomi_reading
.split(",")
.chain(kunyomi_reading.split(","))
.chain(nanori_reading.split(","))
.collect::<Vec<_>>();
let readings = readings.join(",");
println!("readings: {:?}", readings);
let query_string = format!(
r#"
@ -89,17 +127,22 @@ pub async fn add_srs_item(
(CreationDate, AssociatedKanji, NextAnswerDate,
Meanings, Readings)
VALUES
(?, ?, ?, '', '')
"#
(?, ?, ?, ?, ?)
"#,
);
let utc_now = Ticks::now().map_err(|err| err.to_string())?;
let query = sqlx::query(&query_string)
.bind(&utc_now)
.bind(&opts.character)
.bind(&utc_now);
.bind(&options.character)
.bind(&utc_now)
.bind(&meanings)
.bind(&readings);
query.execute(&db.0).await.map_err(|err| err.to_string())?;
query
.execute(&srs_db.0)
.await
.map_err(|err| err.to_string())?;
Ok(())
}
@ -176,5 +219,26 @@ pub async fn update_srs_item(
item_id: u32,
correct: bool,
) -> Result<(), String> {
todo!()
let (success, failure) = match correct {
true => (1, 0),
false => (0, 1),
};
sqlx::query(
r#"
UPDATE SrsEntrySet
SET
SuccessCount = SuccessCount + ?,
FailureCount = FailureCount + ?
WHERE ID = ?
"#,
)
.bind(success)
.bind(failure)
.bind(item_id)
.execute(&srs_db.0)
.await
.map_err(|err| err.to_string())?;
Ok(())
}

View file

@ -3,10 +3,9 @@ import useSWR from "swr";
import { Box, Flex, Grid, GridItem, LinkBox, Stack } from "@chakra-ui/layout";
import styles from "./KanjiPane.module.scss";
import { Link, useParams } from "react-router-dom";
import { useParams } from "react-router-dom";
import KanjiDisplay from "../components/KanjiDisplay";
import { Kanji } from "../types/Kanji";
import classNames from "classnames";
import { KanjiList } from "../components/KanjiList";
import { useEffect, useState } from "react";

View file

@ -85,7 +85,9 @@ export function Component() {
const isCorrect = possibleAnswers.has(currentAnswer);
// Update the backend
await invoke("update_srs_item", { item_id: nextItem.associatedId, correct: isCorrect });
const params = { itemId: nextItem.associatedId, correct: isCorrect };
const result = await invoke("update_srs_item", params);
console.log("result", result);
// Check the answer
if (!isCorrect) {