Example #1
0
        public async Task <IActionResult> Index(int page = 1)
        {
            var location  = this.GetLocation();
            var countryId = await this.countriesService.GetIdAsync(location.Country);

            var cityId = await this.citiesService.GetIdAsync(location.City, countryId);

            var viewModel = new ArenaIndexListViewModel
            {
                Location = $"{location.City}, {location.Country}",
                Arenas   = await this.arenasService.GetAllInCityAsync(cityId, ResultsPerPageCount, (page - 1) *ResultsPerPageCount),
                Filter   = new FilterBarArenasPartialViewModel
                {
                    Cities = await this.citiesService.GetAllWithArenasInCountryAsync(countryId),
                    Sports = await this.sportsService.GetAllInCountryByIdAsync(countryId),
                },
            };

            var count = await this.arenasService.GetCountInCityAsync(cityId);

            viewModel.CurrentPage = page;
            viewModel.PageCount   = (int)Math.Ceiling((double)count / ResultsPerPageCount) != 0
                ? (int)Math.Ceiling((double)count / ResultsPerPageCount) : 1;

            return(this.View(viewModel));
        }
Example #2
0
        public async Task <ArenaIndexListViewModel> FilterAsync(int countryId, int?sportId, int?cityId, int?take = null, int skip = 0)
        {
            var query = this.GetAllActiveInCountryAsIQueryable <ArenaCardPartialViewModel>(countryId);

            if (sportId != null)
            {
                query = query.Where(a => a.SportId == sportId);
            }

            if (cityId != null)
            {
                query = query.Where(a => a.CityId == cityId);
            }

            var resultCount = query.Count();
            IEnumerable <SelectListItem> sports;

            if (cityId == null || resultCount == 0)
            {
                sports = await this.sportsService.GetAllInCountryByIdAsync(countryId);
            }
            else
            {
                sports = query
                         .Select(a => new SelectListItem
                {
                    Text  = a.SportName,
                    Value = a.SportId.ToString(),
                })
                         .Distinct();
            }

            if (skip > 0)
            {
                query = query.Skip(skip);
            }

            if (take.HasValue && resultCount > take)
            {
                query = query.Take(take.Value);
            }

            var viewModel = new ArenaIndexListViewModel
            {
                Arenas      = query.ToList(),
                ResultCount = resultCount,
                CityId      = cityId,
                SportId     = sportId,
                Filter      = new FilterBarArenasPartialViewModel
                {
                    Cities = await this.citiesService.GetAllWithArenasInCountryAsync(countryId),
                    Sports = sports,
                },
            };

            var countryName = await this.countriesService.GetNameByIdAsync(countryId);

            viewModel.Location = cityId.HasValue
              ? await this.citiesService.GetNameByIdAsync(cityId.Value) + ", " + countryName
              : countryName;

            foreach (var model in viewModel.Arenas)
            {
                model.MainImageUrl = this.SetMainImage(model.MainImageUrl);
            }

            return(viewModel);
        }