Skip to main content

sunday_slate/standings/
service.rs

1use crate::contests::service as contests_service;
2use crate::entries::store as entries;
3use crate::scoring::service as scoring_service;
4use crate::standings::standings::{self, ScoredFantasyTeam, StandingRow};
5use crate::{AppError, AppState};
6
7pub async fn scored_teams(
8    state: &AppState,
9    league_id: i64,
10) -> Result<Vec<ScoredFantasyTeam>, AppError> {
11    let results = contests_service::official_contest_weeks(state).await?;
12    let teams = entries::by_fantasy_team(state.db.reader(), league_id).await?;
13
14    Ok(teams
15        .into_iter()
16        .map(|t| ScoredFantasyTeam {
17            id: t.id,
18            name: t.name,
19            owner_name: t.owner_name,
20            logo: t.logo.map(|m| m.url()),
21            entered: t.entries.iter().map(|e| e.slate_id).collect(),
22            scores: scoring_service::score_entries(&t.entries, &results.snapshot, &results.slates),
23        })
24        .collect())
25}
26
27/// One league's ranked standings.
28pub async fn standings_for_league(
29    state: &AppState,
30    league_id: i64,
31) -> Result<Vec<StandingRow>, AppError> {
32    Ok(standings::rank(scored_teams(state, league_id).await?))
33}