コード例 #1
0
        public List <REST.RESTCart> Put(REST.RESTCart data)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    _context.Update(data.ToDataModel());
                    _context.SaveChanges();
                }
                else
                {
                    throw new Exception("Invalid input provided");
                }

                var items    = _context.Carts.Where(c => c.CustomerID == data.CustomerID).ToList();
                var response = new List <REST.RESTCart>();
                foreach (var item in items)
                {
                    response.Add(item.ToRest());
                }

                return(response);
            }
            catch (Exception e)
            {
                throw e;
            }
        }
コード例 #2
0
        public List <REST.RESTCart> Post(REST.RESTCart data)
        {
            try
            {
                if (data.Quantity < 1)
                {
                    throw new Exception("Invalid quantity provided");
                }

                if (ModelState.IsValid)
                {
                    // check if item exists
                    var exists = _context.Carts.Where(c => c.ProductID == data.ProductID)
                                 .Where(c => c.StoreID == data.StoreID)
                                 .Where(c => c.CustomerID == data.CustomerID).FirstOrDefault();

                    var quantity = 0;

                    if (exists == null)
                    {
                        quantity = data.Quantity;
                        _context.Add(data.ToDataModel());
                    }
                    else
                    {
                        exists.Quantity += data.Quantity;

                        quantity = exists.Quantity;
                        _context.Update(exists);
                    }

                    // check the stock level
                    var storeInventory = _context.StoreInventories.Where(c => c.StoreID == data.StoreID).Where(c => c.ProductID == data.ProductID).FirstOrDefault();
                    if (storeInventory == null)
                    {
                        throw new Exception("Invalid Item chosen");
                    }
                    else
                    {
                        if (quantity > storeInventory.StockLevel)
                        {
                            throw new Exception("Insufficient stock");
                        }
                        _context.SaveChanges();
                    }
                }
                else
                {
                    throw new Exception("Invalid input provided");
                }
                var items    = _context.Carts.Include(c => c.Product).Include(c => c.Store).Where(c => c.CustomerID == data.CustomerID).ToList();
                var response = new List <REST.RESTCart>();
                foreach (var item in items)
                {
                    response.Add(item.ToRest());
                }

                return(response);
            }
            catch (Exception e)
            {
                throw e;
            }
        }