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 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 = " const CREATE_CARDS_TABLE_SQL: &str = "
CREATE TABLE cards ( CREATE TABLE cards (
name TEXT NOT NULL UNIQUE, name TEXT NOT NULL UNIQUE,
@@ -84,7 +122,7 @@ CREATE TABLE cards (
type_line TEXT, type_line TEXT,
oracle_text TEXT, oracle_text TEXT,
power_toughness TEXT, power_toughness TEXT,
loyalty INTEGER, loyalty TEXT,
mana_cost TEXT, mana_cost TEXT,
scryfall_uri TEXT NOT NULL UNIQUE scryfall_uri TEXT NOT NULL UNIQUE
)"; )";

View File

@@ -5,7 +5,7 @@ mod deser;
pub use crate::deser::ScryfallCard; pub use crate::deser::ScryfallCard;
mod db; mod db;
pub use db::{get_all_lowercase_card_names, init_db, update_db_with_file}; pub use db::{get_all_lowercase_card_names, get_card_by_name, init_db, update_db_with_file};
mod utils; mod utils;
pub use utils::get_local_cache_folder; pub use utils::get_local_cache_folder;

View File

@@ -1,5 +1,6 @@
use clap::Parser; use clap::Parser;
use scryfall_deser::get_all_lowercase_card_names; use scryfall_deser::get_all_lowercase_card_names;
use scryfall_deser::get_card_by_name;
use scryfall_deser::get_local_cache_folder; use scryfall_deser::get_local_cache_folder;
use scryfall_deser::init_db; use scryfall_deser::init_db;
use scryfall_deser::update_db_with_file; use scryfall_deser::update_db_with_file;
@@ -18,7 +19,7 @@ fn main() {
if args.update { if args.update {
init_db(); init_db();
let mut path = get_local_cache_folder(); let mut path = get_local_cache_folder();
// TODO - actually download and update // TODO - actually download the file - probably do away with TempFile stuff
path.push("oracle-cards-20250814210711.json"); path.push("oracle-cards-20250814210711.json");
update_db_with_file(path); update_db_with_file(path);
return; return;
@@ -41,4 +42,6 @@ fn main() {
// Do some distance checking stuff // Do some distance checking stuff
} }
//dbg!(matching_cards); //dbg!(matching_cards);
let card = get_card_by_name("Black Lotus");
dbg!(card);
} }