game_manager_lib\commands/
plataforms.rs1use crate::constants::{self};
9use crate::database::AppState;
10use crate::errors::AppError;
11use crate::services::steam;
12use crate::utils::status_logic;
13use chrono::{TimeZone, Utc};
14use rusqlite::params;
15use tauri::State;
16use tracing::info;
17use uuid::Uuid;
18
19#[tauri::command]
38pub async fn import_steam_library(
39 state: State<'_, AppState>,
40 api_key: String,
41 steam_id: String,
42) -> Result<String, AppError> {
43 let steam_games = steam::list_steam_games(&api_key, &steam_id)
45 .await
46 .map_err(AppError::NetworkError)?;
47
48 if steam_games.is_empty() {
49 return Ok("Nenhum jogo encontrado.".to_string());
50 }
51
52 let mut inserted = 0;
53 let mut updated = 0;
54 let now = Utc::now().to_rfc3339();
55
56 let conn = state.library_db.lock()?;
57
58 for game in steam_games {
59 let exists: bool = conn
60 .query_row(
61 "SELECT EXISTS(SELECT 1 FROM games WHERE platform = 'Steam' AND platform_id = ?1)",
62 params![game.appid],
63 |row| row.get(0),
64 )
65 .unwrap_or(false);
66
67 let status = status_logic::calculate_status(game.playtime_forever);
68
69 let last_played_iso = if game.rtime_last_played > 0 {
71 Some(
72 Utc.timestamp_opt(game.rtime_last_played, 0)
73 .unwrap()
74 .to_rfc3339(),
75 )
76 } else {
77 None
78 };
79
80 if !exists {
82 let new_id = Uuid::new_v4().to_string();
83 let cover = format!(
84 "{}/steam/apps/{}/library_600x900.jpg",
85 constants::STEAM_CDN_URL,
86 game.appid
87 );
88
89 conn.execute(
90 "INSERT INTO games (
91 id, name, cover_url, platform, platform_id,
92 status, playtime, last_played, added_at, favorite, user_rating
93 ) VALUES (?1, ?2, ?3, 'Steam', ?4, ?5, ?6, ?7, ?8, 0, NULL)",
94 params![
95 new_id,
96 game.name,
97 cover,
98 game.appid,
99 status,
100 game.playtime_forever,
101 last_played_iso,
102 now
103 ],
104 )
105 .ok();
106 inserted += 1;
107 } else {
108 conn.execute(
109 "UPDATE games SET
110 playtime = ?1,
111 status = ?2,
112 last_played = COALESCE(?3, last_played)
113 WHERE platform = 'Steam' AND platform_id = ?4",
114 params![game.playtime_forever, status, last_played_iso, game.appid],
115 )
116 .ok();
117 updated += 1;
118 }
119 }
120
121 let message = format!("{} novos, {} atualizados", inserted, updated);
122 info!("{}", message);
123
124 Ok(message)
125}