public async Task <int> Count(FoodFilter filter) { IQueryable <FoodDAO> Foods = DataContext.Food; Foods = DynamicFilter(Foods, filter); return(await Foods.CountAsync()); }
/// <summary> /// Get all foods that match the given filter, optionally ordered by the specified expression /// </summary> /// <typeparam name="TKey">The property to order by</typeparam> /// <param name="filter">Filter specification object</param> /// <param name="orderBy">Key selector expression to determine the property to order by</param> /// <param name="ascendingOrder">Should we sort in ascendin order?</param> /// <returns>A collection containing all foods that match the filter</returns> public async Task <IEnumerable <Food> > GetFilteredAsync <TKey>(FoodFilter filter, Expression <Func <Food, TKey> > orderBy = null, bool ascendingOrder = true) { var query = _context.Foods.AsQueryable(); if (filter.Name != null) { query = query.Where(food => food.Name.Contains(filter.Name)); } if (filter.FoodGroup != null) { query = query.Where(food => food.FoodGroup == filter.FoodGroup); } if (filter.IsHealthy != null) { query = query.Where(food => food.IsHealthy == filter.IsHealthy); } if (filter.MinRating != 0.0 || filter.MaxRating != 10.0) { query = query.Where(food => food.Rating >= filter.MinRating && food.Rating <= filter.MaxRating); } if (orderBy != null) { query = ascendingOrder ? query.OrderBy(orderBy) : query.OrderByDescending(orderBy); } return(await query.ToListAsync()); }
public ActionResult IndexAjax(FoodFilter f) { var dataquery = db.Foods.ToList(); if (!string.IsNullOrWhiteSpace(f.Name)) { dataquery = dataquery.Where(p => p.Name.Contains(f.Name)).ToList(); } return(PartialView("_IndexTable", dataquery)); }
public async Task <ActionResult <int> > Count([FromBody] Food_FoodFilterDTO Food_FoodFilterDTO) { if (!ModelState.IsValid) { throw new BindException(ModelState); } FoodFilter FoodFilter = ConvertFilterDTOToFilterEntity(Food_FoodFilterDTO); int count = await FoodService.Count(FoodFilter); return(count); }
public async Task <List <Food> > List(FoodFilter filter) { if (filter == null) { return(new List <Food>()); } IQueryable <FoodDAO> FoodDAOs = DataContext.Food; FoodDAOs = DynamicFilter(FoodDAOs, filter); FoodDAOs = DynamicOrder(FoodDAOs, filter); List <Food> Foods = await DynamicSelect(FoodDAOs, filter); return(Foods); }
private async Task <List <Food> > DynamicSelect(IQueryable <FoodDAO> query, FoodFilter filter) { List <Food> Foods = await query.Select(q => new Food() { Id = filter.Selects.Contains(FoodSelect.Id) ? q.Id : default(long), Name = filter.Selects.Contains(FoodSelect.Name) ? q.Name : default(string), PriceEach = filter.Selects.Contains(FoodSelect.PriceEach) ? q.PriceEach : default(decimal), DiscountRate = filter.Selects.Contains(FoodSelect.DiscountRate) ? q.DiscountRate : default(decimal?), Image = filter.Selects.Contains(FoodSelect.Image) ? q.Image : default(string), StatusId = filter.Selects.Contains(FoodSelect.Status) ? q.StatusId : default(long), Descreption = filter.Selects.Contains(FoodSelect.Descreption) ? q.Descreption : default(string), }).AsNoTracking().ToListAsync(); return(Foods); }
public async Task <ActionResult <List <Food_FoodDTO> > > List([FromBody] Food_FoodFilterDTO Food_FoodFilterDTO) { if (!ModelState.IsValid) { throw new BindException(ModelState); } FoodFilter FoodFilter = ConvertFilterDTOToFilterEntity(Food_FoodFilterDTO); List <Food> Foods = await FoodService.List(FoodFilter); List <Food_FoodDTO> Food_FoodDTOs = Foods .Select(c => new Food_FoodDTO(c)).ToList(); return(Food_FoodDTOs); }
private IQueryable <FoodDAO> OrFilter(IQueryable <FoodDAO> query, FoodFilter filter) { if (filter.OrFilter == null || filter.OrFilter.Count == 0) { return(query); } IQueryable <FoodDAO> initQuery = query.Where(q => false); foreach (FoodFilter FoodFilter in filter.OrFilter) { IQueryable <FoodDAO> queryable = query; if (filter.Id != null) { queryable = queryable.Where(q => q.Id, filter.Id); } if (filter.Name != null) { queryable = queryable.Where(q => q.Name, filter.Name); } if (filter.PriceEach != null) { queryable = queryable.Where(q => q.PriceEach, filter.PriceEach); } if (filter.DiscountRate != null) { queryable = queryable.Where(q => q.DiscountRate, filter.DiscountRate); } if (filter.Image != null) { queryable = queryable.Where(q => q.Image, filter.Image); } if (filter.StatusId != null) { queryable = queryable.Where(q => q.StatusId, filter.StatusId); } if (filter.Descreption != null) { queryable = queryable.Where(q => q.Descreption, filter.Descreption); } initQuery = initQuery.Union(queryable); } return(initQuery); }
private FoodFilter ConvertFilterDTOToFilterEntity(Food_FoodFilterDTO Food_FoodFilterDTO) { FoodFilter FoodFilter = new FoodFilter(); FoodFilter.Selects = FoodSelect.ALL; FoodFilter.Skip = Food_FoodFilterDTO.Skip; FoodFilter.Take = Food_FoodFilterDTO.Take; FoodFilter.OrderBy = Food_FoodFilterDTO.OrderBy; FoodFilter.OrderType = Food_FoodFilterDTO.OrderType; FoodFilter.Id = Food_FoodFilterDTO.Id; FoodFilter.Name = Food_FoodFilterDTO.Name; FoodFilter.PriceEach = Food_FoodFilterDTO.PriceEach; FoodFilter.DiscountRate = Food_FoodFilterDTO.DiscountRate; FoodFilter.Image = Food_FoodFilterDTO.Image; FoodFilter.StatusId = Food_FoodFilterDTO.StatusId; FoodFilter.Descreption = Food_FoodFilterDTO.Descreption; return(FoodFilter); }
public async Task <bool> ValidateId(Food Food) { FoodFilter FoodFilter = new FoodFilter { Skip = 0, Take = 10, Id = new IdFilter { Equal = Food.Id }, Selects = FoodSelect.Id }; int count = await UOW.FoodRepository.Count(FoodFilter); if (count == 0) { Food.AddError(nameof(FoodValidator), nameof(Food.Id), ErrorCode.IdNotExisted); } return(count == 1); }
public async Task <List <Food> > List(FoodFilter FoodFilter) { try { List <Food> Foods = await UOW.FoodRepository.List(FoodFilter); return(Foods); } catch (Exception ex) { await Logging.CreateSystemLog(ex.InnerException, nameof(FoodService)); if (ex.InnerException == null) { throw new MessageException(ex); } else { throw new MessageException(ex.InnerException); } } }
public async Task <int> Count(FoodFilter FoodFilter) { try { int result = await UOW.FoodRepository.Count(FoodFilter); return(result); } catch (Exception ex) { await Logging.CreateSystemLog(ex.InnerException, nameof(FoodService)); if (ex.InnerException == null) { throw new MessageException(ex); } else { throw new MessageException(ex.InnerException); } } }
private IQueryable <FoodDAO> DynamicFilter(IQueryable <FoodDAO> query, FoodFilter filter) { if (filter == null) { return(query.Where(q => false)); } query = query.Where(q => !q.DeletedAt.HasValue); if (filter.Id != null) { query = query.Where(q => q.Id, filter.Id); } if (filter.Name != null) { query = query.Where(q => q.Name, filter.Name); } if (filter.PriceEach != null) { query = query.Where(q => q.PriceEach, filter.PriceEach); } if (filter.DiscountRate != null) { query = query.Where(q => q.DiscountRate, filter.DiscountRate); } if (filter.Image != null) { query = query.Where(q => q.Image, filter.Image); } if (filter.StatusId != null) { query = query.Where(q => q.StatusId, filter.StatusId); } if (filter.Descreption != null) { query = query.Where(q => q.Descreption, filter.Descreption); } query = OrFilter(query, filter); return(query); }
private IQueryable <FoodDAO> DynamicOrder(IQueryable <FoodDAO> query, FoodFilter filter) { switch (filter.OrderType) { case OrderType.ASC: switch (filter.OrderBy) { case FoodOrder.Id: query = query.OrderBy(q => q.Id); break; case FoodOrder.Name: query = query.OrderBy(q => q.Name); break; case FoodOrder.PriceEach: query = query.OrderBy(q => q.PriceEach); break; case FoodOrder.DiscountRate: query = query.OrderBy(q => q.DiscountRate); break; case FoodOrder.Image: query = query.OrderBy(q => q.Image); break; case FoodOrder.Status: query = query.OrderBy(q => q.StatusId); break; case FoodOrder.Descreption: query = query.OrderBy(q => q.Descreption); break; } break; case OrderType.DESC: switch (filter.OrderBy) { case FoodOrder.Id: query = query.OrderByDescending(q => q.Id); break; case FoodOrder.Name: query = query.OrderByDescending(q => q.Name); break; case FoodOrder.PriceEach: query = query.OrderByDescending(q => q.PriceEach); break; case FoodOrder.DiscountRate: query = query.OrderByDescending(q => q.DiscountRate); break; case FoodOrder.Image: query = query.OrderByDescending(q => q.Image); break; case FoodOrder.Status: query = query.OrderByDescending(q => q.StatusId); break; case FoodOrder.Descreption: query = query.OrderByDescending(q => q.Descreption); break; } break; } query = query.Skip(filter.Skip).Take(filter.Take); return(query); }