axelclark / ex338

Web application to manage the 338 Challenge fantasy sports league
https://the338challenge.com
MIT License
22 stars 7 forks source link

Add column to track when RosterPosition is deleted #58

Closed axelclark closed 8 years ago

axelclark commented 8 years ago

When a player is dropped or traded, the RosterPosition is updated in a column rather than deleted from the database.

axelclark commented 8 years ago

roster_status: "active", "dropped", "traded" released_at:

"active" is default for any new RosterPosition Waiver claim: "dropped" & update released_at date/time Trade: "traded" & update released_at date/time

Need to update existing queries with where r.roster_status == "active" Need to test the default is working when a player is drafted Need to test existing records get default "active"

axelclark commented 8 years ago

http://www.bennadel.com/blog/1059-grouping-join-clauses-in-sql.htm

axelclark commented 8 years ago
  def right_join_players_by_league(query, fantasy_league_id) do
    from t in query,
    left_join: r in RosterPosition,
    on: r.fantasy_team_id == t.id and t.fantasy_league_id == ^fantasy_league_id,
    right_join: p in assoc(r, :fantasy_player),
    inner_join: s in assoc(p, :sports_league),
    select: %{team_name: t.team_name, player_name: p.player_name,
              league_name: s.league_name},
    order_by: [s.league_name, p.player_name]
  end

  def all_players_with_owners(query, fantasy_league_id) do
    from p in query,
    left_join: 
    fragment(
      (
          @roster_position r
        InnerJoin
          @fantasy_team t
        ON
          r.fantasy_team_id = t.id and t.fantasy_league_id = ?, ^fantasy_league_id
        WHERE
          r.roster_status = ?, "active"
       )
    )
    ON: p.id = r.fantasy_player_id,  
    select: %{team_name: t.team_name, player_name: p.player_name,
              league_name: s.league_name},
    order_by: [s.league_name, p.player_name]
  end