public static Transaction BuildTransaction(AccountingUser user, decimal total = 100, Category fromCat = null, Category toCat = null, TransactionType transType = null) { return new Transaction { Total = total, AccountingUser = user, From = fromCat == null ? new Category { Name = "TestCategoryType" } : fromCat, To = toCat == null ? new Category { Name = "OtherTestCategory" } : toCat, DateTime = DateTime.Now, TransactionType = transType == null ? new TransactionType { Name = "TestTransactionType" } : transType }; }
private async Task SeedDevelopmentData() { if (await this.userManager.FindByEmailAsync("*****@*****.**") == null) { var newUser = new AccountingUser { UserName = "******", Email = "*****@*****.**" }; var result = await this.userManager.CreateAsync(newUser, "password"); } var base64Token = "OMuo2TLAB/iOX4IQs98+ag=="; SignupToken token = null; if (!this.context.SignupTokens.Any(t => t.Token == base64Token)) { this.context.SignupTokens.Add(new SignupToken { Token = base64Token }); } else { token = this.context.SignupTokens.First(t => t.Token == base64Token); token.Used = false; token.AccountingUser = null; token.AccountingUserId = null; this.context.SignupTokens.Update(token); } var user = await this.userManager.FindByNameAsync("testuser"); var expenseType = new CategoryType { Name = "Expenses", DisplayOrder = 2, AddTo = true }; var assetType = new CategoryType { Name = "Assets", DisplayOrder = 1, AddTo = true, AddFrom = false }; var incomeType = new CategoryType { Name = "Income", DisplayOrder = 3, AddFrom = true }; if (!this.context.CategoryTypes.Any(c => c.Name == expenseType.Name)) { this.context.CategoryTypes.Add(expenseType); } else { expenseType = this.context.CategoryTypes.First(c => c.Name == expenseType.Name); } if (!this.context.CategoryTypes.Any(c => c.Name == assetType.Name)) { this.context.CategoryTypes.Add(assetType); } else { assetType = this.context.CategoryTypes.First(c => c.Name == assetType.Name); } if (!this.context.CategoryTypes.Any(c => c.Name == incomeType.Name)) { this.context.CategoryTypes.Add(incomeType); } else { incomeType = this.context.CategoryTypes.First(c => c.Name == incomeType.Name); } var savings = new Category { Name = "Savings", NormalizedName = Category.NormalizeName("Savings"), CategoryType = assetType, AccountingUser = user }; var grocceries = new Category { Name = "Grocceries", NormalizedName = Category.NormalizeName("Grocceries"), CategoryType = expenseType, AccountingUser = user }; var salary = new Category { Name = "Salary", NormalizedName = Category.NormalizeName("Salary"), CategoryType = incomeType, AccountingUser = user }; var work = new Category { Name = "Work", NormalizedName = Category.NormalizeName("Work"), CategoryType = incomeType, AccountingUser = user, ParentCategory = salary }; var subwork = new Category { Name = "Subwork", NormalizedName = Category.NormalizeName("Subwork"), CategoryType = incomeType, AccountingUser = user, ParentCategory = work }; var incomeTransType = new TransactionType { Name = "Income", From = incomeType, To = assetType }; var expenseTransType = new TransactionType { Name = "Expense", From = assetType, To = expenseType }; var transferTransType = new TransactionType { Name = "Transfer", From = assetType, To = assetType }; if (!this.context.Categories.Any(c => c.Name == savings.Name)) { this.context.Categories.Add(savings); this.context.Categories.Add(grocceries); this.context.Categories.Add(salary); this.context.Categories.Add(work); this.context.Categories.Add(subwork); } else { savings = this.context.Categories.SingleOrDefault(c => c.Name == savings.Name); grocceries = this.context.Categories.SingleOrDefault(c => c.Name == grocceries.Name); salary = this.context.Categories.SingleOrDefault(c => c.Name == salary.Name); work = this.context.Categories.SingleOrDefault(c => c.Name == work.Name); subwork = this.context.Categories.SingleOrDefault(c => c.Name == subwork.Name); } if (!this.context.TransactionTypes.Any(t => t.Name == incomeTransType.Name)) { this.context.TransactionTypes.Add(incomeTransType); } else { incomeTransType = this.context.TransactionTypes.SingleOrDefault(t => t.Name == incomeTransType.Name); } if (!this.context.TransactionTypes.Any(t => t.Name == expenseTransType.Name)) { this.context.TransactionTypes.Add(expenseTransType); } else { expenseTransType = this.context.TransactionTypes.SingleOrDefault(t => t.Name == expenseTransType.Name); } if (!this.context.TransactionTypes.Any(t => t.Name == transferTransType.Name)) { this.context.TransactionTypes.Add(transferTransType); } else { transferTransType = this.context.TransactionTypes.SingleOrDefault(t => t.Name == transferTransType.Name); } this.context.SaveChanges(); var trans = new Transaction { From = salary, To = savings, TransactionType = incomeTransType, Total = 100, DateTime = DateTime.UtcNow, AccountingUser = user }; if (!this.context.Transactions.Any()) { this.context.Transactions.Add(trans); trans.To.AddTransaction(trans, from: false); trans.From.AddTransaction(trans, from: true); this.context.SaveChanges(); } }
public bool ValidateAndMapCreateCategory(CategoryViewModel vm, Controller controller, string userId, out Category category, out ErrorViewModel errors) { var modelState = controller.ModelState; category = null; errors = null; if (!modelState.IsValid) { errors = new ErrorViewModel { Error = CategoryErrors.ValidationErrors.CategoryInvalidError, Errors = modelState.Values.SelectMany(v => v.Errors).Select(e => e.ErrorMessage).ToList() }; this.logger.LogWarning("Invalid category model:" + string.Join(";", modelState.Values.SelectMany(v => v.Errors) .Select(e => e.ErrorMessage))); return false; } if (this.categoryRepo.FindAllWhere(c => string.Equals(c.NormalizedName, Category.NormalizeName(vm.Name))).Any()) { errors = new ErrorViewModel { Error = string.Format(CategoryErrors.ValidationErrors.CategoryNameAlreadyExistsError, vm.Name) }; return false; } CategoryType catType; if ((catType = this.categoryTypeRepo.FindById(vm.CategoryTypeId)) == null) { errors = new ErrorViewModel { Error = CategoryErrors.ValidationErrors.CategoryNoCategoryTypeError }; return false; } Category parentCategory = null; if(vm.ParentCategoryId.HasValue){ parentCategory = this.categoryRepo.FindById(vm.ParentCategoryId.Value, c => c.CategoryType); if (parentCategory == null || !string.Equals(parentCategory.AccountingUserId, userId)) { errors = new ErrorViewModel { Error = string.Format(CategoryErrors.ValidationErrors.CategoryParentCategoryNotFoundError, vm.ParentCategoryId) }; return false; } if (parentCategory.CategoryType.Id != catType.Id) { errors = new ErrorViewModel { Error = CategoryErrors.ValidationErrors.CategoryParentTypeMismatchError }; return false; } if (parentCategory.ParentCategoryId.HasValue && this.categoryRepo.FindById(parentCategory.ParentCategoryId.Value).ParentCategoryId.HasValue){ errors = new ErrorViewModel { Error = CategoryErrors.ValidationErrors.CategoryDepthTooGreatError }; return false; } } category = Mapper.Map<Category>(vm); category.Id = 0; category.Total = 0; category.NormalizedName = Category.NormalizeName(category.Name); category.ParentCategoryId = null; category.ParentCategory = parentCategory; category.CategoryType = catType; category.ChildCategories = null; category.ExitingTransactions = null; category.AccountingUserId = userId; return true; }
private void CreateCategories(AccountingUser user) { var sysCategories = this.sysCategoryRepo.FindAll(c => c.CategoryType).ToList(); foreach (var sysCat in sysCategories) { var userCat = new Category { Name = sysCat.Name, AccountingUser = user, CategoryType = sysCat.CategoryType }; this.categoryRepo.Add(userCat); } this.categoryRepo.SaveChanges(); }
public bool ValidateAndMapUpdateCategory(long id, CategoryViewModel vm, Category previous, Controller controller, string userId, out Category category, out ErrorViewModel errors) { var modelState = controller.ModelState; category = null; errors = null; if (vm.Id != id) { errors = new ErrorViewModel { Error = CategoryErrors.ValidationErrors.CategoryInvalidRouteError }; return false; } if (!modelState.IsValid) { errors = new ErrorViewModel { Error = CategoryErrors.ValidationErrors.CategoryInvalidError, Errors = modelState.Values.SelectMany(v => v.Errors).Select(e => e.ErrorMessage).ToList() }; this.logger.LogWarning("Invalid category model:" + string.Join(";", modelState.Values.SelectMany(v => v.Errors) .Select(e => e.ErrorMessage))); return false; } // Does a different category exist with the same name? if (this.categoryRepo.FindAllWhere(c => string.Equals(c.NormalizedName, Category.NormalizeName(vm.Name)) && c.Id != id).Any()) { errors = new ErrorViewModel { Error = string.Format(CategoryErrors.ValidationErrors.CategoryNameAlreadyExistsError, vm.Name) }; return false; } if (vm.CategoryTypeId != previous.CategoryType.Id) { errors = new ErrorViewModel { Error = CategoryErrors.ValidationErrors.CategoryCannotChangeTypeError }; return false; } if (vm.ParentCategoryId.HasValue && vm.ParentCategoryId.Value == id) { errors = new ErrorViewModel { Error = CategoryErrors.ValidationErrors.CategoryCannotBeOwnParentError }; return false; } Category parentCategory = null; if (vm.ParentCategoryId.HasValue || previous.ParentCategoryId.HasValue) { var hasChildren = this.categoryRepo.FindAllWhere(c => c.ParentCategoryId == id).Any(); if (hasChildren && vm.ParentCategoryId != previous.ParentCategoryId) { errors = new ErrorViewModel { Error = CategoryErrors.ValidationErrors.CategoryMoveWithChildrenError }; return false; } if (vm.ParentCategoryId.HasValue) { parentCategory = this.categoryRepo.FindById(vm.ParentCategoryId.Value, c => c.CategoryType); if (parentCategory == null || !string.Equals(parentCategory.AccountingUserId, userId)) { errors = new ErrorViewModel { Error = string.Format(CategoryErrors.ValidationErrors.CategoryParentCategoryNotFoundError, vm.ParentCategoryId) }; return false; } if (parentCategory.CategoryType.Id != vm.CategoryTypeId) { errors = new ErrorViewModel { Error = CategoryErrors.ValidationErrors.CategoryParentTypeMismatchError }; return false; } if (parentCategory.ParentCategoryId.HasValue && this.categoryRepo.FindById(parentCategory.ParentCategoryId.Value).ParentCategoryId.HasValue) { errors = new ErrorViewModel { Error = CategoryErrors.ValidationErrors.CategoryDepthTooGreatError }; return false; } } } category = previous; category.Name = vm.Name; category.NormalizedName = Category.NormalizeName(category.Name); category.ParentCategoryId = parentCategory?.Id; category.ParentCategory = parentCategory; return true; }