public ActionResult Add(string Add, string Back, ProductTemp pr)
        {
            var btn = Add ?? Back;

            if (btn == Back)
            {
                return(RedirectToAction("Index"));
            }
            else
            {
                Product temp = new Product
                {
                    Category = pr.Category, Amount = pr.Amount, CreationDate = DateTime.Now, Brand = pr.Brand
                };
                if (db.Products.Contains(temp))
                {
                    return(BadRequest());
                }

                if (!db.Brands.Contains(pr.Brand))
                {
                    db.Brands.Add(pr.Brand);
                }

                db.Products.Add(temp);
                db.SaveChanges();

                return(RedirectToAction("Index"));
            }
        }
        public async Task <ActionResult <OrderReturn> > PostSalesDocument(OrderModel order)
        {
            if (order == null)
            {
                return(NotFound());
            }
            ;

            var _salesDocuments = new SalesDocuments();
            var _customer       = new Customers();
            var _address        = new Address();
            var _basket         = new SalesDocumentsProduct();
            var _product        = new Product();
            var _productTemp    = new ProductTemp();

            if (order.CustomerData != null)
            {
                _customer.Name        = order.CustomerData.Name;
                _customer.Surname     = order.CustomerData.Surname;
                _customer.PhoneNumber = order.CustomerData.PhoneNumber;
                _customer.Email       = order.CustomerData.Email;

                _context.Customers.Add(_customer);
                await _context.SaveChangesAsync();

                _salesDocuments.Customer = _customer.CustomerId;
            }
            else
            {
                _salesDocuments.Customer = null;
            }

            if (order.AddressData != null)
            {
                _address.Zipcode         = order.AddressData.Zipcode;
                _address.City            = order.AddressData.City;
                _address.Street          = order.AddressData.Street;
                _address.BuildingNumber  = order.AddressData.BuildingNumber;
                _address.ApartmentNumber = order.AddressData.ApartmentNumber;

                _context.Address.Add(_address);
                await _context.SaveChangesAsync();

                _salesDocuments.CustomerAddress = _address.AddressId;
            }
            else
            {
                _salesDocuments.CustomerAddress = null;
            }

            int documentNumber = await _context.SalesDocuments.Where(p => p.WarehouseId == order.WarehouseID && p.DocumentTypes == order.DocumentType)
                                 .Select(p => p.DocumentNumber)
                                 .LastAsync();

            _salesDocuments.DocumentTypes  = order.DocumentType;
            _salesDocuments.WarehouseId    = order.WarehouseID;
            _salesDocuments.DocumentNumber = documentNumber + 1;
            _salesDocuments.BasketId       = order.BasketID;
            _salesDocuments.UserId         = order.UserID;
            _salesDocuments.CreationDate   = DateTime.Now;
            _salesDocuments.OrderDate      = DateTime.Now;

            _context.SalesDocuments.Add(_salesDocuments);

            await _context.SaveChangesAsync();


            foreach (var product in order.Products)
            {
                var productID = await _context.Product.Where(p => p.ProductId == product.ProductId)
                                .Select(p => p.ProductId)
                                .AnyAsync();

                if (productID == true)
                {
                    _basket.SalesDocuments = _salesDocuments.SalesDocumentId;
                    _basket.Product        = product.ProductId;

                    _context.SalesDocumentsProduct.Add(_basket);
                    await _context.SaveChangesAsync();
                }
                else
                {
                    _product.ProductId   = product.ProductId;
                    _product.Description = product.Description;
                    _product.Quantity    = product.Quantity;
                    _product.Price       = product.Price;
                    _product.Punctation  = product.Punctation;

                    _context.Product.Add(_product);

                    _context.Database.OpenConnection();
                    try
                    {
                        _context.Database.ExecuteSqlCommand("SET IDENTITY_INSERT dbo.Product ON");
                        await _context.SaveChangesAsync();

                        _context.Database.ExecuteSqlCommand("SET IDENTITY_INSERT dbo.Product OFF");
                    }
                    finally
                    {
                        _context.Database.CloseConnection();
                    }

                    _basket.SalesDocuments = _salesDocuments.SalesDocumentId;
                    _basket.Product        = product.ProductId;

                    _context.SalesDocumentsProduct.Add(_basket);
                    await _context.SaveChangesAsync();
                }
            }

            var documentType = await _context.DocumentTypes.Where(p => p.DocumentTypeId == _salesDocuments.DocumentTypes)
                               .Select(p => p.Type)
                               .LastAsync();

            return(Ok(new OrderReturn {
                Warehouse = _salesDocuments.WarehouseId,
                DocumentType = documentType,
                DocumentNumber = _salesDocuments.DocumentNumber
            }));
        }
