From a79a7e5dce8a8954cc8a9ad1d3529b6ab7058e61 Mon Sep 17 00:00:00 2001 From: RobDavenport Date: Sat, 20 Apr 2024 01:46:08 +0900 Subject: [PATCH] refactor local games store --- gamercade_app/src/local_directory/game.rs | 33 +++++++++++------------ gamercade_app/src/local_directory/mod.rs | 2 -- 2 files changed, 16 insertions(+), 19 deletions(-) diff --git a/gamercade_app/src/local_directory/game.rs b/gamercade_app/src/local_directory/game.rs index 69a2fb66..835514c3 100644 --- a/gamercade_app/src/local_directory/game.rs +++ b/gamercade_app/src/local_directory/game.rs @@ -1,37 +1,28 @@ +use gamercade_interface::game::GameInfoBasic; use rusqlite::Connection; -use super::{tag::TagId, LocalDirectory}; +use super::LocalDirectory; pub struct Game { pub id: i64, pub title: String, pub short_description: String, - pub long_description: String, - pub tags: Vec, + pub long_description: Option, + pub tags: Vec, pub rating: f32, } const UPSERT_GAMES_QUERIES: &str = " -BEGIN; CREATE TABLE IF NOT EXISTS games ( id INTEGER PRIMARY KEY, title TEXT NOT NULL, short_description TEXT NOT NULL, - long_description TEXT NOT NULL, + long_description TEXT, + tags BLOB, rating REAL, file_checksum INTEGER, UNIQUE(title) ) STRICT; - -CREATE TABLE IF NOT EXISTS game_tags( - id INTEGER PRIMARY KEY, - game_id INTEGER NOT NULL, - tag_id INTEGER NOT NULL, - FOREIGN KEY (game_id) REFERENCES games (id), - FOREIGN KEY (tag_id) REFERENCES tags (pid), - UNIQUE(game_id, tag_id) -) STRICT; -COMMIT; "; pub(super) fn upsert_games_table(db: &Connection) { @@ -39,8 +30,16 @@ pub(super) fn upsert_games_table(db: &Connection) { } impl LocalDirectory { - pub fn refresh_cached_games(&self) { - // TODO: Hit the DB to fetch the list of games and populate all of the fields + pub fn update_game(&self, game: GameInfoBasic) { + let tag_bytes = game + .tags + .into_iter() + .map(|tag| u8::try_from(tag).unwrap()) + .collect::>(); + + self.db.execute("INSERT OR REPLACE INTO games (id, title, short_description, file_checksum, rating, tags) + VALUES (?, ?, ?, ?, ?, ?, ?)", + (game.game_id, game.title, game.short_description, game.checksum, game.average_rating, tag_bytes)).unwrap(); } pub fn iter_games(&self) -> GameIter<'_> { diff --git a/gamercade_app/src/local_directory/mod.rs b/gamercade_app/src/local_directory/mod.rs index 5af6979d..f4822592 100644 --- a/gamercade_app/src/local_directory/mod.rs +++ b/gamercade_app/src/local_directory/mod.rs @@ -164,8 +164,6 @@ impl Default for LocalDirectory { upsert_games_table(&output.db); - output.refresh_cached_games(); - output } }