예제 #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
 /// <summary>
 /// changes rest object to data object
 /// </summary>
 /// <param name="item"></param>
 /// <returns></returns>
 public static Assignment2.Models.Cart ToDataModel(this REST.RESTCart item)
 {
     return(new Assignment2.Models.Cart {
         CustomerID = item.CustomerID,
         ProductID = item.ProductID,
         StoreID = item.StoreID,
         Quantity = item.Quantity
     });
 }
예제 #3
0
        public static FormUrlEncodedContent ToHttpBody(this REST.RESTCart item)
        {
            var pairs = new List <KeyValuePair <string, string> >();

            pairs.Add(new KeyValuePair <string, string>("CustomerID", item.CustomerID));
            pairs.Add(new KeyValuePair <string, string>("ProductID", item.ProductID.ToString()));
            pairs.Add(new KeyValuePair <string, string>("StoreID", item.StoreID.ToString()));
            pairs.Add(new KeyValuePair <string, string>("Quantity", item.Quantity.ToString()));

            var content = new FormUrlEncodedContent(pairs);

            return(content);
        }
예제 #4
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;
            }
        }