Skip to content

Commit

Permalink
refactor local games store
Browse files Browse the repository at this point in the history
  • Loading branch information
RobDavenport committed Apr 19, 2024
1 parent 7d8b5fe commit a79a7e5
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 19 deletions.
33 changes: 16 additions & 17 deletions gamercade_app/src/local_directory/game.rs
Original file line number Diff line number Diff line change
@@ -1,46 +1,45 @@
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<TagId>,
pub long_description: Option<String>,
pub tags: Vec<i32>,
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) {
db.execute_batch(UPSERT_GAMES_QUERIES).unwrap();
}

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::<Vec<_>>();

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<'_> {
Expand Down
2 changes: 0 additions & 2 deletions gamercade_app/src/local_directory/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,6 @@ impl Default for LocalDirectory {

upsert_games_table(&output.db);

output.refresh_cached_games();

output
}
}

0 comments on commit a79a7e5

Please sign in to comment.