Example #1
0
        public RedirectToActionResult Add(CountryViewModel model)
        {
            model.Country = context.Countries
                            .Include(t => t.Category)
                            .Include(t => t.Game)
                            .Where(t => t.CountryID == model.Country.CountryID)
                            .FirstOrDefault();

            var session   = new OlympicsSession(HttpContext.Session);
            var countries = session.GetMyCountries();

            countries.Add(model.Country);
            session.SetMyCountries(countries);

            var cookies = new OlympicsCookies(Response.Cookies);

            cookies.SetMyCountryIds(countries);

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

            return(RedirectToAction("Index",
                                    new
            {
                ActiveCat = session.GetActiveCat(),
                ActiveGame = session.GetActiveGame()
            }));
        }
        public RedirectToActionResult Delete()
        {
            var session = new OlympicsSession(HttpContext.Session);
            var cookies = new OlympicsCookies(Response.Cookies);

            session.RemoveMyCountries();
            cookies.RemoveMyCountryIds();

            TempData["message"] = "Favorite countries cleared";

            return(RedirectToAction("Index", "Home",
                                    new
            {
                ActiveCat = session.GetActiveCat(),
                ActiveGame = session.GetActiveGame()
            }));
        }
Example #3
0
        public ViewResult Index(CountryListViewModel listModel)
        {
            var session = new OlympicsSession(HttpContext.Session);

            session.SetActiveCat(listModel.ActiveCat);
            session.SetActiveGame(listModel.ActiveGame);

            // if no count in session, get cookie and restore fave countries in session
            int?count = session.GetMyCountryCount();

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

                List <Country> mycountries = new List <Country>();
                if (ids.Length > 0)
                {
                    mycountries = context.Countries.Include(t => t.Category)
                                  .Include(t => t.Game)
                                  .Where(t => ids.Contains(t.CountryID)).ToList();
                }
                session.SetMyCountries(mycountries);
            }

            listModel.Categories = context.Categories.ToList();
            listModel.Games      = context.Games.ToList();

            IQueryable <Country> query = context.Countries;

            if (listModel.ActiveCat != "all")
            {
                query = query.Where(
                    t => t.Category.CategoryID.ToLower() == listModel.ActiveCat.ToLower());
            }
            if (listModel.ActiveGame != "all")
            {
                query = query.Where(
                    t => t.Game.GameID.ToLower() == listModel.ActiveGame.ToLower());
            }
            listModel.Countries = query.ToList();
            return(View(listModel));
        }