Exemplo n.º 1
0
        public int IncrementProductStock(Product product, int count)
        {
            if (product == null)
                throw new ArgumentNullException("product");
            if (count <= 0)
                throw new ArgumentException("Count must be greater than zero");
            if (string.IsNullOrEmpty(product.Id))
                throw new ArgumentException("Product must have an Id");

            rwlProductStock.EnterWriteLock();
            try
            {
                if (!productStock.ContainsKey(product))
                {
                    productStock.Add(product, count);
                }
                else
                {
                    productStock[product] += count;
                }

                return productStock[product];
            }
            finally
            {
                rwlProductStock.ExitWriteLock();
            }
        }
Exemplo n.º 2
0
        public IEnumerable<Product> Get()
        {
            var fruits = new Category("Fruits");
            var veggies = new Category("Veggies");

            var apple = new Product("apple");
            apple.Categories = new List<Category>() { fruits };

            var potato = new Product("Potatoes");
            potato.Categories = new List<Category>() { veggies };

            var banana = new Product("Banana");
            banana.Categories = new List<Category>() { fruits };

            List<Product> list = new List<Product>(){
              apple, potato, banana
            };

            return list.AsQueryable<Product>();
        }
Exemplo n.º 3
0
        public int DecrementProductStock(Product product, int count)
        {
            if (product == null)
                throw new ArgumentNullException("product");
            if (count <= 0)
                throw new ArgumentException("Count must be greater than zero");

            rwlProductStock.EnterWriteLock();
            try
            {
                if (!this.productStock.ContainsKey(product))
                    throw new ArgumentException("Product " + product + " is not in stock");
                if (count > this.productStock[product])
                    throw new ArgumentException("Requested count exceeds available stock; Requested: " + count + ", available: " + this.productStock[product]);

                this.productStock[product] -= count;

                return this.productStock[product];
            }
            finally
            {
                rwlProductStock.ExitWriteLock();
            }
        }