Skip to main content

game_manager_lib/commands/
debug.rs

1use crate::database;
2use crate::database::AppState;
3use crate::errors::AppError;
4use rusqlite::params;
5use tauri::State;
6use tauri::{AppHandle, Manager};
7
8/// DEBUG — apenas para testes manuais via devtools.
9/// Busca o catálogo do Nexus Mods e popula a tabela `nexus_games` do cache,
10/// sem passar pelo fluxo de enriquecimento. Remover depois dos testes.
11#[tauri::command]
12pub async fn debug_populate_nexus_cache(app: AppHandle) -> Result<String, AppError> {
13    let api_key = database::get_secret(&app, "nexus_api_key")?;
14    if api_key.is_empty() {
15        return Err(AppError::ValidationError(
16            "Nexus API Key não configurada.".to_string(),
17        ));
18    }
19
20    let games = crate::services::integration::nexus::fetch_nexus_games(&api_key)
21        .await
22        .map_err(|e| AppError::ValidationError(format!("Erro ao buscar dados do Nexus: {}", e)))?;
23
24    let count = games.len();
25
26    let state: tauri::State<database::AppState> = app.state();
27    let cache_conn = state
28        .cache_db
29        .lock()
30        .map_err(|_| AppError::ValidationError("Falha ao travar cache_db".to_string()))?;
31
32    crate::services::cache::save_nexus_games_cache(&cache_conn, &games)
33        .map_err(|e| AppError::ValidationError(format!("Erro ao salvar cache: {}", e)))?;
34
35    Ok(format!("{} jogos do Nexus salvos no cache local.", count))
36}
37
38/// Comando temporário: popula `slug` para jogos já existentes no banco
39/// (antes da coluna existir ou antes de rodar o slugify no import).
40/// Chamar uma vez pela devtools e remover depois.
41#[tauri::command]
42pub fn backfill_slug_names(state: State<AppState>) -> Result<String, AppError> {
43    let conn = state.games_db.lock().map_err(|_| AppError::MutexError)?;
44
45    let mut stmt = conn
46        .prepare("SELECT id, name FROM games WHERE slug = ''")
47        .map_err(|e| AppError::DatabaseError(e.to_string()))?;
48
49    let rows: Vec<(String, String)> = stmt
50        .query_map([], |row| Ok((row.get(0)?, row.get(1)?)))
51        .map_err(|e| AppError::DatabaseError(e.to_string()))?
52        .filter_map(Result::ok)
53        .collect();
54
55    let count = rows.len();
56
57    for (id, name) in rows {
58        let slug = crate::utils::text::slugify(&name);
59        conn.execute(
60            "UPDATE games SET slug = ?1 WHERE id = ?2",
61            params![slug, id],
62        )
63            .map_err(|e| AppError::DatabaseError(e.to_string()))?;
64    }
65
66    Ok(format!("{} jogos atualizados com slug", count))
67}