public async Task <IActionResult> OnPostAsync() { return(await Expense.ProcessAsync(ModelState, nameof(Expense), async() => { await _expenseFlowCommands.AddExpense(Expense.ToModel()); return RedirectToPage(Expense.ReturnPage); }, async() => { await PrepareModels(Expense.FlowId); return Page(); }, async vrList => { if (!Expense.Account.IsNullOrEmpty()) { var account = await _accountQueries.GetByName(Expense.Account); if (account == null) { vrList.Add(new ModelValidationResult(nameof(Expense.Account), "Нет такого счета")); } } if (!Expense.FlowName.IsNullOrEmpty()) { var flow = await _expenseFlowQueries.GetByName(Expense.FlowName); if (flow == null) { vrList.Add(new ModelValidationResult(nameof(Expense.FlowName), "Нет такой статьи расхода")); } else { Expense.FlowId = flow.Id; } } CategoryModel category = null; if (!string.IsNullOrEmpty(Expense.Category)) { category = await _categoriesQueries.GetFlowCategoryByName(Expense.FlowId, Expense.Category); if (category == null) { if (Expense.Category == Expense.CategoryToAdd) { category = await _categoriesCommands.CreateNewOrBind( Expense.FlowId, Expense.Category); Expense.CategoryToAdd = null; } else { vrList.Add(new ModelValidationResult(nameof(Expense.Category), "Нет такой категории, добавить ее?")); Expense.CategoryToAdd = Expense.Category; } } } if (!string.IsNullOrEmpty(Expense.Product)) { var product = await _productQueries.GetFlowProductByName(Expense.FlowId, Expense.Product); if (product == null) { if (Expense.Product == Expense.ProductToAdd && category != null) { await _productCommands.AddProductToCategory(category.Id, Expense.Product); Expense.ProductToAdd = null; } else { vrList.Add(new ModelValidationResult(nameof(Expense.Product), "Нет такого товара, добавить его?")); Expense.ProductToAdd = Expense.Product; } } } })); }
public async Task <IActionResult> OnPostAddAsync() { return(await Good.ProcessAsync(ModelState, nameof(Good), async() => { await PrepareModelsAsync(Good.FlowId); Bill = JsonConvert.DeserializeObject <ExpenseBillModel>(Good.Bill); Bill.AddItem(await GetExpenseItem()); Good.ClearInput(); Good.Bill = JsonConvert.SerializeObject(Bill); return Page(); }, async() => { await PrepareModelsAsync(Good.FlowId); Bill = JsonConvert.DeserializeObject <ExpenseBillModel>(Good.Bill); return Page(); }, async vrList => { if (Good.Account.IsNullOrEmpty()) { vrList.Add(new ModelValidationResult(nameof(Good.Account), "Укажите счет")); } else { var account = await _accountQueries.GetByName(Good.Account); if (account == null) { vrList.Add(new ModelValidationResult(nameof(Good.Account), "Нет такого счета")); } } if (Good.FlowName.IsNullOrEmpty()) { vrList.Add(new ModelValidationResult(nameof(Good.FlowName), "Укажите статью расходов")); return; } var flowId = await _expenseFlowQueries.GetIdByName(Good.FlowName); if (flowId == null) { vrList.Add(new ModelValidationResult(nameof(Good.FlowName), "Нет такой статьи расходов")); return; } Good.FlowId = flowId.Value; CategoryModel category = null; if (!Good.Category.IsNullOrEmpty()) { category = await _categoriesQueries.GetFlowCategoryByName(Good.FlowId, Good.Category); if (category == null) { if (Good.Category == Good.CategoryToAdd) { category = await _categoriesCommands.CreateNewOrBind(Good.FlowId, Good.Category); Good.CategoryToAdd = null; } else { vrList.Add(new ModelValidationResult(nameof(Good.Category), "Нет такой категории, добавить ее?")); Good.CategoryToAdd = Good.Category; } } } if (!Good.Product.IsNullOrEmpty()) { var product = await _productQueries.GetFlowProductByName(Good.FlowId, Good.Product); if (product == null) { if (Good.Product == Good.ProductToAdd && category != null) { await _productCommands.AddProductToCategory(category.Id, Good.Product); Good.ProductToAdd = null; } else { vrList.Add(new ModelValidationResult(nameof(Good.Product), "Нет такого товара, добавить его?")); Good.ProductToAdd = Good.Product; } } } })); }