public IEnumerable <GetExpenseDto> GetAll(DateTime?from = null, DateTime?to = null, TypeEnum?type = null)
        {
            IQueryable <Expense> result = context.Expenses.Include(expense => expense.Comments);

            if (from == null && to == null && type == null)
            {
                return(result.Select(expense => GetExpenseDto.DtoFromModel(expense)));
            }

            if (from != null)
            {
                result = result.Where(expense => expense.Date >= from);
            }

            if (to != null)
            {
                result = result.Where(expense => expense.Date <= to);
            }

            if (type != null)
            {
                result = result.Where(expense => expense.Type == type);
            }

            return(result.Select(expense => GetExpenseDto.DtoFromModel(expense)));
        }
Exemple #2
0
        public PaginatedList <GetExpenseDto> GetAll(int page, DateTime?from = null, DateTime?to = null, TypeEnum?type = null)
        {
            IQueryable <Expense> result = context
                                          .Expenses
                                          .OrderBy(e => e.Date)
                                          .Include(expense => expense.Comments);

            PaginatedList <GetExpenseDto> paginatedResult = new PaginatedList <GetExpenseDto>();

            paginatedResult.CurrentPage = page;

            //if (from == null && to == null && type == null)
            //{
            //    return result.Select(expense => GetExpenseDto.DtoFromModel(expense));
            //}

            if (from != null)
            {
                result = result.Where(expense => expense.Date >= from);
            }

            if (to != null)
            {
                result = result.Where(expense => expense.Date <= to);
            }

            if (type != null)
            {
                result = result.Where(expense => expense.Type == type);
            }

            paginatedResult.NumberOfPages = (result.Count() - 1) / PaginatedList <GetExpenseDto> .EntriesPerPage + 1;
            result = result
                     .Skip((page - 1) * PaginatedList <GetExpenseDto> .EntriesPerPage)
                     .Take(PaginatedList <GetExpenseDto> .EntriesPerPage);
            paginatedResult.Entries = result.Select(expense => GetExpenseDto.DtoFromModel(expense)).ToList();

            return(paginatedResult);
        }