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, _ => continue,
}; };
println!("kanji: {}, radicals: {:?}", kanji, radicals);
} }
Ok(()) Ok(())

View file

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

View file

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

View file

@ -3,7 +3,7 @@ use std::time::Duration;
use sqlx::{Row, SqlitePool}; use sqlx::{Row, SqlitePool};
use tauri::State; use tauri::State;
use crate::utils::Ticks; use crate::{kanji::KanjiDb, utils::Ticks};
pub struct SrsDb(pub SqlitePool); pub struct SrsDb(pub SqlitePool);
@ -78,10 +78,48 @@ pub struct AddSrsItemOptions {
#[tauri::command] #[tauri::command]
pub async fn add_srs_item( pub async fn add_srs_item(
db: State<'_, SrsDb>, kanji_db: State<'_, KanjiDb>,
options: Option<AddSrsItemOptions>, srs_db: State<'_, SrsDb>,
options: AddSrsItemOptions,
) -> Result<(), String> { ) -> 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!( let query_string = format!(
r#" r#"
@ -89,17 +127,22 @@ pub async fn add_srs_item(
(CreationDate, AssociatedKanji, NextAnswerDate, (CreationDate, AssociatedKanji, NextAnswerDate,
Meanings, Readings) Meanings, Readings)
VALUES VALUES
(?, ?, ?, '', '') (?, ?, ?, ?, ?)
"# "#,
); );
let utc_now = Ticks::now().map_err(|err| err.to_string())?; let utc_now = Ticks::now().map_err(|err| err.to_string())?;
let query = sqlx::query(&query_string) let query = sqlx::query(&query_string)
.bind(&utc_now) .bind(&utc_now)
.bind(&opts.character) .bind(&options.character)
.bind(&utc_now); .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(()) Ok(())
} }
@ -176,5 +219,26 @@ pub async fn update_srs_item(
item_id: u32, item_id: u32,
correct: bool, correct: bool,
) -> Result<(), String> { ) -> 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 { Box, Flex, Grid, GridItem, LinkBox, Stack } from "@chakra-ui/layout";
import styles from "./KanjiPane.module.scss"; 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 KanjiDisplay from "../components/KanjiDisplay";
import { Kanji } from "../types/Kanji"; import { Kanji } from "../types/Kanji";
import classNames from "classnames";
import { KanjiList } from "../components/KanjiList"; import { KanjiList } from "../components/KanjiList";
import { useEffect, useState } from "react"; import { useEffect, useState } from "react";

View file

@ -85,7 +85,9 @@ export function Component() {
const isCorrect = possibleAnswers.has(currentAnswer); const isCorrect = possibleAnswers.has(currentAnswer);
// Update the backend // 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 // Check the answer
if (!isCorrect) { if (!isCorrect) {