public async Task <ExpenseFlowModel> Update(ExpenseFlowModel model) { var other = await _repository.FindByNameAsync <ExpenseFlow>(_currentSession.UserId, model.Name); if (other != null) { if (other.Id != model.Id) { throw new ArgumentException("Категория расходов с таким названием уже есть"); } else { _repository.Detach(other); } } var flow = new ExpenseFlow { Name = model.Name, Balance = model.Balance, DateCreated = model.DateCreated, Number = model.Number, IsDeleted = false, OwnerId = _currentSession.UserId }; if (model.Id < 0) { flow.Version = 1; _repository.Create(flow); } else { flow.Id = model.Id; flow.Version = model.Version + 1; _repository.Update(flow); if (model.Categories != null) { UpdateFlowCategories(flow.Id, model.Categories); } } await _repository.SaveChangesAsync().ConfigureAwait(false); model.Id = flow.Id; model.Version = flow.Version; return(model); }
public static ExpenseFlowModel ToModel(this ExpenseFlow entity) { if (entity == null) { return(null); } return(new ExpenseFlowModel { Id = entity.Id, Number = entity.Number, Name = entity.Name, DateCreated = entity.DateCreated, Balance = entity.Balance, Version = entity.Version, }); }
public ExpenseFlow CreateExpenseFlow(string name, decimal balance, DateTime date, int number) { var commands = _unitOfWork.GetCommandRepository <ExpenseFlow>(); var entity = new ExpenseFlow { Name = name, Balance = balance, DateCreated = date, Number = number, Version = 1, OwnerId = UserSession.UserId }; commands.Create(entity); return(entity); }
public async Task <IActionResult> OnPostCommitAsync() { return(await ExpenseFlow.ProcessAsync(ModelState, nameof(ExpenseFlow), async() => { var model = await _expenseFlowQueries.GetById(ExpenseFlow.Id); ExpenseFlow.ToExpenseFlowModel(model); await _expenseFlowCommands.Update(model); return RedirectToPage("./ExpenseFlows"); }, async() => { await LoadCategories(); return Page(); })); }
public async Task <int> ProcessReducingBalanceAsCreditFees(Account account, decimal reduceAmount) { if (account == null) { throw new ArgumentNullException(nameof(account)); } if (account.AccountType != AccountType.CreditCard) { throw new InvalidOperationException("Счет должен быть кредитной картой"); } var category = await _repository.FindByNameAsync <Category>( _currentSession.UserId, "Кредиты").ConfigureAwait(false); if (category == null) { category = new Category { OwnerId = _currentSession.UserId, Name = "Кредиты", }; _repository.Create(category); await _repository.SaveChangesAsync().ConfigureAwait(false); } var mustSave = false; var product = await _repository.FindByNameAsync <Product>(_currentSession.UserId, account.Name).ConfigureAwait(false); if (product == null) { product = new Product { OwnerId = _currentSession.UserId, Name = account.Name, CategoryId = category.Id }; _repository.Create(product); mustSave = true; } var flow = await _repository.FindByNameAsync <ExpenseFlow>(_currentSession.UserId, "Кредиты").ConfigureAwait(false); if (flow == null) { flow = new ExpenseFlow { OwnerId = _currentSession.UserId, Balance = 0, DateCreated = _timeService.ClientLocalNow, Name = "Кредиты", Number = 1, Version = 1 }; _repository.Create(flow); mustSave = true; } if (mustSave) { await _repository.SaveChangesAsync().ConfigureAwait(false); } var bill = new ExpenseBillModel { DateTime = _timeService.ClientLocalNow, OwnerId = _currentSession.UserId, AccountId = account.Id, ExpenseFlowId = flow.Id, IsCorection = true, }; var item = new ExpenseItemModel { CategoryId = category.Id, ProductId = product.Id, Cost = reduceAmount }; bill.AddItem(item); return(await _expensesBillCommands.Create(bill, true).ConfigureAwait(false)); }