aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMiquel Sabaté Solà <mssola@mssola.com>2026-01-14 22:36:54 +0100
committerMiquel Sabaté Solà <mssola@mssola.com>2026-01-14 22:39:40 +0100
commit0c89256c782f0d1e98e93047fa90ad90b4e003e3 (patch)
tree67165aa26061867ac1c3af278a0d98be40671ace
parent9bc834bcb6e694a0ce065fc8c638c699b62688c5 (diff)
downloadmihi-0c89256c782f0d1e98e93047fa90ad90b4e003e3.tar.gz
mihi-0c89256c782f0d1e98e93047fa90ad90b4e003e3.zip
Touch the 'updated_at' column on exercise success
If the user runs into an exercise and is satisfied with the results, then touch the 'updated_at' column for that exercise. This is relevant because in practice exercises are selected by the lastly updated exercise. Signed-off-by: Miquel Sabaté Solà <mssola@mssola.com>
-rw-r--r--crates/cli/src/run.rs10
-rw-r--r--lib/mihi/src/lib.rs19
2 files changed, 26 insertions, 3 deletions
diff --git a/crates/cli/src/run.rs b/crates/cli/src/run.rs
index 744fe6f..bbfa6bc 100644
--- a/crates/cli/src/run.rs
+++ b/crates/cli/src/run.rs
@@ -1,4 +1,5 @@
use inquire::{Confirm, Editor, Text};
+use mihi::touch_exercise;
use mihi::{select_relevant_words, update_success, Category, Exercise, ExerciseKind, Word};
use std::env;
use std::fs;
@@ -124,7 +125,7 @@ fn diff_tool() -> Option<&'static str> {
// and interactively ask the user if things are ok. Returns a boolean depending
// on the user's answer to that final question, or false if something went
// wrong.
-fn accepted_diff(given: String, expected: String) -> bool {
+fn accepted_diff(given: String, expected: &String) -> bool {
// If a diff tool could be fetched, then write into temporary files and call
// the diff tool against both temporary files; otherwise just print things
// out into the stdout.
@@ -192,8 +193,11 @@ fn run_exercises(exercises: Vec<Exercise>) -> bool {
exercise.title, exercise.enunciate
);
- if !accepted_diff(solution, exercise.solution) {
- // TODO: update_{success,failure}
+ // If the exercise is seen as correct by the user, then "touch"
+ // (i.e. refresh the 'updated_at' date). This way, next time we select
+ // exercises to show the user, we can prevent this one showing up first.
+ if accepted_diff(solution, &exercise.solution) {
+ let _ = touch_exercise(exercise);
}
}
diff --git a/lib/mihi/src/lib.rs b/lib/mihi/src/lib.rs
index 2915368..4d337b9 100644
--- a/lib/mihi/src/lib.rs
+++ b/lib/mihi/src/lib.rs
@@ -856,6 +856,25 @@ pub fn update_exercise(exercise: Exercise) -> Result<(), String> {
}
}
+/// Updates the 'updated_at' column for an exercise.
+pub fn touch_exercise(exercise: Exercise) -> Result<(), String> {
+ if exercise.id == 0 {
+ return Err("invalid exercise to update; seems it has not been created before".to_string());
+ }
+
+ let conn = get_connection()?;
+
+ match conn.execute(
+ "UPDATE exercises \
+ SET updated_at = datetime('now') \
+ WHERE id = ?1",
+ params![exercise.id],
+ ) {
+ Ok(_) => Ok(()),
+ Err(e) => Err(format!("could not update '{}': {}", exercise.title, e)),
+ }
+}
+
/// Delete an exercise from the database.
pub fn delete_exercise(title: &str) -> Result<(), String> {
let conn = get_connection()?;