Exemplo n.º 1
0
        /// <summary>
        /// This method Retrieves data and applies filters/ordering/paging from our database
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        protected async Task <DatatableResponse <T> > GetData(DatatablesPostModel model)
        {
            //counts the number of rows in the db.
            var total = await _context.Set <T>()
                        .CountAsync();

            //how many items per page. Example 10
            var pageSize = model.Length;

            //calculates the page we want
            var pageIndex = (int)Math.Ceiling(
                (decimal)(model.Start / model.Length) + 1);

            //Column and how to sort. This is how datatables sends us the information
            var columnName = model.Columns[model.Order[0].Column].Data;
            //descending sort or asc
            var isDescending = model.Order[0].Dir == "desc";

            //initializes the repository
            var repository = new Repository <T>(_context);

            //Here we get the search value from datatable
            var filter = GetFilter(model.Search.Value);
            //this method gets the includes for the entity
            var includes = GetIncludes();

            var gridItems = await repository
                            .GetAllWithPagingAsync(filter, includes,
                                                   pageSize, pageIndex, t => t.OrderingHelper(columnName, isDescending));

            //we map the data to the response we want to send
            var response = new DatatableResponse <T>
            {
                data            = gridItems,
                draw            = model.Draw,
                recordsFiltered = total,
                recordsTotal    = total
            };

            return(response);
        }
Exemplo n.º 2
0
        public async Task <PositionViewModel> GetPosition(int?id)
        {
            if (id == null)
            {
                return(null);
            }

            var position = await _context.Set <Position>()
                           .FindAsync(id);

            if (position == null)
            {
                return(null);
            }

            var viewModel = _mapper.Map <PositionViewModel>(position);

            viewModel.Departments = await _context.Departments
                                    .ToListAsync();

            return(viewModel);
        }
Exemplo n.º 3
0
 public Repository(ChocolateDbContext context)
 {
     _context = context;
     _set     = _context.Set <TEntity>();
 }