Skip to main content

sunday_slate/fantasy_teams/
model.rs

1use crate::media::Media;
2use crate::media::MediaId;
3use time::PrimitiveDateTime;
4
5#[derive(Debug, Clone, sqlx::FromRow)]
6pub struct FantasyTeam {
7    pub id: i64,
8    pub league_id: i64,
9    pub user_id: i64,
10    pub name: String,
11    pub owner_name: String,
12    pub logo_media_id: Option<MediaId>,
13    pub is_commissioner: bool,
14    pub created_at: PrimitiveDateTime,
15    pub updated_at: PrimitiveDateTime,
16}
17
18/// A league's fantasy team with its logo joined in — the roster listing,
19/// without the columns only the team's own pages need.
20pub struct LeagueTeam {
21    pub id: FantasyTeamId,
22    pub name: String,
23    pub owner_name: String,
24    pub logo: Option<Media>,
25}
26
27/// A fantasy team's id as an opaque token: typed so it cannot be confused
28/// with other ids, open because the type distinction is the protection.
29#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, sqlx::Type)]
30#[sqlx(transparent)]
31pub struct FantasyTeamId(pub i64);
32
33/// Up to two initials from a fantasy team name, for the logo fallback.
34pub fn monogram(name: &str) -> String {
35    name.split_whitespace()
36        .filter_map(|w| w.chars().next())
37        .take(2)
38        .collect::<String>()
39        .to_uppercase()
40}
41
42#[cfg(test)]
43mod tests {
44    use super::*;
45
46    #[test]
47    fn monogram_takes_up_to_two_initials() {
48        assert_eq!(monogram("Gridiron Giants"), "GG");
49        assert_eq!(monogram("Solo"), "S");
50        assert_eq!(monogram("a b c"), "AB");
51        assert_eq!(monogram(""), "");
52    }
53}