public void ExpensesCategoryMustBeLessOrEqualToBudgetAmount() { var request = new MonthBudgetRequest { UserId = "ingris", Amount = 3000, BudgetCategories = new List <BudgetCategoryDto> { new BudgetCategoryDto { Percentage = 80 }, new BudgetCategoryDto { Percentage = 20, CategoryId = 1 } } }; var categories = new List <ExpenseCategory> { new ExpenseCategory { }, new ExpenseCategory { UId = 1 } }; var budget = ExpensesTrackerFactory.CreateNewBudget(request, categories); var validation = _userBudgetDomainService.IsValidNewUserBudget(budget); Assert.IsTrue(validation.IsValid()); Assert.AreEqual((decimal)3000, budget.BudgetDetails.Sum(c => c.Amount)); }
public void BudgetMustHaveOwnerId() { var request = new MonthBudgetRequest { }; var budget = ExpensesTrackerFactory.CreateNewBudget(request, null); var validation = _userBudgetDomainService.IsValidNewUserBudget(budget); Assert.IsFalse(validation.IsValid()); Assert.AreEqual(Messages.BudgetMustHaveAnOwner, validation.ValidationErrorMessage); }
public async Task <MonthBudgetResponse> AddNewUserBudget(MonthBudgetRequest request) { var categories = await _context.ExpenseCategory.Where(c => c.IsDefault || c.OwnerId == request.UserId).ToListAsync(); var newBudget = ExpensesTrackerFactory.CreateNewBudget(request, categories); var validation = _budgetDomainService.IsValidNewUserBudget(newBudget); if (validation.IsValid()) { _context.UserBudget.Add(newBudget); await _context.SaveChangesAsync(); } return(MapUserBudgetToResponse(newBudget, !validation.IsValid(), validation)); }
public void AllExpensesCategoriesExists() { var request = new MonthBudgetRequest { UserId = "ingris", BudgetCategories = new List <BudgetCategoryDto> { new BudgetCategoryDto { } } }; var categories = new List <ExpenseCategory> { }; var budget = ExpensesTrackerFactory.CreateNewBudget(request, categories); var validation = _userBudgetDomainService.IsValidNewUserBudget(budget); Assert.IsFalse(validation.IsValid()); Assert.AreEqual(Messages.ExpensesCategoriesMustExists, validation.ValidationErrorMessage); }
public static UserBudget CreateNewBudget(MonthBudgetRequest request, IEnumerable <ExpenseCategory> categories) { var budget = new UserBudget { UserId = request.UserId, CurrencySign = request.Currency ?? "$", StartDate = request.StartDate, EndDate = request.EndDate, Amount = request.Amount, }; budget.BudgetDetails = request.BudgetCategories?.Select(c => new BudgetDetail { CategoryId = c.CategoryId, UserBudget = budget, ExpenseCategory = categories?.FirstOrDefault(d => d.UId == c.CategoryId), Percentage = c.Percentage / 100, Amount = request.Amount * (c.Percentage / 100) }).ToList(); return(budget); }
public void ExpensesCategoryMustBeLessOrEqualTo100Percent() { var request = new MonthBudgetRequest { UserId = "ingris", Amount = 3000, BudgetCategories = new List <BudgetCategoryDto> { new BudgetCategoryDto { Percentage = 120, } } }; var categories = new List <ExpenseCategory> { new ExpenseCategory { } }; var budget = ExpensesTrackerFactory.CreateNewBudget(request, categories); var validation = _userBudgetDomainService.IsValidNewUserBudget(budget); Assert.IsFalse(validation.IsValid()); Assert.AreEqual(Messages.ExpesesCategoriesExceed100Percent, validation.ValidationErrorMessage); }