Пример #1
0
 public async Task <JsonResult> OnPostGetFlowCategoriesAsync()
 {
     return(await this.ProcessAjaxPostRequestAsync(async name =>
     {
         var flow = await _expenseFlowQueries.GetByName(name);
         if (flow == null)
         {
             return new { flowId = 0, categories = Enumerable.Empty <string>() }
         }
         ;
         var categories = await _categoriesQueries.GetFlowCategories(flow.Id);
         return new { flowId = flow.Id, categories = categories.Select(x => x.Name) };
     }));
 }
Пример #2
0
        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;
                        }
                    }
                }
            }));
        }