game_manager_lib\services/
gamerpower.rs1use crate::database::AppState;
7use crate::services::cache;
8use crate::utils::http_client::HTTP_CLIENT;
9use serde::{Deserialize, Serialize};
10use tauri::{AppHandle, Manager};
11
12#[derive(Debug, Serialize, Deserialize, Clone)]
13pub struct Giveaway {
14 pub id: u32,
15 pub title: String,
16 pub worth: String, pub thumbnail: String, pub image: String, pub description: String,
20 pub instructions: String,
21 #[serde(rename = "open_giveaway_url")]
22 pub open_giveaway_url: String,
23 #[serde(rename = "published_date")]
24 pub published_date: String,
25 #[serde(rename = "type")]
26 pub type_prop: String, pub platforms: String, #[serde(rename = "end_date")]
29 pub end_date: Option<String>,
30 pub status: String, }
32
33pub async fn fetch_giveaways(app: &AppHandle) -> Result<Vec<Giveaway>, String> {
35 let cache_key = "gamerpower_list_active";
36 let url = "https://www.gamerpower.com/api/giveaways?platform=pc&type=game&sort-by=popularity";
37
38 match HTTP_CLIENT.get(url).send().await {
40 Ok(res) => {
41 if res.status().is_success() {
42 let giveaways: Vec<Giveaway> = res.json().await.map_err(|e| e.to_string())?;
43
44 let active_ones: Vec<Giveaway> = giveaways
46 .into_iter()
47 .filter(|g| g.status == "Active")
48 .collect();
49
50 if let Ok(conn) = app.state::<AppState>().metadata_db.lock() {
52 if let Ok(json) = serde_json::to_string(&active_ones) {
53 let _ = cache::save_cached_api_data(&conn, "gamerpower", cache_key, &json);
55 }
56 }
57
58 return Ok(active_ones);
59 }
60 }
61 Err(_) => {} }
63
64 if let Ok(conn) = app.state::<AppState>().metadata_db.lock() {
66 if let Some(payload) = cache::get_stale_api_data(&conn, "gamerpower", cache_key) {
67 if let Ok(cached_giveaways) = serde_json::from_str::<Vec<Giveaway>>(&payload) {
68 return Ok(cached_giveaways);
69 }
70 }
71 }
72
73 Err("Não foi possível carregar jogos grátis (sem conexão e sem cache).".to_string())
74}