Exemple #1
0
        public async Task <bool> Do(Request request)
        {
            //service responsibility
            if (!_stockManager.EnoughStock(request.StockId, request.Quantity))
            {
                return(false);
            }

            await _stockManager
            .PutSockOnHold(request.StockId, request.Quantity, _sessionManager.GetId());

            var stock = _stockManager.GetStockWithProduct(request.StockId);

            var cartProduct = new CartProduct()
            {
                ProductId   = stock.Product.Id,
                ProductName = stock.Product.Name,
                StockId     = stock.Id,
                Quantity    = request.Quantity,
                Price       = stock.Product.Price,
                PhotoUrl    = stock.Product.PhotoUrl
            };

            _sessionManager.AddProduct(cartProduct);

            return(true);
        }
Exemple #2
0
        public async Task <bool> Do(Request request)
        {
            if (!_stockManager.EnoughStock(request.StockId, request.Qty))
            {
                return(false);
                //notEnough stock
            }

            await _stockManager.PutStockOnHold(request.StockId, request.Qty, _sessionManager.GetId());

            var stock       = _stockManager.GetStockProduct(request.StockId);
            var cartProduct = new CartProduct()
            {
                ProductName = stock.Product.Name,
                ProductId   = stock.ProductId,
                StockId     = stock.Id,
                Qty         = request.Qty,
                TheValue    = stock.Product.Value,
                ImgUrl      = stock.Product.ImgUrl
            };

            _sessionManager.AddProduct(cartProduct);

            return(true);
        }
Exemple #3
0
        public async Task <bool> Do(Request request)
        {
            if (!_stockManager.EnoughStock(request.StockId, request.Qty))
            {
                throw new System.Exception("Not enough stock.");
            }

            await _stockManager.PutStockOnHold(request.StockId, request.Qty, _sessionManager.GetId());

            var stock = _stockManager.GetStockWithProduct(request.StockId);

            var cartProduct = new CartProduct()
            {
                ProductId   = stock.ProductId,
                StockId     = stock.Id,
                Qty         = request.Qty,
                ProductName = stock.Product.Name,
                Value       = stock.Product.Value,
                Image       = stock.Product.PrimaryImage,
                Description = stock.Description
            };

            _sessionManager.AddProduct(cartProduct);

            return(true);
        }
Exemple #4
0
        public async Task <bool> Do(Request request)
        {
            // Episode 28 - Stock Race Condition
            // Service responsability
            if (!_stockManager.EnoughStock(request.StockId, request.Qty))
            {
                return(false);
            }

            await _stockManager
            .PutStockOnHold(request.StockId, request.Qty, _sessionManager.GetId());

            var stock = _stockManager.GetStockWithProduct(request.StockId);

            var cartProduct = new CartProduct()
            {
                ProductId   = stock.ProductId,
                ProductName = stock.Product.Name,
                StockId     = stock.Id,
                Qty         = request.Qty,
                Value       = stock.Product.Value,
            };

            _sessionManager.AddProduct(cartProduct);

            return(true);
        }
Exemple #5
0
        public async Task <bool> ExecAsync(CartDto cartDto)
        {
            if (!_stockManager.EnoughStock(cartDto.StockId, cartDto.Quantity))
            {
                return(false);
            }

            var success =
                await _stockManager.PutStockOnHold(cartDto.StockId, cartDto.Quantity, _sessionManager.GetId()) > 0;

            if (!success)
            {
                return(false);
            }

            var stock = _stockManager.GetStockWithProduct(cartDto.StockId);

            if (stock == null)
            {
                return(false);
            }

            var cartProduct = new CartProduct
            {
                ProductId   = stock.ProductId,
                ProductName = stock.Product.Name,
                StockId     = stock.Id,
                Quantity    = cartDto.Quantity,
                Value       = stock.Product.Value
            };

            _sessionManager.AddProduct(cartProduct);

            return(true);
        }
Exemple #6
0
        public async Task <bool> Do(Request request)
        {
            if (request.Qty <= 0)
            {
                return(false);
            }

            if (!await _stockManager.EnoughStock(request.StockId, request.Qty))
            {
                return(false);
            }

            await _stockManager.PutStockOnHold(request.StockId, request.Qty, _sessionManager.GetId());

            var stock = await _stockManager.GetStock(request.StockId);

            var product = await _productManager.GetProductById(stock.ProductId);

            var images = await _productImageManager.GetImages(stock.ProductId);

            var cartProduct = new CartProduct
            {
                ProductId   = stock.ProductId,
                ProductName = product.Name,
                StockId     = stock.Id,
                Images      = images.Select(x => x.Path).ToList(),
                Qty         = request.Qty,
                Value       = product.Value
            };

            _sessionManager.AddProduct(cartProduct);

            return(true);
        }
        public async Task <bool> Do(Request request)
        {
            if (!_stockManager.EnoughStock(request.StockID, request.Qty))
            {
                return(false);
            }
            if (request.Qty <= 0)
            {
                return(false);
            }

            await _stockManager
            .PutStockOnHold(request.StockID, request.Qty, _sessionManager.GetId());

            var stock = _stockManager.GetStockWithProduct(request.StockID);

            var cartProduct = new CartProduct()
            {
                ProductID   = stock.ProductID,
                StockID     = stock.ID,
                Qty         = request.Qty,
                ProductName = stock.Product.Name,
                Value       = stock.Product.Value
            };

            _sessionManager.AddProduct(cartProduct);

            return(true);
        }
