public RedirectToActionResult Add(TeamViewModel model)
        {
            model.Team = context.Teams
                         .Include(t => t.Conference)
                         .Include(t => t.Division)
                         .Where(t => t.TeamID == model.Team.TeamID)
                         .FirstOrDefault();

            var session = new NFLSession(HttpContext.Session);
            var teams   = session.GetMyTeams();

            teams.Add(model.Team);
            session.SetMyTeams(teams);

            var cookies = new NFLCookies(HttpContext.Response.Cookies);

            cookies.SetMyTeamIds(teams);

            TempData["message"] = $"{model.Team.Name} added to your favorites";

            return(RedirectToAction("Index",
                                    new {
                ActiveDiv = session.GetActiveDiv(),
                ActiveConf = session.GetActiveConf()
            }));
        }
Beispiel #2
0
        public IActionResult Index(string activeConf = "all", string activeDiv = "all")
        {
            var session = new NFLSession(HttpContext.Session);

            session.SetActiveConf(activeConf);
            session.SetActiveDiv(activeDiv);

            int?count = session.GetMyTeamCount();

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

                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
            {
                ActiveConf  = activeConf,
                ActiveDiv   = activeDiv,
                Conferences = context.Conferences.ToList(),
                Divisions   = context.Divisions.ToList()
            };

            IQueryable <Team> query = context.Teams;

            if (activeConf != "all")
            {
                query = query.Where(t => t.Conference.ConferenceID.ToLower() == activeConf.ToLower());
            }
            if (activeDiv != "all")
            {
                query = query.Where(t => t.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));
        }
        public IActionResult Index(TeamListViewModel model)
        {
            model.Conferences = context.Conferences.ToList();
            model.Divisions   = context.Divisions.ToList();

            var session = new NFLSession(HttpContext.Session);

            session.SetActiveConf(model.ActiveConf);
            session.SetActiveDiv(model.ActiveDiv);

            // if no count value in session, use data in cookie to restore fave teams in session
            int?count = session.GetMyTeamCount();

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

                List <Team> myteams = new List <Team>();
                if (ids.Length > 0)
                {
                    myteams = context.Teams.Include(t => t.Conference)
                              .Include(t => t.Division)
                              .Where(t => ids.Contains(t.TeamID)).ToList();
                }
                session.SetMyTeams(myteams);
            }

            IQueryable <Team> query = context.Teams;

            if (model.ActiveConf != "all")
            {
                query = query.Where(
                    t => t.Conference.ConferenceID.ToLower() == model.ActiveConf.ToLower());
            }
            if (model.ActiveDiv != "all")
            {
                query = query.Where(
                    t => t.Division.DivisionID.ToLower() == model.ActiveDiv.ToLower());
            }
            model.Teams = query.ToList();

            return(View(model));
        }