public async Task <Expense> CreateExpense(CreateExpenseParameter createExpenseParameter) { User user = await this.userRepository.Get(createExpenseParameter.UserId); string userCurrency = user.Currency; string expenseCurrency = createExpenseParameter.Currency; DateTime expenseDate = createExpenseParameter.Date; if (expenseDate > DateTime.Now) { throw new ExpenseInTheFutureException(); } DateTime threeMonthsAgo = DateTime.Today.AddMonths(-3); if (expenseDate < threeMonthsAgo) { throw new OutdatedExpenseException((int)Math.Round((DateTime.Now - expenseDate).TotalDays)); } if (string.IsNullOrEmpty(createExpenseParameter.Comment)) { throw new EmptyCommentException(); } IEnumerable <Expense> expensesForThisUser = await expenseRepository.Find(e => e.User.Id.Equals(createExpenseParameter.UserId)); if (expensesForThisUser.Any(e => (e.Amount == createExpenseParameter.Amount) && (e.Date == expenseDate))) { throw new DuplicateExpenseException(); } if (userCurrency != expenseCurrency) { throw new InvalidCurrencyException(expenseCurrency, userCurrency); } Expense createdExpense = await this.expenseRepository.Add( new Expense { User = user, Amount = createExpenseParameter.Amount, Currency = expenseCurrency, Comment = createExpenseParameter.Comment, Date = expenseDate, ExpenseType = createExpenseParameter.ExpenseType } ); return(createdExpense); }
public async Task <ActionResult <Expense> > Post([FromBody] CreateExpenseParameter createExpenseParameter) { return(await expenseService.CreateExpense(createExpenseParameter)); }
public async Task CreateDuplicatedExpenseKO() { CreateExpenseParameter parameter = BuildCreateExpenseParameter(1, ExpenseType.Misc, 20, "RUB", "initial", DateTime.Today.AddMonths(-1)); await Assert.ThrowsAsync <DuplicateExpenseException>(() => this.ExpenseService.CreateExpense(parameter)); }
public async Task CreateExpenseWithInvalidCurrencyKO() { CreateExpenseParameter parameter = BuildCreateExpenseParameter(1, ExpenseType.Misc, 20, "USD", "not the currency of this user", DateTime.Today.AddMonths(-1)); await Assert.ThrowsAsync <InvalidCurrencyException>(() => this.ExpenseService.CreateExpense(parameter)); }
public async Task CreateNotCommentedExpenseKO() { CreateExpenseParameter parameter = BuildCreateExpenseParameter(1, ExpenseType.Restaurant, 20, "$", string.Empty, DateTime.Today.AddMonths(-2)); await Assert.ThrowsAsync <EmptyCommentException>(() => this.ExpenseService.CreateExpense(parameter)); }
public async Task CreateOutdatedExpenseKO() { CreateExpenseParameter parameter = BuildCreateExpenseParameter(1, ExpenseType.Restaurant, 20, "$", "outdated", DateTime.Today.AddMonths(-4)); await Assert.ThrowsAsync <OutdatedExpenseException>(() => this.ExpenseService.CreateExpense(parameter)); }
public async Task CreateExpenseWithDateInFutureKO() { CreateExpenseParameter parameter = BuildCreateExpenseParameter(1, ExpenseType.Restaurant, 20, "$", "date in future", DateTime.Now.AddDays(1)); await Assert.ThrowsAsync <ExpenseInTheFutureException>(() => this.ExpenseService.CreateExpense(parameter)); }