the ultimate cat database
cargo devel CI / cargo CI (push) Successful in 2m31s Details

This commit is contained in:
Christoph J. Scherr 2024-06-25 15:27:27 +02:00
parent 5e04bb7578
commit 318149bf48
1 changed files with 34 additions and 3 deletions

View File

@ -198,12 +198,39 @@ fn interactive_add_cat(conn: &Connection) -> anyhow::Result<usize> {
let cat_id = new_cat(conn, &cat_name, get_color_id(conn, &cat_color)?)?; let cat_id = new_cat(conn, &cat_name, get_color_id(conn, &cat_color)?)?;
assert!(check_if_cat_exists(conn, cat_id)?); assert!(check_if_cat_exists(conn, cat_id)?);
println!("your cat has id {cat_id}"); println!("your cat has id {cat_id}");
// FIXME: some sql error here: `Error: Query returned no rows`
println!("your cat has color '{}'", get_cat_color(conn, cat_id)?); println!("your cat has color '{}'", get_cat_color(conn, cat_id)?);
Ok(cat_id) Ok(cat_id)
} }
fn interactive_find_cat(conn: &Connection) -> anyhow::Result<()> {
let stdin = io::stdin();
print!("the name of your cat?\n> ");
io::stdout().flush()?;
let mut cat_name: String = String::new();
let _ = stdin.read_line(&mut cat_name)?;
cat_name = cat_name.trim().to_string();
print!("the color of your cat?\n> ");
io::stdout().flush()?;
let mut cat_color: String = String::new();
let _ = stdin.read_line(&mut cat_color)?;
cat_color = cat_color.trim().to_string();
new_color(conn, &cat_color)?;
let mut stmt = conn.prepare(&format!(
"SELECT * FROM {TABLE_CAT} c, {TABLE_CAT_COLOR} cc
WHERE c.color_id = cc.id
AND (c.name LIKE (?1) OR cc.name LIKE (?2))"
))?;
let mut fitting_cats = stmt.query([cat_name,cat_color])?;
println!("These cats might fit your description:\n");
print_cats(conn, &mut fitting_cats)?;
Ok(())
}
fn print_colors(_conn: &Connection, colors: &mut Rows) -> anyhow::Result<()> { fn print_colors(_conn: &Connection, colors: &mut Rows) -> anyhow::Result<()> {
println!("{: <14}| {: <19}", "id", "name"); println!("{: <14}| {: <19}", "id", "name");
println!("{:=^80}", ""); println!("{:=^80}", "");
@ -262,12 +289,13 @@ fn main() -> anyhow::Result<()> {
io::stdout().flush()?; io::stdout().flush()?;
let _ = stdin.lock().read_line(&mut buf); let _ = stdin.lock().read_line(&mut buf);
buf = buf.trim().to_string(); buf = buf.trim().to_string();
buf = buf.to_uppercase().to_string();
match buf.as_str() { match buf.as_str() {
"A" => { "A" => {
interactive_add_cat(&conn)?; interactive_add_cat(&conn)?;
} }
"F" => { "F" => {
println!("currently not implemented"); interactive_find_cat(&conn)?;
} }
"P" => { "P" => {
print_all_data(&conn)?; print_all_data(&conn)?;
@ -278,8 +306,11 @@ fn main() -> anyhow::Result<()> {
} }
_ => (), _ => (),
} }
println!("\n(Enter to continue)");
let _ = stdin.lock().read_line(&mut buf); // wait for enter as confirmation
buf.clear(); buf.clear();
println!(); print!("{}[2J", 27 as char); // clear terminal
io::stdout().flush()?;
} }
Ok(()) Ok(())