Ejemplo n.º 1
0
        public async Task <IActionResult> IndexLoggedIn(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 userId = this.userManager.GetUserId(this.User);

            var viewModel = new HomeEventsListViewModel
            {
                Events = await this.eventsService.GetNotParticipatingInCityAsync <EventCardPartialViewModel>(
                    countryId, userId, cityId, ResultsPerPageCount, (page - 1) *ResultsPerPageCount),
                Filter = new FilterBarPartialViewModel
                {
                    Cities = await this.citiesService.GetAllWithEventsInCountryAsync(countryId),
                    Sports = await this.sportsService.GetAllInCountryByIdAsync(countryId),
                    From   = DateTime.UtcNow,
                    To     = DateTime.UtcNow.AddMonths(6),
                },
                Location = location.City + ", " + location.Country,
            };

            var count = await this.eventsService.GetNotParticipatingCount(userId, cityId);

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

            return(this.View(viewModel));
        }
Ejemplo n.º 2
0
        public async Task <HomeEventsListViewModel> FilterAsync(
            int?cityId, int?sportId, DateTime from, DateTime to, int countryId, string userId, int?take = null, int skip = 0)
        {
            await this.SetPassedStatusAsync(countryId);

            var query = this.GetActiveEventsInCountryInPeriodOfTheYearAsIQuerable(countryId, from, to);

            if (userId != null)
            {
                query = query.Where(e => !e.Users.Any(u => u.UserId == userId));
            }

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

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

            var resultCount = await query.CountAsync();

            IEnumerable <SelectListItem> sports;

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

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

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

            var viewModel = new HomeEventsListViewModel
            {
                Events      = await query.To <EventCardPartialViewModel>().ToListAsync(),
                CityId      = cityId,
                SportId     = sportId,
                From        = from,
                To          = to,
                ResultCount = resultCount,
                Filter      = new FilterBarPartialViewModel
                {
                    Cities  = await this.citiesService.GetAllWithEventsInCountryAsync(countryId),
                    Sports  = sports,
                    From    = from,
                    To      = to,
                    SportId = sportId,
                    CityId  = cityId,
                },
            };

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

            viewModel.Location = cityId != null
                ? await this.citiesService.GetNameByIdAsync(cityId.Value) + ", " + countryName
                : $"{countryName}";

            return(viewModel);
        }