Пример #1
0
        public async Task <ActionResult <ProductDelivery> > PostProductDelivery(ProductDelivery productDelivery)
        {
            _context.ProductDeliveries.Add(productDelivery);
            await _context.SaveChangesAsync();

            return(CreatedAtAction("GetProductDelivery", new { id = productDelivery.Id }, productDelivery));
        }
Пример #2
0
        public async Task <IActionResult> PutProductDelivery(int id, ProductDelivery productDelivery)
        {
            if (id != productDelivery.Id)
            {
                return(BadRequest());
            }

            _context.Entry(productDelivery).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!ProductDeliveryExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
Пример #3
0
        private void ParseProductDeliveryInfo(Product product)
        {
            object lockObject = new object();

            if (product.Availability == ProductAvailability.OutStock)
            {
                return;
            }

            ConcurrentBag <ProductDelivery> productDeliveries = new ConcurrentBag <ProductDelivery>();

            var token = SouqApi.GetProductAccessTokens(product.Url.ExtractProductFullId())["searchForm"];

            int errorCount = 0;
            int tryCount   = cities.Count;
            ManualResetEvent manualResetEvent = new ManualResetEvent(false);
            AutoResetEvent   autoResetEvent   = new AutoResetEvent(false);
            bool             validToken       = true;

            //foreach(var city in cities)
            Parallel.ForEach(cities, (city) =>
            {
                retoken:
                SouqDeliveryInfo deliveryInfo;
                try
                {
                    deliveryInfo = SouqApi.GetDeliveryInfo(city.Code, city.Name, product.UnitId, token.hitsCfs,
                                                           token.hitsCfsMeta);
                    --tryCount;
                }
                catch (Exception ex)
                {
                    ++errorCount;
                    validToken = false;

                    if (errorCount > 0 && tryCount != errorCount)
                    {
                        manualResetEvent.WaitOne();
                    }
                    else if (errorCount > 0 && tryCount == errorCount)
                    {
                        autoResetEvent.Set();
                        manualResetEvent.Set();
                    }

                    autoResetEvent.WaitOne();

                    if (!validToken)
                    {
                        errorCount = 0;
                        manualResetEvent.Reset();
                        token      = SouqApi.GetProductAccessTokens(product.Url.ExtractProductFullId())["searchForm"];
                        validToken = true;
                    }

                    autoResetEvent.Set();
                    goto retoken;
                }

                var canDeliver = deliveryInfo.estimate_by_days != null;
                var daysNo     = deliveryInfo.estimate_by_days;

                ProductDelivery delivery = new ProductDelivery()
                {
                    City          = city,
                    Id            = Guid.NewGuid(),
                    EstimatedDays = daysNo,
                    CanDeliver    = canDeliver
                };

                productDeliveries.Add(delivery);
            });

            product.Deliveries = productDeliveries.ToList();
        }