public async Task <SyncedEntityIds> SyncEntitiesAsync(SyncEntities model) { (var conn, var uowTransaction) = _unitOfWork.StartTransaction(); var accountIds = new int[model.Accounts.Count]; var categoryIds = new int[model.Categories.Count]; var transactionIds = new int[model.Transactions.Count]; var upcomingExpenseIds = new int[model.UpcomingExpenses.Count]; var debtIds = new int[model.Debts.Count]; for (var i = 0; i < accountIds.Length; i++) { var account = _mapper.Map <Account>(model.Accounts[i]); int id = await _accountsRepository.CreateAsync(account, conn, uowTransaction); accountIds[i] = id; } for (var i = 0; i < categoryIds.Length; i++) { var category = _mapper.Map <Category>(model.Categories[i]); int id = await _categoriesRepository.CreateAsync(category, conn, uowTransaction); categoryIds[i] = id; } for (var i = 0; i < transactionIds.Length; i++) { var transaction = _mapper.Map <Transaction>(model.Transactions[i]); int id = await _transactionsRepository.CreateAsync(transaction, conn, uowTransaction); transactionIds[i] = id; } for (var i = 0; i < upcomingExpenseIds.Length; i++) { var upcomingExpense = _mapper.Map <UpcomingExpense>(model.UpcomingExpenses[i]); int id = await _upcomingExpensesRepository.CreateAsync(upcomingExpense, conn, uowTransaction); upcomingExpenseIds[i] = id; } for (var i = 0; i < debtIds.Length; i++) { var debt = _mapper.Map <Debt>(model.Debts[i]); int id = await _debtsRepository.CreateAsync(debt, conn, uowTransaction); debtIds[i] = id; } _unitOfWork.CommitTransaction(conn, uowTransaction); return(new SyncedEntityIds(accountIds, categoryIds, transactionIds, upcomingExpenseIds, debtIds)); }
public async Task <CategoryId> CreateAsync(UncreatedCategory category) { if (await _repository.NameAlreadyExistsAsync(category.Name)) { throw new CategoryNameAlreadyExistsException(category.Name); } var categoryId = await _repository.CreateAsync(category); return(categoryId); }
public Task <int> CreateAsync(CreateCategory model) { if (model.Type == CategoryType.DepositOnly) { model.GenerateUpcomingExpense = false; } var category = _mapper.Map <Category>(model); return(_categoriesRepository.CreateAsync(category)); }
public async Task <IActionResult> CreateCategory(Category category) { if (string.IsNullOrWhiteSpace(category.Title)) { return(BadRequest("Zadejte název kategorie.")); } if (await categoriesRepository.FindByTitleAsync(category.Title) != null) { return(BadRequest("Kategorie s tímto názvem již existuje.")); } category.Created = DateTime.Now; category.User = await userManager.GetUserAsync(User); await categoriesRepository.CreateAsync(category); return(Json(category)); }
public async Task <Category> CreateAsync(CreateCategoryDto categoryToCreate) { if (categoryToCreate == null) { throw new ArgumentNullException(nameof(categoryToCreate)); } if (categoryToCreate.Name.Length > 32) { throw new ArgumentOutOfRangeException(nameof(categoryToCreate.Name), categoryToCreate.Name, "Category name length cannot be greater than 32."); } if (await _categoriesRepository.ExistsByName(categoryToCreate.Name)) { throw new ArgumentException(nameof(categoryToCreate.Name), $"Category {categoryToCreate.Name} already exists."); } var modelDb = await _categoriesRepository.CreateAsync(categoryToCreate); return(modelDb); }
public async Task <CategoryDto> Handle(CreateCategoryCommand request, CancellationToken cancellationToken) { if (request.Dto == null) { throw new ValidationException($"{nameof(request.Dto)} is invalid"); } var item = new Category { Name = request.Dto.Name }; var category = await _repository.CreateAsync(item, cancellationToken); var result = new CategoryDto { ID = category.ID, Name = category.Name }; return(result); }
public async Task <ServiceResult <int> > CreateAsync(ClaimsPrincipal user, int groupId, CategoryRequestModel category) { var group = groupsRepository.Get(groupId); if (group is null) { return(ServiceResult <int> .Error(404, "Group was not found")); } var authorizationResult = await authorizationService.AuthorizeAsync(user, group, GroupOperations.Write); if (!authorizationResult.Succeeded) { return(ServiceResult <int> .Error(401, "Unauthorized")); } var existing = categoriesRepository.GetCount(groupId, category.Name); if (existing != 0) { return(ServiceResult <int> .Error(400, "Categgory with this name already exists in this group")); } var categoryEntity = new Category() { Name = category.Name, Description = category.Description, GroupId = groupId }; var result = await categoriesRepository.CreateAsync(categoryEntity); if (result is null) { return(ServiceResult <int> .Error(500, "Could not create category")); } return(ServiceResult <int> .Success(CategoryViewModel.FromModel(result))); }