示例#1
0
        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;
                        }
                    }
                }
            }));
        }