Example #1
0
        public async Task <GridData <CategoryDTO> > Get(GridData <CategoryDTO> queryParams)
        {
            var query = _context.Categories as IQueryable <Category>;
            int count;

            if (queryParams.Filters != null)
            {
                foreach (var filter in queryParams.Filters)
                {
                    if (filter.Key == "Name")
                    {
                        query = query.Where(c => c.Name.Contains(filter.Value));
                    }
                }
            }

            count = await query.CountAsync();

            query = query.OrderBy(queryParams.SortBy + (queryParams.SortType == SortType.Asc ? " asc" : " desc"));

            query = query.Skip((queryParams.PageNumber - 1) * queryParams.PageSize).Take(queryParams.PageSize);

            return(new GridData <CategoryDTO>
            {
                Data = await query.Select(c => DTOConvert.CategoryModelToDTO(c)).ToListAsync(),
                Filters = queryParams.Filters,
                SortBy = queryParams.SortBy,
                SortType = queryParams.SortType,
                PageSize = queryParams.PageSize,
                PageNumber = queryParams.PageNumber,
                Count = count
            });
        }
Example #2
0
 public async Task <CategoryDTO> GetById(int id)
 {
     return(DTOConvert.CategoryModelToDTO(await _context.Categories.FindAsync(id)));
 }
Example #3
0
        public void AddCategory(CategoryDTO dto)
        {
            Category cat = DTOConvert.CategoryDTOToModel(dto);

            _context.Categories.AddAsync(cat);
        }
Example #4
0
        public void AddProduct(ProductDTO dto)
        {
            Product product = DTOConvert.ProductDTOToModel(dto);

            _context.Add(product);
        }