示例#1
0
 public SearchVM()
 {
     _cts             = new CancellationTokenSource();
     _skApi           = new SkladchikApiClient(_cts.Token);
     StartCommand     = new RelayCommand(StartAsync);
     FiltersManagerVM = new FiltersManagerVM(FiltersManagerVM.TemporaryFiltersPath);
 }
示例#2
0
 public ProductParser(SkladchikApiClient skApi, CancellationToken token)
 {
     _token            = token;
     _skApi            = skApi;
     _productEventArgs = new ProductEventArgs()
     {
         Products = new List <Product>()
     };
 }
示例#3
0
        private async Task RefreshProductsAsync(IEnumerable <ProductVM> coll, CancellationTokenSource cts)
        {
            var  skApi           = new SkladchikApiClient(cts.Token);
            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);

                    cts.Cancel();
                    return;
                }
            }

            foreach (var productVm in coll)
            {
                cts.Token.ThrowIfCancellationRequested();
                var updatedProduct = await skApi.Products.GetByIdAsync(productVm.Model.Id)
                                     .ConfigureAwait(false);

                bool productWasDeleted = updatedProduct == null;
                if (productWasDeleted)
                {
                    await Db.Products.RemoveAsync(productVm.Model).ConfigureAwait(false);
                }
                else
                {
                    var oldProduct = productVm.Model;
                    updatedProduct.Color     = oldProduct.Color;
                    updatedProduct.Note      = oldProduct.Note;
                    updatedProduct.ViewCount = oldProduct.ViewCount;
                    productVm.Model          = updatedProduct;
                }
            }

            await Db.Products.AddOrUpdateAsync(coll.Select(p => p.Model).ToList()).ConfigureAwait(false);
        }
示例#4
0
 public Products(SkladchikApiClient skladchikApiClient)
 {
     _skladchikApiClient = skladchikApiClient;
 }
示例#5
0
 public Users(SkladchikApiClient skladchikApiClient)
 {
     _skladchikApiClient = skladchikApiClient;
 }
示例#6
0
 public Chapters(SkladchikApiClient skladchikApiClient)
 {
     _skladchikApiClient = skladchikApiClient;
 }
示例#7
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);
            }
        }