Example #1
0
        public IActionResult Index(string activeConf = "all", string activeDiv = "all")
        {
            var session = new NFLSession(HttpContext.Session);

            session.SetActiveConference(activeConf);
            session.SetActiveDivision(activeDiv);

            int?count = session.GetTeamCount();

            if (count == null)
            {
                var      cookies = new NFLCookies(Request.Cookies);
                string[] ids     = cookies.GetTeamsIds();

                List <Team> myTeams = new List <Team>();
                if (ids.Length > 0)
                {
                    myTeams = _ctx.Teams.Include(c => c.Conference)
                              .Include(d => d.Division)
                              .Where(t => ids.Contains(t.TeamID))
                              .ToList();
                }
                //this preserves the state of the cookies even after the browser has been closed...as long as it doesn't exceed 10days
                session.SetTeams(myTeams);
            }

            var data = new TeamListViewModel
            {
                ActiveConference = activeConf,
                ActiveDivision   = activeDiv,
                Conferences      = _ctx.Conferences.ToList(),
                Divisions        = _ctx.Divisions.ToList()
            };

            IEnumerable <Team> query = _ctx.Teams;

            if (activeConf != "all")
            {
                query = query.Where(t => t.Conference.ConferenceID.ToLower() == activeConf.ToLower());
            }

            if (activeDiv != "all")
            {
                query = query.Where(d => d.Division.DivisionID.ToLower() == activeDiv.ToLower());
            }

            data.Teams = query.ToList();

            return(View(data));
        }
        public IActionResult Index(string activeConference = "all", string activeDivision = "all")
        {
            //Save to session
            var session = new NFLSession(HttpContext.Session);

            session.SetActiveConference(activeConference);
            session.SetActiveDivision(activeDivision);

            //Create cookie
            int?teamCount = session.GetMyTeamCount();

            if (teamCount == null)
            {
                var         cookies = new NFLCookies(Request.Cookies);
                string[]    ids     = cookies.GetMyTeamIds();
                List <Team> myTeams = new List <Team>();

                if (ids.Length > 0)
                {
                    myTeams = context.Teams.Include(c => c.Conference).Include(d => d.Division).Where(t => ids.Contains(t.TeamID)).ToList();
                    session.SetMyTeams(myTeams);
                }
            }

            var data = new TeamListViewModel {
                ActiveConference = activeConference,
                ActiveDivision   = activeDivision,
                Conferences      = context.Conferences.ToList(),
                Divsisions       = context.Divisions.ToList()
            };

            IQueryable <Team> query = context.Teams;

            if (activeConference != "all")
            {
                query = query.Where(t => t.Conference.ConferenceID.ToLower() == activeConference.ToLower());
            }

            if (activeDivision != "all")
            {
                query = query.Where(d => d.Division.DivisionID.ToLower() == activeDivision.ToLower());
            }

            data.Teams = query.ToList();

            return(View(data));
        }