Exemple #8
0
        public async Task <bool> DoAsync(Request request)
        {
            #region Stock Race

            //var stockToHold = _context.Stock.Where(x => x.Id == request.StockId).FirstOrDefault();
            //if (stockToHold.Quantity < request.Quantity)
            //{
            //    return false;
            //}

            //var stockOnHold = _context.StocksOnHold.Where(x => x.SessionId == _sessionManager.GetId()).ToList();
            //if (stockOnHold.Any(x => x.StockId == request.StockId))
            //{
            //    stockOnHold.Find(x => x.StockId == request.StockId).Quantity += request.Quantity;
            //}
            //else
            //{
            //    _context.StocksOnHold.Add(new StockOnHold
            //    {
            //        StockId = request.StockId,
            //        SessionId = _sessionManager.GetId(),
            //        Quantity = request.Quantity,
            //        ExpiryDate = DateTime.Now.AddMinutes(20)
            //    });
            //}

            //foreach (var stock in stockOnHold)
            //{
            //    stock.ExpiryDate = DateTime.Now.AddMinutes(20);
            //}

            //stockToHold.Quantity = stockToHold.Quantity - request.Quantity;

            //await _context.SaveChangesAsync();

            #endregion

            var product = _productManager.GetProductById(request.ProductId);

            var cartPorduct = new CartProduct()
            {
                ProductId   = product.Id,
                StockId     = product.Id,
                Quantity    = request.Quantity,
                ProductName = product.Name,
                Description = product.Description,
                Seller      = product.Distributor.DistributorName,
                Category    = product.Category.CategoryName,
                Producer    = product.Producer.ProducerName,
                SourceUrl   = product.SourceUrl,
                Value       = product.Value
            };

            await Task.Run(() => _sessionManager.AddProduct(cartPorduct));

            return(true);
        }
        public async Task <bool> Do(Request request)
        {
            //service responsibility
            if (!_stockManager.EnoughStock(request.StockId, request.Qty))
            {
                return(false);
            }


            await _stockManager
            .PutStockOnHold(request.StockId, request.Qty, _sessionManager.GetId());

            var stock = _stockManager.GetStockWithProduct(request.StockId);

            var cartProduct = new CartProduct()
            {
                ProductId   = stock.ProductId,
                StockId     = stock.Id,
                Qty         = request.Qty,
                ProductName = stock.Product.Name,
                Value       = stock.Product.Value,
            };

            _sessionManager.AddProduct(cartProduct);
            // var cartList = new List<CartProduct>();
            //  var stringObject = _session.GetString("cart");
            //  if (!string.IsNullOrEmpty(stringObject))
            //  {
            //      cartList = JsonConvert.DeserializeObject<List<CartProduct>>(stringObject);
            //  }
            //
            //  if (cartList.Any(x => x.StockId == request.StockId))
            //  {
            //      cartList.Find(x => x.StockId == request.StockId).Qty += request.Qty;
            //  }
            //  else
            //  {
            //      cartList.Add(new CartProduct
            //      {
            //          StockId = request.StockId,
            //          Qty = request.Qty
            //      });
            //  }
            //
            //      stringObject = JsonConvert.SerializeObject(cartList);
            //
            //      _session.SetString("cart", stringObject);
            return(true);
        }
Exemple #10
0
        public async Task <bool> Do(Request request)
        {
            var stockOnHold = _ctx.StocksOnHold.Where(x => x.SessionId == _sessionManager.GetId()).ToList();
            var stockToHold = _ctx.Stock.Where(x => x.Id == request.StockId).FirstOrDefault();



            if (stockToHold.Qty < request.Qty)
            {
                //return not enough stock
                return(false);
            }

            if (stockOnHold.Any(x => x.StockId == request.StockId))
            {
                stockOnHold.Find(x => x.StockId == request.StockId).Qty += request.Qty;
            }
            else
            {
                _ctx.StocksOnHold.Add(new StockOnHold
                {
                    StockId    = stockToHold.Id,
                    SessionId  = _sessionManager.GetId(),
                    Qty        = stockToHold.Qty,
                    ExpiryDate = DateTime.Now.AddMinutes(20)
                });
            }


            stockToHold.Qty = stockToHold.Qty - request.Qty;

            foreach (var stock in stockOnHold)
            {
                stock.ExpiryDate = DateTime.Now.AddMinutes(20);
            }
            await _ctx.SaveChangesAsync();

            _sessionManager.AddProduct(request.StockId, request.Qty);
            // var cartList = new List<CartProduct>();
            //  var stringObject = _session.GetString("cart");
            //  if (!string.IsNullOrEmpty(stringObject))
            //  {
            //      cartList = JsonConvert.DeserializeObject<List<CartProduct>>(stringObject);
            //  }
            //
            //  if (cartList.Any(x => x.StockId == request.StockId))
            //  {
            //      cartList.Find(x => x.StockId == request.StockId).Qty += request.Qty;
            //  }
            //  else
            //  {
            //      cartList.Add(new CartProduct
            //      {
            //          StockId = request.StockId,
            //          Qty = request.Qty
            //      });
            //  }
            //
            //      stringObject = JsonConvert.SerializeObject(cartList);
            //
            //      _session.SetString("cart", stringObject);
            return(true);
        }