public async Task <List <PublicEventListItem> > Handle(GetPublicEventsQuery request, CancellationToken cancellationToken) { IQueryable <PublicEvent> publicEvents; CitiesHelper citiesHelper = new CitiesHelper(); if (request.CategoryId != 0 && request.CategoryId != 10) { publicEvents = from p in _context.PublicEvents .Where(p => p.Category.CategoryId == request.CategoryId || p.Category.ParentCategoryId == request.CategoryId) .Where(e => e.Date.Date > DateTime.Now.AddDays(-2).Date) select p; } else { publicEvents = from p in _context.PublicEvents .Where(e => e.Date.Date > DateTime.Now.AddDays(-2).Date) select p; } if (!String.IsNullOrEmpty(request.Date)) { if (request.Date.Length >= 21) { publicEvents = publicEvents.Where(e => e.Date.ToString("dd'.'MM'.'yyyy") == request.Date || (e.Date >= DateTime.Parse(request.Date.Substring(0, 10)) && e.Date <= DateTime.Parse(request.Date.Substring(13, 10)))); } else { publicEvents = publicEvents.Where(e => e.Date.ToString("dd'.'MM'.'yyyy") == request.Date || e.Date.ToString("yyyy-MM-dd") == request.Date); } } if (!String.IsNullOrEmpty(request.City)) { publicEvents = publicEvents.Where(e => citiesHelper.GetCityName(e.Place) == request.City); } switch (request.SortBy) { case "date": publicEvents = publicEvents.OrderBy(e => e.Date); break; case "follows": publicEvents = publicEvents.OrderByDescending(e => e.Follows); break; } var data = publicEvents .Include(p => p.Category) .ThenInclude(c => c.ParentCategory) .Include(te => te.TagEvents) .ThenInclude(t => t.Tag) .ThenInclude(t => t.TagType) .Skip(request.PageSize * request.PageNumber - request.PageSize).Take(request.PageSize).ToList(); var publicEventsList = data.AsQueryable().Select(p => new PublicEventListItem { PublicEventId = p.PublicEventId, Name = p.Name, Description = p.Description, Date = p.Date, CategoryId = p.CategoryId, ImagePath = p.ImagePath, Url = p.Url, Follows = p.Follows, Place = p.Place, City = citiesHelper.GetCityName(p.Place), Promotor = p.Promotor, Price = p.Price, IsFollowed = p.IsFollowed, TagEvents = p.TagEvents, Category = p.Category }).ToList(); if (!string.IsNullOrEmpty(request.UserId)) { var user = _context.Users .Include(f => f.UserFollows).SingleOrDefault(u => u.Id == request.UserId); foreach (var p in publicEventsList) { if (user.UserFollows.Select(e => e.PublicEventId).Contains(p.PublicEventId)) { p.IsFollowed = true; } } } return(publicEventsList); }
public async Task <List <PublicEventListItem> > GetPublicEvents(int?categoryId = 0, string sortBy = "date", string date = "", string city = "", int pageNumber = 1, int pageSize = 25, string userId = "") { IQueryable <PublicEvent> publicEvents; CitiesHelper citiesHelper = new CitiesHelper(); if (categoryId != 0 && categoryId != 10) { publicEvents = from p in _context.PublicEvents .Where(p => p.Category.CategoryId == categoryId || p.Category.ParentCategoryId == categoryId) .Where(e => e.Date.Date > DateTime.Now.AddDays(-2).Date) select p; } else { publicEvents = from p in _context.PublicEvents .Where(e => e.Date.Date > DateTime.Now.AddDays(-2).Date) select p; } if (!String.IsNullOrEmpty(date)) { if (date.Length >= 21) { publicEvents = publicEvents.Where(e => e.Date.ToString("dd'.'MM'.'yyyy") == date || (e.Date >= DateTime.Parse(date.Substring(0, 10)) && e.Date <= DateTime.Parse(date.Substring(13, 10)))); } else { publicEvents = publicEvents.Where(e => e.Date.ToString("dd'.'MM'.'yyyy") == date || e.Date.ToString("yyyy-MM-dd") == date); } } if (!String.IsNullOrEmpty(city)) { publicEvents = publicEvents.Where(e => citiesHelper.GetCityName(e.Place) == city); } switch (sortBy) { case "date": publicEvents = publicEvents.OrderBy(e => e.Date); break; case "follows": publicEvents = publicEvents.OrderByDescending(e => e.Follows); break; } var data = await publicEvents .Include(p => p.Category) .ThenInclude(c => c.ParentCategory) .Include(te => te.TagEvents) .ThenInclude(t => t.Tag) .ThenInclude(t => t.TagType) .Skip(pageSize * pageNumber - pageSize) .Take(pageSize) .ToListAsync(); var publicEventsList = data.Select(p => new PublicEventListItem { PublicEventId = p.PublicEventId, Name = p.Name, Description = p.Description, Date = p.Date, CategoryId = p.CategoryId, ImagePath = p.ImagePath, Url = p.Url, Follows = p.Follows, Place = p.Place, City = citiesHelper.GetCityName(p.Place), Promotor = p.Promotor, Price = p.Price, IsFollowed = p.IsFollowed, TagEvents = p.TagEvents, Category = p.Category }).ToList(); foreach (var pe in publicEventsList) { if (!string.IsNullOrEmpty(userId)) { pe.IsFollowed = await IsEventFollowed(pe.PublicEventId, userId); } else { pe.IsFollowed = false; } } return(publicEventsList); }