Skip to main content

sunday_slate/avatars/
mod.rs

1mod fetch;
2
3use std::collections::HashMap;
4
5use crate::entries::PlayerOrDefenseId;
6use crate::media::Media;
7use crate::nfl_player_headshots::store as player_store;
8use crate::nfl_teams::store as team_store;
9use crate::{AppError, Db};
10
11pub use fetch::{
12    AvatarFetchRunner, FetchProgress, FetchReport, espn_logo_url, fetch_missing, start_fetch,
13};
14
15/// The local headshot or logo for each pick, keyed by what the slot holds.
16pub type Avatars = HashMap<PlayerOrDefenseId, Media>;
17
18/// Load the local media for every given id. Players fetch headshots, defenses
19/// fetch logos; an id with no cached media is absent from the map.
20pub async fn by_id(db: &Db, values: &[PlayerOrDefenseId]) -> Result<Avatars, AppError> {
21    let mut player_ids = Vec::new();
22    let mut teams = Vec::new();
23    for value in values {
24        match value {
25            PlayerOrDefenseId::Player(id) => player_ids.push(id.as_ref()),
26            PlayerOrDefenseId::Defense(team) => teams.push(team.as_str()),
27        }
28    }
29
30    let mut headshots = player_store::headshots_for(db.reader(), &player_ids).await?;
31    let mut logos = team_store::logos_for(db.reader(), &teams).await?;
32
33    let mut avatars = HashMap::new();
34    for value in values {
35        let media = match value {
36            PlayerOrDefenseId::Player(id) => headshots.remove(id.as_ref()),
37            PlayerOrDefenseId::Defense(team) => logos.remove(team.as_str()),
38        };
39        if let Some(media) = media {
40            avatars.insert(value.clone(), media);
41        }
42    }
43    Ok(avatars)
44}