/// <summary>
        /// Saves a new Product
        /// </summary>
        /// <param name="product">JSON object containing the Product data</param>
        /// <returns>Number of records affected</returns>
        /// <exception cref="InvalidOperationException">Throws an exception when there is already a Product with a given Product ID</exception>
        public int SaveProduct(JSON_Models.Product product)
        {
            //Obtaining the Model to be saved
            Models.Product productModel = new Models.Product()
            {
                ProductNumber = int.Parse(product.ProductNumber),
                Title         = product.Title,
                Price         = decimal.Parse(product.Price),
                DateCreated   = DateTime.Parse(product.DateCreated)
            };

            int  records      = 0;
            bool productExist = false;

            using (var model = new WebshopEntities())
            {
                productExist = model.Products.Any(p => p.ProductNumber == productModel.ProductNumber);
                if (!productExist)
                {
                    model.Products.Add(productModel);
                    records = model.SaveChanges();
                }
                else
                {
                    throw new InvalidOperationException(string.Format("There is already a Product with Product Number: {0}", productModel.ProductNumber));
                }
            }

            return(records);
        }
예제 #2
0
 // POST api/Menu
 public void Post([FromBody] Menu menu)
 {
     database.Menu.Add(menu);
     database.SaveChanges();
 }
예제 #3
0
 // POST api/Product
 public void Post([FromBody] Product product)
 {
     database.Product.Add(product);
     database.SaveChanges();
 }