Exemplo n.º 1
0
        private void AddBarcode()
        {
            try
            {
                var barcode = new Barcode {Barcode1 = Barcode};

                var productWithThisBarcode = productsViewModel.AllProducts.FirstOrDefault(x => x.Barcodes.Any(y => y.Barcode1 == barcode.Barcode1));

                if (productWithThisBarcode != null)
                {
                    throw new ArgumentException($"Продуктът \"{productWithThisBarcode.Name}\" вече използва въведеният баркод.");
                }

                Product.Barcodes.Add(barcode);
                databaseHandlerService.AddBarcodeAsync(product, barcode);

                Barcode = null;

                productsViewModel.UpdateProduct(product);

                var notifySuccessEvent = eventAggregator.GetEvent<NotifyOnSuccessEvent>();
                notifySuccessEvent.Publish("Баркодът е добавен успешно.");
            }
            catch (Exception ex)
            {
                var notifyErrorEvent = eventAggregator.GetEvent<NotifyOnErrorEvent>();
                notifyErrorEvent.Publish(ex.Message);
            }
        }
        public void AddBarcodeAsync(Product originalProduct, Barcode barcode)
        {
            using (var worker = new BackgroundWorker())
            {
                worker.DoWork += (object sender, DoWorkEventArgs e) =>
                {
                    using (var context = new EugenieEntities(connectionStringAsString))
                    {
                        var product = context.Products.FirstOrDefault(x => x.ID == originalProduct.ID);

                        product.Barcodes.Add(barcode);
                        context.SaveChanges();
                    }
                };

                worker.RunWorkerAsync();
            }
        }