예제 #1
0
        public void ValidFiltersTest(string filterString, int?expMonth, int?expYear, string expCategory, string expSubcat, ExpenseType?expectedType = null, AmountCompareType?amountComp = null, decimal amount = 0)
        {
            Thread.CurrentThread.CurrentCulture = new CultureInfo("DE");
            var result = ExpenseFilterString.Parse(filterString);

            result.Month.Should().Be(expMonth);
            result.Year.Should().Be(expYear);
            result.Category.Should().Be(expCategory);
            result.Subcategory.Should().Be(expSubcat);
            result.Type.Should().Be(expectedType);

            if (amountComp.HasValue)
            {
                result.AmountCompareType.Should().Be(amountComp.Value);
                result.Amount.Should().Be(amount);
            }
        }
예제 #2
0
 public void InvalidFiltersTest(string filterString)
 {
     this.Invoking(i => ExpenseFilterString.Parse(filterString))
     .Should().Throw <FilterParseException>();
 }
예제 #3
0
        public async Task <ExpenseViewModel[]> Handle(GetExpensesQuery request, CancellationToken cancellationToken)
        {
            var account = await this.db.Accounts
                          .SingleOrDefaultAsync(acct => acct.Id == request.Account.Id);

            if (account == null)
            {
                return(new ExpenseViewModel[0]);
            }

            var query = this.db.Expenses
                        .Include(ex => ex.Account)
                        .OrderByDescending(ex => ex.Date)
                        .ThenByDescending(ex => ex.LastModified)
                        .Where(ex => ex.AccountId == account.Id);

            if (!request.IncludeFuture)
            {
                query = query.Where(e => e.Date.Date <= DateTime.Now.Date);
            }

            if (!string.IsNullOrEmpty(request.SearchText))
            {
                if (request.SearchText.StartsWith("!"))
                {
                    try
                    {
                        var filter = ExpenseFilterString.Parse(request.SearchText.Substring(1));
                        if (filter.Month.HasValue)
                        {
                            query = query.Where(ex => ex.Date.Month == filter.Month.Value);
                        }
                        if (filter.Year.HasValue)
                        {
                            query = query.Where(ex => ex.Date.Year == filter.Year.Value);
                        }
                        if (!string.IsNullOrEmpty(filter.Category))
                        {
                            query = query.Where(ex => ex.Category.Equals(filter.Category));
                        }
                        if (!string.IsNullOrEmpty(filter.Subcategory))
                        {
                            query = query.Where(ex => ex.Subcategory.Equals(filter.Subcategory));
                        }
                        if (filter.Type.HasValue)
                        {
                            query = query.Where(ex => ex.Type == filter.Type.Value);
                        }
                        if (filter.AmountCompareType.HasValue)
                        {
                            switch (filter.AmountCompareType.Value)
                            {
                            case AmountCompareType.Equals:
                                query = query.Where(ex => ex.Amount == filter.Amount);
                                break;

                            case AmountCompareType.GreaterThan:
                                query = query.Where(ex => ex.Amount > filter.Amount);
                                break;

                            case AmountCompareType.LowerThan:
                                query = query.Where(ex => ex.Amount < filter.Amount);
                                break;
                            }
                        }
                    }
                    catch (FilterParseException ex)
                    {
                        throw new UserException(ex.Message, ex);
                    }
                }
                else
                {
                    var amountSearchText = request.SearchText;
                    if (double.TryParse(amountSearchText, NumberStyles.AllowDecimalPoint, SearchCulture, out var amount))
                    {
                        // Translate the amount's search culture into local culture (used for differing decimal points).
                        amountSearchText = amount.ToString("0.00");
                    }

                    query = query.Where(ex => ex.Category.Contains(request.SearchText) ||
                                        ex.Subcategory.Contains(request.SearchText) ||
                                        ex.Details.Contains(request.SearchText) ||
                                        ex.Amount.ToString().Contains(amountSearchText));
                }
            }

            var expenses = await query
                           .Skip(request.Offset)
                           .Take(request.Limit)
                           .ToListAsync();

            return(expenses
                   .Select(ExpenseViewModel.FromDb)
                   .ToArray());
        }