Пример #1
0
        public async Task <TableFilteringDTO> GetFiltered(DataTableModel model)
        {
            ServiceValidator.ObjectIsEqualNull(model);
            var allTransactions   = this.GetAllTransactionsTable();
            int totalResultsCount = await allTransactions.CountAsync();

            int filteredResultsCount = totalResultsCount;

            var searchBy = model.search?.value ?? string.Empty;

            var take = model.length;
            var skip = model.start;

            if (take < 0)
            {
                take = 10;
            }
            if (skip < 0)
            {
                skip = 0;
            }

            var dic = new Dictionary <string, Expression <Func <Transaction, object> > >()
            {
                { "createdOn", tr => tr.CreatedOn },
                { "alias", tr => tr.User.Alias },
                { "transactionTypeName", tr => tr.TransactionType.Name },
                { "normalisedAmount", tr => tr.NormalisedAmount }
            };

            string sortBy  = "";
            bool   sortDir = true;

            if (model.order != null)
            {
                sortBy  = model.columns[model.order[0].column].data;
                sortDir = model.order[0].dir.ToLower() == "asc";
            }

            Expression <Func <Transaction, object> > exp;

            if (!dic.ContainsKey(sortBy))
            {
                exp = dic["createdOn"];
            }
            else
            {
                exp = dic[sortBy];
            }

            var collection = allTransactions.Where(tr => tr.User.Alias.Contains(searchBy) || tr.TransactionType.Name.Contains(searchBy));

            if (sortDir)
            {
                collection = collection.OrderBy(exp);
            }
            else
            {
                collection = collection.OrderByDescending(exp);
            }

            if (searchBy != string.Empty)
            {
                filteredResultsCount = collection.Count();
            }

            var returnModel = new TableFilteringDTO()
            {
                Transactions    = await collection.Skip(skip).Take(take).ToListAsync(),
                RecordsTotal    = totalResultsCount,
                RecordsFiltered = filteredResultsCount
            };

            return(returnModel);
        }