public IHttpActionResult Get(int id)
        {
            try
            {

                Product product;
                var productRepo = new ProductRepository();
                if (id > 0)
                {
                    var products = productRepo.Retrieve();
                    product = products.FirstOrDefault(p => p.ProductId == id);
                    if (product == null)
                    {
                        return NotFound();
                    }
                }
                else
                {
                    product = new Product();
                }

                return Ok(product);

            }
            catch (Exception ex)
            {
                return InternalServerError(ex);
            }
        }
 /// <summary>
 /// Creates a new product with default values.
 /// </summary>
 /// <returns></returns>
 internal Product Create()
 {
     Product product = new Product
     {
         ReleaseDate = DateTime.Now
     };
     return product;
 }
 public Product Update(Product product)
 {
     var original = GetById(product.ProductId);
     original.Description = product.Description;
     original.Price = product.Price;
     original.ProductCode = product.ProductCode;
     original.ProductName = product.ProductName;
     original.ReleaseDate = product.ReleaseDate;
     _db.SaveChanges();
     return original;
 }
 public void TestCreateNewProduct()
 {
     ProductsController ctrl = new ProductsController(_mockedRepository.Object);
     Product p = new Product();
     p.ProductName = "New Product";
     p.Description = "A new product description";
     p.Price = 100.00M;
     p.ProductCode = "XYZ 123";
     p.ReleaseDate = DateTime.Now;
     var savedProduct = ctrl.Post(p);
     Assert.IsNotNull(savedProduct);
 }
示例#5
0
        public Product Save(Product product)
        {
            var products = this.Retrieve();

            var maxId = products.Max(p => p.ProductId);
            product.ProductId = maxId + 1;
            products.Add(product);

            WriteDate(products);

            return product;
        }
        internal Product Save(Product product)
        {
            //Read in the existing products
            var products = this.Retrieve();

            //Assign a new Id
            var maxId = products.Max(p => p.ProductId);
            product.ProductId = maxId + 1;
            products.Add(product);

            WriteData(products);
            return product;
        }
        /// <summary>
        /// Saves a new product
        /// </summary>
        /// <param name="product"></param>
        /// <returns></returns>
        internal Product Save(Product product)
        {
            //Get products
            var products = this.Retrieve();

            //Assign a new ID
            var maxID = products.Max(p => p.ProductId);
            product.ProductId = maxID++;
            products.Add(product);

            WriteData(products);
            return product;

        }
示例#8
0
        public Product Save(int id, Product product)
        {
            var products = this.Retrieve();
            var itemIndex = products.FindIndex(p => p.ProductId == id);
            if (itemIndex > 0)
            {
                products[itemIndex] = product;
            }
            else
            {
                return null;
            }

            WriteDate(products);

            return product;
        }
        internal Product Save(int id, Product product)
        {
            var products = this.Retrieve();

            //Locate and replace item
            var itemIndex = products.FindIndex(p => p.ProductId == id);
            if (itemIndex > 0)
            {
                products[itemIndex] = product;
            }
            else
            {
                return null;
            }

            WriteData(products);
            return product;
        }
示例#10
0
        /// <summary>
        /// Updates an existing product
        /// </summary>
        /// <param name="id"></param>
        /// <param name="product"></param>
        /// <returns></returns>
        internal Product Save(int id, Product product)
        {
            // Read in the existing products
            var products = Retrieve();

            // Locate and replace the item
            var itemIndex = products.FindIndex(p => p.ProductId == product.ProductId);
            if (itemIndex > 0)
            {
                products[itemIndex] = product;
            }
            else
            {
                return null;
            }

            WriteData(products);
            return product;
        }
 public Product Insert(Product newProduct)
 {
     var savedProduct = _db.Products.Add(newProduct);
     _db.SaveChanges();
     return savedProduct;
 }
        /// <summary>
        /// Updates an existing product
        /// </summary>
        /// <param name="id"></param>
        /// <param name="product"></param>
        /// <returns></returns>
        public Product Save(int id, Product product)
        {
            // Read in the existing products
            var products = this.Retrieve();

            // Locate and replace the item
            Product currProduct = products.FirstOrDefault(p => p.ProductId == id);

            currProduct.Description = product.Description;
            currProduct.Price = product.Price;
            currProduct.ProductCode = product.ProductCode;
            currProduct.ProductName = product.ProductName;
            currProduct.ReleaseDate = product.ReleaseDate;

            WriteData(products);
            return product;
        }
 public IHttpActionResult Post(Product product)
 {
     var newProduct = _repository.Insert(product);
     return Ok(newProduct);
 }