Exemple #3
0
        private async static void digikala_7_AddProductToMongo()
        {
            for (short i = 0; i < 120; i++)
            {
                List <DigikalaPageBaseDTO> getAll = new List <DigikalaPageBaseDTO>();
                List <BasePage>            pages  = new List <BasePage>();
                using (IWebsiteCrawler digikala = new DigikalaHelper())
                {
                    getAll = (await digikala.GetAllBasePage <GetAllBasePageDigikalaResult>()).BasePages;
                    Console.WriteLine($"list total {getAll.Count()}");
                    pages = getAll.Select(x => new BasePage
                    {
                        Id  = x._id,
                        Loc = x.Loc
                    }).ToList();
                    getAll.Clear();
                }

                long  x          = 0;
                short errorCount = 0;
                foreach (var item in pages)
                {
                    try
                    {
                        var _s = DateTime.Now;
                        DigikalaProductDTO product = null;
                        using (IWebsiteCrawler digikala = new DigikalaHelper())
                        {
                            product = await digikala.GetProduct <DigikalaProductDTO>(item.Loc);
                        }

                        if (product == null)
                        {
                            int dkp = getDKPWithUrl(item.Loc);
                            Console.WriteLine($"Try Again , DKP - {dkp} Wait: {1000} Secs");
                            System.Threading.Thread.Sleep(1000 * errorCount);
                            using (IWebsiteCrawler digikala = new DigikalaHelper())
                            {
                                product = await digikala.GetProduct <DigikalaProductDTO>(item.Loc);
                            }
                        }
                        var _t = Math.Round((DateTime.Now - _s).TotalSeconds, 2);
                        if (product != null)
                        {
                            ProductTemp prd = new ProductTemp();
                            prd.BasePage        = item;
                            prd.DigikalaProduct = product;
                            digikala_SaveProductBatch(prd);
                            Console.WriteLine($"S{i},   {++x}  =  DKP-{product.DKP}    , Comment={(product.Comments != null ? product.Comments.Count() + "+  " : "0  ")} ,  in {_t} Secs ");
                            if (x % 5 == 0)
                            {
                                Console.WriteLine("--------------");
                                System.Threading.Thread.Sleep(100);
                            }
                            if (x % 100 == 0)
                            {
                                System.Threading.Thread.Sleep(500);
                                Console.Clear();
                            }
                            errorCount = 0;
                        }
                        else
                        {
                            if (errorCount < 3)
                            {
                                errorCount += 1;
                            }
                            int dkp = getDKPWithUrl(item.Loc);
                            Console.WriteLine($"{++x} = DKP-{dkp} , Wait: {1000 * errorCount} Secs ,  *** Error *** ,");
                            System.Threading.Thread.Sleep(1000 * errorCount);
                        }
                    }
                    catch (Exception ex)
                    {
                        if (errorCount < 3)
                        {
                            errorCount += 1;
                        }
                        int dkp = getDKPWithUrl(item.Loc);
                        Console.WriteLine($"{++x} = DKP-{dkp} , Wait: {1000 * errorCount} Secs , *** Error ***   Problem");
                        using (ILoger Logger = new MongoDBLoggerHelper())
                        {
                            LogDTO log = new LogDTO()
                            {
                                _id         = ObjectId.GenerateNewId().ToString(),
                                DateTime    = DateTime.Now,
                                Description = ex.Message.ToString(),
                                ProjectId   = (int)ProjectNames.Console,
                                Url         = item.Loc,
                                MethodName  = "Digikala - Console App",
                                Title       = "Get Product Error - " + dkp
                            };
                            Logger.AddLog(log);
                        }
                        System.Threading.Thread.Sleep(1000 * errorCount);
                    }
                }
            }
            digikala_SaveProductBatch(null, true);
        }