Seaching card by name works

This commit is contained in:
2025-08-17 01:08:27 +01:00
parent 9a9f42bc1e
commit 9f03e3e11f
3 changed files with 44 additions and 3 deletions

View File

@@ -77,6 +77,44 @@ pub fn update_db_with_file(file: PathBuf) -> bool {
true
}
#[derive(Debug)]
pub struct DbCard {
pub name: String,
pub type_line: String,
pub oracle_text: Option<String>,
pub power_toughness: Option<String>,
pub loyalty: Option<String>,
pub mana_cost: Option<String>,
pub scryfall_uri: String,
}
pub fn get_card_by_name(name: &str) -> DbCard {
let sqlite_file = get_local_data_sqlite_file();
let conn = rusqlite::Connection::open(sqlite_file).unwrap();
let mut stmt = conn
.prepare(
"SELECT name, type_line, oracle_text, power_toughness, loyalty, mana_cost, scryfall_uri
FROM cards WHERE name = (?1)",
)
.unwrap();
let mut rows = stmt.query([name]).unwrap();
let card = match rows.next().unwrap() {
Some(row) => DbCard {
name: row.get(0).unwrap(),
type_line: row.get(1).unwrap(),
oracle_text: row.get(2).unwrap(),
power_toughness: row.get(3).unwrap(),
loyalty: row.get(4).unwrap(),
mana_cost: row.get(5).unwrap(),
scryfall_uri: row.get(6).unwrap(),
},
None => {
panic!("{} isn't a card that exists", name);
}
};
card
}
const CREATE_CARDS_TABLE_SQL: &str = "
CREATE TABLE cards (
name TEXT NOT NULL UNIQUE,
@@ -84,7 +122,7 @@ CREATE TABLE cards (
type_line TEXT,
oracle_text TEXT,
power_toughness TEXT,
loyalty INTEGER,
loyalty TEXT,
mana_cost TEXT,
scryfall_uri TEXT NOT NULL UNIQUE
)";