示例#1
0
        public async Task <DTOExpense> AddExpense(DTOExpense dtoExpense)
        {
            using (LuccaContext context = this.serviceProvider.GetService <LuccaContext>())
            {
                this.ThrowIfExpenseInvalid(context, dtoExpense);

                var expenseToAdd = new Expenses(dtoExpense);
                context.Expenses.Add(expenseToAdd);

                await context.SaveChangesAsync();

                return(expenseToAdd.ToDTOExpense(context));
            }
        }
示例#2
0
        public DTOExpense ToDTOExpense(LuccaContext context)
        {
            context.Entry(this).Reference(expense => expense.User).Load();

            // TODO use a mapper
            return(new DTOExpense()
            {
                UserId = this.UserId,
                User = $"{this.User.FirstName.Trim()} {this.User.LastName.Trim()}",
                Amount = this.Amount,
                Category = Enum.GetName(typeof(ExpenseCategory), this.Category),
                Comment = this.Comment,
                Currency = this.Currency,
                PurchasedOn = this.PurchasedOn
            });
        }
示例#3
0
        private void ThrowIfExpenseInvalid(LuccaContext context, DTOExpense expense)
        {
            var errors = new Dictionary <ExpenseErrorCode, string>();

            if (expense.PurchasedOn > this.timeService.GetCurrentUtcTime())
            {
                errors.Add(ExpenseErrorCode.PurchaseInFuture, $"Expense cannot be recorded for this date {expense.PurchasedOn}");
            }
            else if (DateTime.UtcNow - expense.PurchasedOn > TimeSpan.FromDays(90))
            {
                errors.Add(ExpenseErrorCode.PurchaseMoreThan3MonthOld, $"The expense is older than 90 days: {expense.PurchasedOn}");
            }

            if (string.IsNullOrEmpty(expense.Comment))
            {
                errors.Add(ExpenseErrorCode.CommentIsMandatory, $"The expense comment is mandatory");
            }

            var user = context.UserInfo.Where(user => user.Id == expense.UserId).FirstOrDefault();

            if (user == null)
            {
                errors.Add(ExpenseErrorCode.UserNotFound, $"The user {expense.UserId} cannot be found");
            }
            else
            {
                var userExpenses = context.Expenses.Where(exp => exp.UserId == expense.UserId);
                if (userExpenses.Any(exp => exp.PurchasedOn == expense.PurchasedOn && exp.Amount == expense.Amount))
                {
                    errors.Add(ExpenseErrorCode.DuplicatedExpense, $"The expense purchased on {expense.PurchasedOn} with an amount of {expense.Amount} seems already recorded.");
                }

                if (expense.Currency != user.Currency)
                {
                    errors.Add(ExpenseErrorCode.ExpenseCurrencyDifferentThanUserCurrency, $"The expense currency {expense.Currency} should be the same than the user currency {user.Currency}");
                }
            }

            if (errors.Any())
            {
                throw new ExpenseValidationException(errors);
            }
        }
示例#4
0
        public async Task <List <DTOExpense> > GetExpenses(ExpensesQuery query)
        {
            using (LuccaContext context = this.serviceProvider.GetService <LuccaContext>())
            {
                var expenses = context.Expenses.AsQueryable();

                if (query.UserId.HasValue)
                {
                    expenses = context.Expenses.Where(expense => expense.UserId == query.UserId);
                }

                if (query.Sort.HasValue)
                {
                    expenses = this.AscSort(expenses, query.Sort.Value);
                }

                return(await expenses.Select(expense => expense.ToDTOExpense(context)).ToListAsync());
            }
        }