Пример #1
0
        public IActionResult Index(string category, string filter)
        {
            var    vm           = new TradeIndexViewModel();
            string city         = ((string.Compare(filter, "Mine", true) == 0) ? GetCity() : string.Empty);
            bool   filter16Plus = (string.Compare(filter, "16Plus", true) == 0);
            bool   filter18Plus = (string.Compare(filter, "18Plus", true) == 0);
            bool   filter21Plus = (string.Compare(filter, "21Plus", true) == 0);

            vm.Trades = _tradeService.GetTrades(city, category, filter16Plus, filter18Plus, filter21Plus);

            if (DateTime.Now.Second >= 45)
            {
                throw new System.Exception("kaboom");
            }
            if (DateTime.Now.Second > 10 && DateTime.Now.Second < 15)
            {
                System.Threading.Thread.Sleep(new TimeSpan(0, 0, 13));
            }

            return(View(vm));
        }
Пример #2
0
        //An Index view for our TradeRequest resource. Will show all incoming and outgoing TradeRequests, as well as allow users to search for CaughtPokemon to create a TradeRequest for
        public async Task <IActionResult> Index(string searchString)
        {
            TradeIndexViewModel model = new TradeIndexViewModel();
            //Gets the currently logged in user
            ApplicationUser currentUser = await GetCurrentUserAsync();

            //Sets a variable in our ViewBag to the search string that was passed in as a parameter
            ViewBag.SearchString = searchString;

            //Loads the 3 most recent TradeRequests sent TO the current user
            model.IncomingRequests = await _context.TradeRequest
                                     .Include(tr => tr.DesiredPokemon)
                                     .ThenInclude(tr => tr.User)
                                     .Include(tr => tr.DesiredPokemon)
                                     .ThenInclude(tr => tr.Gender)
                                     .Include(tr => tr.DesiredPokemon)
                                     .ThenInclude(tr => tr.Pokemon)
                                     .Include(tr => tr.OfferedPokemon)
                                     .ThenInclude(tr => tr.User)
                                     .Include(tr => tr.OfferedPokemon)
                                     .ThenInclude(tr => tr.Gender)
                                     .Include(tr => tr.OfferedPokemon)
                                     .ThenInclude(tr => tr.Pokemon)
                                     .Take(3)
                                     .Where(tr => tr.DesiredPokemon.UserId == currentUser.Id)
                                     .OrderByDescending(tr => tr.DateSent)
                                     .ToListAsync();

            //Loads the 3 most recent TradeRequests sent BY the current user
            model.OutgoingRequests = await _context.TradeRequest
                                     .Include(tr => tr.DesiredPokemon)
                                     .ThenInclude(tr => tr.User)
                                     .Include(tr => tr.DesiredPokemon)
                                     .ThenInclude(tr => tr.Gender)
                                     .Include(tr => tr.DesiredPokemon)
                                     .ThenInclude(tr => tr.Pokemon)
                                     .Include(tr => tr.OfferedPokemon)
                                     .ThenInclude(tr => tr.User)
                                     .Include(tr => tr.OfferedPokemon)
                                     .ThenInclude(tr => tr.Gender)
                                     .Include(tr => tr.OfferedPokemon)
                                     .ThenInclude(tr => tr.Pokemon)
                                     .Take(3)
                                     .Where(tr => tr.OfferedPokemon.UserId == currentUser.Id)
                                     .OrderByDescending(tr => tr.DateSent)
                                     .ToListAsync();

            //Loads all CaughtPokemon that are open for trade
            model.SearchResults = await _context.CaughtPokemon
                                  .Include(cp => cp.User)
                                  .Include(cp => cp.Pokemon)
                                  .Include(cp => cp.Gender)
                                  .Where(cp => cp.isTradeOpen == true)
                                  .Where(cp => cp.isHidden == false)
                                  .Where(cp => cp.isOwned == true)
                                  .Where(cp => cp.UserId != currentUser.Id)
                                  .OrderByDescending(cp => cp.DateCaught)
                                  .ToListAsync();

            if (searchString != null && searchString != "")
            {
                //If a search string parameter was passed in, we'll filter our results to only have CaughtPokemon whose nickname or species name contain that search query
                model.SearchResults = model.SearchResults
                                      .Where(cp => (cp.Nickname ?? "").Contains(searchString, StringComparison.OrdinalIgnoreCase) ||
                                             cp.Pokemon.Name.Contains(searchString, StringComparison.OrdinalIgnoreCase))
                                      .ToList();
            }
            else
            {
                //If there's no search string parameter, we'll just show the 10 most recently caught Pokemon that are open for trade
                model.SearchResults = model.SearchResults
                                      .Take(10)
                                      .ToList();
            }

            //Gets a list of all the Pokemon the current user has caught (and have not been soft-deleted)
            List <CaughtPokemon> listOfUserCaughtPokemon = await _context.CaughtPokemon
                                                           .Where(cp => cp.UserId == currentUser.Id)
                                                           .Where(cp => cp.isOwned == true)
                                                           .Where(cp => cp.isHidden == false)
                                                           .ToListAsync();

            //Loops through the list of Pokemon the user owns, so that we can get their IDs. Pokemon whose IDs are listed here will be highlighted as "caught" when viewing search results
            foreach (CaughtPokemon caughtPokemon in listOfUserCaughtPokemon)
            {
                //If this list does not already contain the species of Pokemon we're currently looking at...
                if (!model.CurrentUserCollection.Contains(caughtPokemon.PokemonId))
                {
                    //...add it to the list of IDs
                    model.CurrentUserCollection.Add(caughtPokemon.PokemonId);
                }
            }

            //Sends our completed ViewModel to our view
            return(View(model));
        }