Example #1
0
        private async Task <bool> ShowAddProductToCategoryDialogAsync(IEnumerable <ProductVM> products,
                                                                      bool addFromSearch = false)
        {
            var productVms          = products as IList <ProductVM> ?? products.ToList();
            var addToCategoryDialog = new AddToCategoryWindow()
            {
                DataContext = this, Products = productVms.ToList()
            };

            if (addToCategoryDialog.ShowDialog() == true)
            {
                if (!addFromSearch && SelectedCategory != null)
                {
                    bool sameCategory = addToCategoryDialog.SelectedCategories.Count == 1 &&
                                        addToCategoryDialog.SelectedCategories[0].Model.Id ==
                                        SelectedCategory.Model.Id;
                    if (sameCategory)
                    {
                        return(false);
                    }
                }


                foreach (var category in addToCategoryDialog.SelectedCategories)
                {
                    await ProductManagerVM.AddProductToCategoryAsync(category, productVms).ConfigureAwait(false);
                }
                return(true);
            }
            return(false);
        }
Example #2
0
 public CategoryVM(Category model)
 {
     Model                       = model;
     ProductManager              = new ProductManagerVM(Products);
     Moved                      += CategoryMovedHandler;
     Products.CollectionChanged += (sender, args) => RefreshProductsCount();
     RefreshProductsCount();
 }
Example #3
0
 public CategoryManagerVM(RangeObservableCollection <ProductVM> searchColl)
 {
     _searchColl                    = searchColl;
     _selectedCategory              = null;
     _searchColl.CollectionChanged += SearchCollOnCollectionChanged;
     ProductManager                 = new ProductManagerVM(_searchColl);
     GetCategoriesFromDb().Wait();
 }
Example #4
0
        private async void AddProductFromUrl()
        {
            var category = SelectedCategory;
            var dialog   = new AddProductFromUrlWindow();

            if (dialog.ShowDialog() == true)
            {
                string  url          = dialog.Url;
                Product addedProduct = null;

                try
                {
                    var addedProductId = Products.ParseId(url);

                    if (category.Products.Any(p => p.Model.Id == addedProductId))
                    {
                        await MessageBox.ShowAsync("Данная складчина уже была добавлена ранее.", "Информация",
                                                   MessageBoxButton.OK,
                                                   MessageBoxImage.Information).ConfigureAwait(false);

                        return;
                    }

                    Product dbProduct;
                    using (var context = new DbContext())
                    {
                        dbProduct = await context.Products.AsNoTracking().Where(x => x.Id == addedProductId)
                                    .Include(p => p.Prefix)
                                    .Include(p => p.Chapter)
                                    .Include(p => p.Subсhapter)
                                    .Include(p => p.Creator)
                                    .Include(p => p.Organizer)
                                    .FirstOrDefaultAsync().ConfigureAwait(false);
                    }

                    if (dbProduct != null)
                    {
                        addedProduct = dbProduct;
                    }
                    else
                    {
                        var  skApi           = new SkladchikApiClient(new CancellationToken());
                        bool isNeedGetCookie = await Cookies.IsNeedGetCookie().ConfigureAwait(false);

                        if (isNeedGetCookie)
                        {
                            var cookieContainer = Cookies.GetSkladchikCookie();
                            skApi.CookieContainer = cookieContainer;
                            if (cookieContainer.Count == 0)
                            {
                                await MessageBox.ShowAsync("Добавление складчины невозможно. Загрузите сайт в браузере и попробуйте заново.", "Ошибка",
                                                           MessageBoxButton.OK, MessageBoxImage.Error).ConfigureAwait(false);

                                return;
                            }
                        }



                        addedProduct = await skApi.Products.GetByUrlAsync(url).ConfigureAwait(false);
                    }
                }
                catch (System.Exception)
                {
                    // ignored
                }

                if (addedProduct == null)
                {
                    await MessageBox.ShowAsync("Ошибка добавления. Проверьте указанный URL.", "Ошибка",
                                               MessageBoxButton.OK,
                                               MessageBoxImage.Error).ConfigureAwait(false);

                    return;
                }

                ProductVM productVm;
                if (_allProducts.ContainsKey(addedProduct.Id))
                {
                    productVm = _allProducts[addedProduct.Id];
                }
                else
                {
                    productVm = new ProductVM(addedProduct);
                    _allProducts.Add(addedProduct.Id, productVm);
                }

                await Db.Products.AddOrUpdateAsync(new List <Product> {
                    addedProduct
                }).ConfigureAwait(false);

                await Db.Categories.AddProductsAsync(category.Model, new List <Product> {
                    addedProduct
                })
                .ConfigureAwait(false);

                await ProductManagerVM.AddProductToCategoryAsync(category, new List <ProductVM> {
                    productVm
                })
                .ConfigureAwait(false);
            }
        }